• 解决只打开一次浏览器,测试多条用例?


    通过classmethod装饰器,如下代码,即只需要SetUpClass和tearDownClass执行一次即可

    # _*_ encoding:utf-8 _*_
    
    from selenium import webdriver
    import unittest
    import time
    
    class login(unittest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Firefox()
            cls.driver.implicitly_wait(10)
            cls.base_url = '网址'
            cls.driver.get(cls.base_url)
            print ("Will start SetUpClass,start Firefox.....")
    
        def test_login_mlf(self):
            driver = self.driver
            driver.get(self.base_url)
            driver.find_element_by_name("UserName").clear()
            driver.find_element_by_name("UserName").send_keys("account")
            driver.find_element_by_name("Password").clear()
            driver.find_element_by_name("Password").send_keys("aa123456")
            driver.find_element_by_css_selector(".btn.btn-primary.btn-lg.btn-block").click()
            self.assertIsNotNone(driver.find_element_by_link_text("个人中心"))
            print ("Test login.....")
    
        def test_abc(self):
            print ("Test abc.....")
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
            print ("Will stop tearDownClass,quit Firefox.....")
    
    if __name__ == '__main__':
        unittest.main()

    执行结果如下:

    .Will start SetUpClass,start Firefox.....
    Test abc.....
    .Test login.....
    
    ----------------------------------------------------------------------
    Ran 2 tests in 12.226s
    
    OK
    Will stop tearDownClass,quit Firefox.....
  • 相关阅读:
    从零开始学asyncio(中)
    从零开始学asyncio(上)
    Java 后台多次获取requestBody
    Swift中的构造器
    CIDetector 相册识别二维码出错
    Swift-类和结构体
    定时器的使用以及注意事项
    ios-视图控制器跳转时生命周期的调用
    使用NSString的一些注意事项
    CALayer的锚点
  • 原文地址:https://www.cnblogs.com/cindy-cindy/p/8250884.html
Copyright © 2020-2023  润新知