Python中方法的传递
参数化
pip install parameterized -i https://pypi.douban.com/simple
需求:验证Tpshop登录
# 断言+参数化
import time
import unittest
from parameterized import parameterized
from selenium import webdriver
from selenium.webdriver.common.by import By
class login_test(unittest.TestCase):# 用类级提取“公因式”@classmethoddef setUpClass(cls) :# cls.driver 创建浏览器传入命令cls.driver = webdriver.Chrome()cls.driver.maximize_window()cls.driver.implicitly_wait(15)cls.driver.get("http://localhost/")cls.driver.find_element(By.CSS_SELECTOR, ".red").click()# 每次跳转到登录页面都刷新def setUp(self) :self.driver.refresh()@classmethoddef tearDownClass(cls):cls.driver.quit()# 参数化@parameterized.expand([("13800138006","123456","8888","安全退出")])def test_login03_success(self,username,pwd,code,result):self.driver.find_element_by_xpath("//*[contains(@autofocus,'autofocus')]").send_keys(username)self.driver.find_element_by_xpath("//div/input[@class='text_cmu' and @name = 'password']").send_keys(pwd)self.driver.find_element_by_xpath("//input[@name='verify_code']").send_keys(code)self.driver.find_element_by_xpath("//a[@class='J-login-submit']").click()time.sleep(3)msg = self.driver.find_element(By.XPATH, "//*[text()='安全退出']").text# 使用断言(机器自动比对,判读,得出结果)self.assertIn(result,msg)print(msg)self.driver.quit()@parameterized.expand([("13800130006","123456","8888","账号不存在"),("13800138006","123056","8888","密码错误")])def test_login01_username_is_error(self,username,pwd,code,result):# 输入用户名self.driver.find_element_by_id("username").send_keys(username)# 输入密码self.driver.find_element_by_id("password").send_keys(pwd)# 输入验证码self.driver.find_element_by_id("verify_code").send_keys(code)# 点击‘登录’self.driver.find_element_by_name("sbtbutton").click()time.sleep(3)# 断言提示信息msg = self.driver.find_element_by_class_name("layui-layer-content").textself.assertIn(result,msg)