• python+selenium+unittest测试框架2-装饰器@classmethod


    装饰器@classmethod

    一、装饰器@classmethod

    多个用例可能需要多次打开浏览器,装饰器@classmethod只打开一次浏览器。classmethod是python里的类方法,@是修饰符号。

    1、setUpClass():

        @classmethod
        def setUpClass(cls):

    2、tearDownClass():

        @classmethod
        def tearDownClass(cls):

    示例:

    复制代码
    from selenium import webdriver
    from time import sleep
    import unittest
    
    class Login(unittest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.get("http://www.anenda.com")
            cls.driver.implicitly_wait(30)
            cls.driver.maximize_window()
            sleep(2)
            cls.driver.find_element_by_id("liger-textbox-user").send_keys("chen")
            cls.driver.find_element_by_id("liger-textbox-pwd_old").clear()
            cls.driver.find_element_by_id("liger-textbox-pwd").send_keys("chen")
            cls.driver.find_element_by_id("go").click()
            sleep(3)
    
        def test01(self):
            result1 = self.driver.find_element_by_xpath(".//*[@id='l-topmenu-r-bottm']/span[2]").text
            print(result1)
            result2 = "安恩达,欢迎您"
            self.assertIn(result2,result1,msg="失败原因:%s中没有发现%s"%(result1,result2))
            sleep(2)
    
        def test02(self):
            result1 = self.driver.find_element_by_id("hour").text
            print(result1)
            result2 = "2018年"
            self.assertIn(result2,result1,msg="失败原因:%s中没有发现%s"%(result1,result2))
            sleep(2)
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
    复制代码
  • 相关阅读:
    bzoj1914
    bzoj3144
    bzoj2756
    poj3177
    一些比较水的题目
    bzoj2282
    屯题50AC纪念
    Base64解码中文部分中文乱码的原因
    随机生成36位字符串
    jQuery判断某个元素是否存在某个样式
  • 原文地址:https://www.cnblogs.com/txx403341512/p/9353826.html
Copyright © 2020-2023  润新知