(五)引入装饰器@classmethod

我们知道setUp()和setDown()的作用是在每条测试用例执行前准备测试环境以及用例测试结束后恢复测试环境,如果我们执行的测试类下所有测试用例的环境准备和环境复原的操作都是一样的,那么我们就没必要每条测试用例执行前都执行一次setUp()和setDown()的操作。在运行测试类前只执行一次环境的准备,测试类运行结束后只执行一次环境复原的操作,这时我们就可以引入装饰器@classmethod

代码:

# coding = utf-8
import unittest
import warnings
from selenium import webdriver
from time import sleep
# 驱动文件路径
driverfile_path = r'D:\coship\Test_Framework\drivers\IEDriverServer.exe'

class CmsLoginTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print("test start!")
        # 这行代码的作用是忽略一些告警打印
        warnings.simplefilter("ignore", ResourceWarning)
        cls.driver = webdriver.Ie(executable_path=driverfile_path)
        cls.driver.get("http://172.21.13.83:28080/")

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        print("test end!")

    def test_login1(self):
        '''用户名、密码为空'''
        self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
        self.driver.find_element_by_css_selector("[name='admin.password']").clear()
        self.driver.find_element_by_css_selector("#imageField").click()
        error_message1 = self.driver.find_element_by_css_selector("[for='loginName']").text
        error_message2 = self.driver.find_element_by_css_selector("[for='textfield']").text
        self.assertEqual(error_message1, '用户名不能为空')
        self.assertEqual(error_message2, '密码不能为空')


    def test_login2(self):
        '''用户名正确,密码错误'''
        self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
        self.driver.find_element_by_css_selector("[name='admin.password']").clear()
        self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
        self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("123456")
        self.driver.find_element_by_css_selector("#imageField").click()
        error_message = self.driver.find_element_by_css_selector(".errorMessage").text
        self.assertEqual(error_message, '密码错误,请重新输入!')

    def test_login3(self):
        '''用户名不存在'''
        self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
        self.driver.find_element_by_css_selector("[name='admin.password']").clear()
        self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("test007")
        self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("123456")
        self.driver.find_element_by_css_selector("#imageField").click()
        error_message = self.driver.find_element_by_css_selector(".errorMessage").text
        self.assertEqual(error_message, '用户不存在!')

    def test_login4(self):
        '''用户名、密码正确'''
        self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
        self.driver.find_element_by_css_selector("[name='admin.password']").clear()
        self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
        self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("111111")
        self.driver.find_element_by_css_selector("#imageField").click()
        sleep(1)
        self.driver.switch_to.frame("topFrame")
        username = self.driver.find_element_by_css_selector("#nav_top>ul>li>a").text
        self.assertEqual(username,"autotest")


if __name__ == "__main__":
    unittest.main(verbosity=2)

  

上一篇:springboot利用AOP记录每一次请求的日志


下一篇:1615:【例 1】序列的第 k 个数