一、测试夹具(固件,钩子函数,前后置)TestFixtrue详解
setUp/tearDown 在测试用例之前和之后执行
setUpClass/tearDownClass 在测试类之前和之后执行(必须要加@classmethod装饰器)
setUpModule/tearDownModule 在测试模块之前和之后执行
夹具封装:
自动化测试框架必备思想(有很多重复的代码时)
二、忽略测试用例
class TestUnittest(MyUint):
a = 18
@unittest.skip('无条件忽略')
def test_01(self):
print("测试01")
@unittest.skipIf(a >= 16, "满足条件忽略")
def test_02(self):
print("测试02")
@unittest.skipUnless(a >= 20, "不满足条件忽略")
def test_03(self):
print("测试03")
三、断言(判断用例是否执行成功)
实际工作中:
assertEqual(a,b)
assertIn(a,b)
assertTrue(x)
例子:
class TestShopnc(MyUint):
def test_01_add_user(self):
global driver
# 打开浏览器
driver = webdriver.Chrome()
driver.implicitly_wait(
10
)
# 加载网页
driver.get("http://localhost:9527/login?redirect=%2Fdashboard")
# 输入用户名
driver.find_element(By.CLASS_NAME, "el-input__inner").clear()
driver.find_element(By.CLASS_NAME,"el-input__inner").send_keys("")
# 输入密码
driver.find_element(By.NAME, "pwd").clear()
driver.find_element(By.NAME,"pwd").send_keys("")
# 通过Cookie绕过验证码
print("------------------------------")
print("登陆之前")
cks = driver.get_cookies()
for ck in cks:
print(ck)
# 睡10秒,手动输入验证码
time.sleep(10)
print("------------------------------")
print("登陆之后")
# 登陆之后再次打印cookie
cks = driver.get_cookies()
for ck in cks:
print(ck)
登陆后获取cookie再重新赋值(绕过验证码)
class TestShopnc(MyUint):
def test_01_add_user(self):
global driver
# 打开浏览器
driver = webdriver.Chrome()
driver.implicitly_wait(
10
)
# 加载网页
driver.get("http://localhost:9527/login?redirect=%2Fdashboard")
# 通过Cookie绕过验证码(解决所有的不需要第三方验证的登陆)
driver.add_cookie({'name': 'sidebarStatus', 'value': '0'})
driver.add_cookie({'name': 'Authori-zation', 'value': 'd8c81eecb1f846f1bb2d3dd3c0d57894'})
driver.add_cookie({'name': 'JavaInfo',
'value': '{%22id%22:1%2C%22account%22:%22admin%22%2C%22realName%22:%22%E8%B6%85%E7%AE%A1'
'%22%2C%22isSms%22:false%2C%22token%22:%22d8c81eecb1f846f1bb2d3dd3c0d57894%22}'})
# 睡两秒后加载登陆之后的网页
time.sleep(2)
driver.get("http://localhost:9527/dashboard")
最终实际案例:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from common.my_unit import MyUint
class TestShopnc(MyUint):
def test_01_add_user(self):
global driver
# 打开浏览器
driver = webdriver.Chrome()
driver.implicitly_wait(
10
)
# 加载网页
driver.get("http://localhost:9527/login?redirect=%2Fdashboard")
# 通过Cookie绕过验证码(解决所有的不需要第三方验证的登陆)
driver.add_cookie({'name': 'sidebarStatus', 'value': '0'})
driver.add_cookie({'name': 'Authori-zation', 'value': 'd8c81eecb1f846f1bb2d3dd3c0d57894'})
driver.add_cookie({'name': 'JavaInfo',
'value': '{%22id%22:1%2C%22account%22:%22admin%22%2C%22realName%22:%22%E8%B6%85%E7%AE%A1'
'%22%2C%22isSms%22:false%2C%22token%22:%22d8c81eecb1f846f1bb2d3dd3c0d57894%22}'})
# 睡两秒后加载登陆之后的网页
time.sleep(2)
driver.get("http://localhost:9527/dashboard")
# 断言当前路径中包含有后台的主页路径
self.assertIn("dashboard", driver.current_url)
# 点击【会员管理】
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='app']/div/div[2]/section/div[1]/div[2]/div/div[6]/div/div/a").click()
# 点击【编辑】
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='app']/div/div[2]/section/div[1]/div[1]/div[2]/div[1]/div[4]/div[2]/table/tbody/tr[1]/td[9]/div/a/button").click()
# 输入作者
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='app']/div/div[2]/section/div[1]/div/div/div/form/div[2]/div/div/input").clear()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='app']/div/div[2]/section/div[1]/div/div/div/form/div[2]/div/div/input").send_keys("杨柳轩轩")
# 保存
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='app']/div/div[2]/section/div[1]/div/div/div/form/div[9]/div/button").click()
# 断言
time.sleep(1)
self.assertEqual(driver.find_element(By.CLASS_NAME,"el-message__content").text,'编辑文章成功')