实战——新浪邮箱注册及发送邮件

一、新浪邮箱注册

验证邮箱名正确,密码为空的情况

1、在page对象层中新建registry.py的模块,导入需要的库

如:基础层中的元素信息

from selenium.webdriver.common.by import By
from base.base import webUI
import time as t

# from selenium import  webdriver
#  handles:处理
# driver=webdriver.Chrome()
# driver.current_window_handle
# driver.window_handles

# 注册:registry、enroll
class Registry(webUI):
    registry=(By.LINK_TEXT,"注册")
    emailAddress=(By.XPATH,"/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[1]/span/input")
    enroll=(By.XPATH,"/html/body/div[2]/div/div/div/div/form[1]/div[3]/ul/li/div/div/a")
# 邮箱为空
    emailNull=(By.XPATH,"/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[1]/div[3]/p/abbr")
# 密码为空
    passNull=(By.XPATH,"/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[2]/span/input")
    passNullValue=(By.XPATH,"/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[2]/div[1]/p/abbr")
# 手机号码输入格式错误
    phoneError=(By.XPATH,"/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[4]/span/input")
    phoneErrorValue=(By.XPATH,"/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[4]/div[1]/p/abbr")

# 点击注册
    def clickRegistry(self):
        self.findElement(*self.registry).click()
        t.sleep(3)

# 点击立即注册
    def clickEnroll(self):
        self.findElement(*self.enroll).click()
        t.sleep(3)

    def inputEmailAddress(self,address):
        self.findElement(*self.emailAddress).send_keys(address)
        t.sleep(3)
        self.clickEnroll()


# 邮箱为空的验证
    def getEmailValue(self):
        return self.findElement(*self.emailNull).text

# 密码为空的验证
    def inputPassnNullValue(self,passnull):
        self.findElement(*self.passNull).send_keys(passnull)
        t.sleep(3)
        self.clickEnroll()

    def getPassNullValue(self):
        return self.findElement(*self.passNullValue).text

# 手机格式不正确的验证
    def inputCallValue(self,phoneError):
        self.findElement(*self.phoneError).send_keys(phoneError)
        t.sleep(3)
        self.clickEnroll()

    def getPhoneErrorValue(self):
        return self.findElement(*self.phoneErrorValue).text


# 跳转注册页面并判断是否注册,多窗口切换验证是否成功跳转
    def switchRegEmail(self):
        nowhandler=self.driver.current_window_handle
        self.clickRegistry()
        allHandlers=self.driver.window_handles
        for handler in allHandlers:
            if handler!=nowhandler:
                self.driver.switch_to.window(handler)
                self.inputEmailAddress(address='')


    def switchRegPasswd(self):
        nowhandler = self.driver.current_window_handle
        self.clickRegistry()
        allHandlers = self.driver.window_handles
        for handler in allHandlers:
            if handler != nowhandler:
                self.driver.switch_to.window(handler)
                self.inputPassnNullValue(passnull='')

    def switchRegCall(self):
        nowhandler = self.driver.current_window_handle
        self.clickRegistry()
        allHandlers = self.driver.window_handles
        for handler in allHandlers:
            if handler != nowhandler:
                self.driver.switch_to.window(handler)
                self.inputCallValue(phoneError="")

2、在编写测试用例之前,需要将分离的数据通过字典格式:key:value的形式写入

{
  "null":"请输入邮箱名",
  "format": "您输入的邮箱名格式不正确",
  "error": "登录名或密码错误",
  "absence": "请输入密码",
  "phoneerror": "请输入手机号码"
}

3、test测试层中需要导入基本的登录信息,并将输入的信息进行断言

from selenium import webdriver
import unittest
from page.login import Login
from page.registry import Registry
from utils.operationJson import readJson

class LoginTest(unittest.TestCase,Registry,Login):
    def setUp(self)->None:
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get("https://mail.sina.com.cn/")
        self.driver.implicitly_wait(30)

    def tearDown(self) -> None:
        self.driver.quit()

# 注册:邮箱地址为空的验证
    def test_registry_email_address(self):
        self.switchRegEmail()
        self.assertEqual(self.getEmailValue(),readJson()["null"])

# 注册:密码为空的验证
    def test_registry_email_password(self):
        self.switchRegPasswd()
        self.assertEqual(self.getPassNullValue(),readJson()["absence"])

# 注册:手机号码格式不正确
    def test_registry_email_phone(self):
        self.switchRegCall()
        self.assertEqual(self.getPhoneErrorValue(),readJson()["phoneerror"])

 

posted @ 2022-04-15 14:44  棠小梨  阅读(588)  评论(0编辑  收藏  举报