解决只打开一次浏览器,测试多条用例?
通过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.....