解決只打開一次瀏覽器,測試多條用例?

經過classmethod裝飾器,以下代碼,即只須要SetUpClass和tearDownClass執行一次便可css

# _*_ 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()

執行結果以下:web

.Will start SetUpClass,start Firefox.....
Test abc.....
.Test login.....

----------------------------------------------------------------------
Ran 2 tests in 12.226s

OK
Will stop tearDownClass,quit Firefox.....
相關文章
相關標籤/搜索