Unittest框架--自动化

Python中方法的传递Unittest框架--自动化_第1张图片

Unittest框架--自动化_第2张图片

参数化

pip install parameterized -i https://pypi.douban.com/simple

Unittest框架--自动化_第3张图片
需求:验证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):
    # 用类级提取“公因式”
    @classmethod
    def 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()
    @classmethod
    def 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").text
        self.assertIn(result,msg)

Unittest框架--自动化_第4张图片

你可能感兴趣的:(自动化,python,运维)