1.导入parameterized,实现参数化
导入命令:
from parameterized import param,parameterized
2.分离
分离:把固件分离出来,方便以后换浏览器或者网址测试的修改,否则需要多处修改,使用继承的思想
分离的代码:定义一个Init的类
class Init(unittest.TestCase): 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.close()
调用分离的固件
class TestSina(Init):
3.登录部分的参数化
import unittest from selenium import webdriver from selenium.webdriver.common.by import By import time from parameterized import param,parameterized
class Init(unittest.TestCase):
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.close()
class TestSina(Init):
@parameterized.expand([
param('','','请输入邮箱名'),
param('2334','','您输入的邮箱名格式不正确'),
param('121331sd@sina.com','123','登录名或密码错误')
])
def test_sina_login_param(self,username,password,result):
'''密码错误'''
self.driver.find_element(By.ID, 'freename').send_keys(username)
self.driver.find_element(By.ID, 'freepassword').send_keys(password)
self.driver.find_element(By.CLASS_NAME, 'loginBtn').click()
time.sleep(1)
test_password = self.driver.find_element(By.XPATH,
'/html/body/div[1]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]').text
self.assertEqual(test_password, result)
4.注册
从登录页面进行窗口切换到注册页面
再针对注册页面实行分离和参数化
此处是分离窗口切换和需要点击同意书的哪一步操作
class Qiehuan(unittest.TestCase): 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) '''打开当前窗口''' self.nowHandle=self.driver.current_window_handle '''点击注册''' self.driver.find_element(By.CLASS_NAME,'registerBtn').click() '''打开所有窗口''' self.allHandlers = self.driver.window_handles '''循环所有窗口并切换到到当前窗口''' for handle in self.allHandlers: if handle!= self.nowHandle: self.driver.switch_to.window(handle) time.sleep(2) '''点击同意协议''' self.driver.find_element(By.ID,'agreement1').click()
调用分离出来的固件和窗口切换,并且验证用户名和密码部分出现的所有错误
class Testsina(Qiehuan): @parameterized.expand([ param('','请输入邮箱名'), param('1','邮箱名必须是4-16个字符之间(包括4、16)'), param('QQQQ','不能有大写字母'), param('@!!!!', '不能有全角字符'), param('@5678', '邮箱名仅允许使用小写英文、数字或下划线'), param('哈哈哈哈', '不能有中文'), param('fghj', '该邮箱名禁止注册,请换一个再试'), ]) def test_sina_loginError(self,email,result): self.driver.find_element(By.NAME,'email').send_keys(email) self.driver.find_element(By.CLASS_NAME, 'subIco').click() time.sleep(2) loginError=self.driver.find_element(By.XPATH,'/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[1]/div[3]/p/abbr').text self.assertEqual(loginError,result) @parameterized.expand([ param('','请输入密码'), param('1','密码的长度应该在6-16个字符之间'), param(' ','6-16位字符(字母、数字、特殊符号),区分大小写') ]) def test_sina_passwError(self, password, result): self.driver.find_element(By.NAME,'psw').send_keys(password) self.driver.find_element(By.CLASS_NAME,'subIco').click() time.sleep(2) passwError = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[2]/div[1]/p/abbr').text time.sleep(1) self.assertEqual(passwError, result) def tearDown(self) -> None: self.driver.close()
在主函数运行
if __name__ == '__main__': '''参数化的执行必须把鼠标放到main函数上才能执行''' unittest.main()
建立和此目录平级的运行代码,以做输出报告的操作
运行部分的代码如下:
记得导入:import HTMLTestRunner
import unittest import os import HTMLTestRunner def allTests(): '''使用测试套件加载所有的模块''' suite=unittest.TestLoader().discover(start_dir=os.path.dirname(__file__), pattern='test_*.py') return suite def base_dir(): return os.path.dirname(os.path.dirname(__file__)) def run(): filename=os.path.join(base_dir(),'report','report_sina.html') fp=open(filename,'wb') runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='新浪自动化测试报告',description='Sina自动化测试报告') runner.run(allTests()) if __name__ == '__main__': run()
运行完成之后的结果:
针对以上操作的完整代码如下:
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from parameterized import param,parameterized
'''
分离:把固件分离出来,方便以后换浏览器或者网址测试的修改,否则需要多处修改
继承的思想
'''
class Init(unittest.TestCase):
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.close()
class TestSina(Init):
@parameterized.expand([
param('','','请输入邮箱名'),
param('2334','','您输入的邮箱名格式不正确'),
param('121331sd@sina.com','123','登录名或密码错误')
])
def test_sina_login_param(self,username,password,result):
'''密码错误'''
self.driver.find_element(By.ID, 'freename').send_keys(username)
self.driver.find_element(By.ID, 'freepassword').send_keys(password)
self.driver.find_element(By.CLASS_NAME, 'loginBtn').click()
time.sleep(1)
test_password = self.driver.find_element(By.XPATH,
'/html/body/div[1]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]').text
self.assertEqual(test_password, result)
class Qiehuan(unittest.TestCase):
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)
'''打开当前窗口'''
self.nowHandle=self.driver.current_window_handle
'''点击注册'''
self.driver.find_element(By.CLASS_NAME,'registerBtn').click()
'''打开所有窗口'''
self.allHandlers = self.driver.window_handles
'''循环所有窗口并切换到到当前窗口'''
for handle in self.allHandlers:
if handle!= self.nowHandle:
self.driver.switch_to.window(handle)
time.sleep(2)
'''点击同意协议'''
self.driver.find_element(By.ID,'agreement1').click()
class Testsina(Qiehuan):
@parameterized.expand([
param('','请输入邮箱名'),
param('1','邮箱名必须是4-16个字符之间(包括4、16)'),
param('QQQQ','不能有大写字母'),
param('@!!!!', '不能有全角字符'),
param('@5678', '邮箱名仅允许使用小写英文、数字或下划线'),
param('哈哈哈哈', '不能有中文'),
param('fghj', '该邮箱名禁止注册,请换一个再试'),
])
def test_sina_loginError(self,email,result):
self.driver.find_element(By.NAME,'email').send_keys(email)
self.driver.find_element(By.CLASS_NAME, 'subIco').click()
time.sleep(2)
loginError=self.driver.find_element(By.XPATH,'/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[1]/div[3]/p/abbr').text
self.assertEqual(loginError,result)
@parameterized.expand([
param('','请输入密码'),
param('1','密码的长度应该在6-16个字符之间'),
param(' ','6-16位字符(字母、数字、特殊符号),区分大小写')
])
def test_sina_passwError(self, password, result):
self.driver.find_element(By.NAME,'psw').send_keys(password)
self.driver.find_element(By.CLASS_NAME,'subIco').click()
time.sleep(2)
passwError = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[2]/div[1]/p/abbr').text
time.sleep(1)
self.assertEqual(passwError, result)
def tearDown(self) -> None:
self.driver.close()
if __name__ == '__main__':
'''参数化的执行必须把鼠标放到main函数上才能执行'''
unittest.main()