UI自动化测试实战之新浪邮箱注册/发送邮件
一、下面以新浪邮箱的注册为例
1.验证邮箱地址为空,密码为空2个测试用例,首先需要创建一个registry.py模块
from selenium.webdriver.common.by import By from base.base import WebUI import time as t 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') lijiReg=(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') passwd=(By.XPATH,'/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[2]/span/input') emailNull2=(By.XPATH,'/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[2]/div[1]/p/abbr') def clickRegistry(self): '''点击注册''' self.findElement(*self.registry).click() t.sleep(3) def clickLijiReg(self): #点击立即注册 self.findElement(*self.lijiReg).click() t.sleep(3) def inputEmailAddress(self,address):#输入邮箱地址 self.findElement(*self.emailAddress).send_keys(address) t.sleep(3) self.clickLijiReg() def getEmailValue(self):#获取邮箱文本值 return self.findElement(*self.emailNull).text def inputPasswd(self,password):#输入密码 self.findElement(*self.passwd).send_keys(password) t.sleep(3) self.clickLijiReg() def getPasswdValue(self):#获取密码文本值 return self.findElement(*self.emailNull2).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.inputPasswd(password='')
2.创建一个test_register.py模块,在这里执行用例
from selenium import webdriver import unittest from page.login import Login from page.registry import Registry import time as t from utils.operationJson import readJson class RegistryTest(unittest.TestCase,Registry,Login): def setUp(self)->None: self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('http://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_passwd(self): #注册:密码为空的验证 self.switchRegPasswd() self.assertEqual(self.getPasswdValue(), readJson()['passwdnull'])
from selenium.webdriver.common.by import By from base.base import WebUI import time as t from selenium.webdriver.support.select import Select class Send(WebUI): write=(By.XPATH,'/html/body/div[2]/div/div[5]/div[2]/div[1]/div[1]/div[1]/ul/li[1]/a') sendRen=(By.XPATH,'/html/body/div[2]/div/div[5]/div[2]/div[2]/div[2]/div[1]/div/div[1]/div[2]/form/div/table/tbody/tr[2]/td/ul/li/input') subject=(By.XPATH,'/html/body/div[2]/div/div[5]/div[2]/div[2]/div[2]/div[1]/div/div[1]/div[2]/form/div/table/tbody/tr[6]/td/input') emailContent=(By.XPATH,'/html/body') sendEmail=(By.XPATH,'/html/body/div[2]/div/div[5]/div[2]/div[2]/div[2]/div[1]/div/div[1]/div[1]/span/span[1]/a/i[2]') success=(By.XPATH,'/html/body/div[2]/div/div[5]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div[1]/div/p[1]') def clickWrite(self): '''点击写信''' self.findElement(*self.write).click() t.sleep(3) def inputSendRen(self,content):#要输入内容的地方就写形式参数 '''点击收件人''' self.findElement(*self.sendRen).send_keys(content) t.sleep(3) def inputSubject(self,content): '''点击主题''' self.findElement(*self.subject).send_keys(content) t.sleep(3) def clickSendEmail(self): '''点击发送邮件''' self.findElement(*self.sendEmail).click() t.sleep(3) def getSuccess(self):#获取发送成功的文本值 return self.findElement(*self.success).text def sendEmailSuccess(self,sendPerson,subject):#方法封装 self.clickWrite() self.inputSendRen(content=sendPerson) self.inputSubject(content=subject) self.clickSendEmail()
2.再建一个test_sendEmail.py模块
from selenium import webdriver import unittest from page.login import Login from page.send import Send import time as t from utils.operationJson import readJson class SendTest(unittest.TestCase,Login,Send): def setUp(self)->None: self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('http://mail.sina.com.cn/') self.driver.implicitly_wait(30) def tearDown(self) -> None: self.driver.quit() def test_send_email(self): """发送邮件""" self.singLogin(username='wuya1303@sina.com',password='admin123') self.sendEmailSuccess(sendPerson='1754788101@qq.com',subject='自动化测试') self.assertEqual(self.getSuccess(),readJson()['success'])