python selenium multiple test cases

后端 未结 6 2070
孤街浪徒
孤街浪徒 2020-12-18 07:25

I have the following code in python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Sele         


        
相关标签:
6条回答
  • 2020-12-18 07:49

    Initialize the firefox driver in __init__:

    class HomePageTest(unittest.TestCase):
        def __init__(self):
            self.driver = webdriver.Firefox()
            self.driver.implicitly_wait(30)
            self.base_url = "https://somewebsite.com"
            self.verificationErrors = []
    
        ...
    
        def tearDown(self):
            self.driver.quit()
    
    0 讨论(0)
  • 2020-12-18 07:52
    def suite():  
    
        suite = unittest.TestSuite()  
        suite.addTest(HomePageTest("test_home_page"))  
        suite.addTest(HomePageTest("test_whatever"))  
        return suite  
    
    if __name__ == "__main__":
    
        unittest.TextTestRunner().run(suite()) 
    

    Run many testcase with the same Firefox instance. Btw, i have a question hope someone could know it and answer to me. How could i execute the same testcase in different browsers?

    0 讨论(0)
  • 2020-12-18 07:58

    Usually you want the browser to close between tests so that you start each test with a clean cache, localStorage, history database, etc. Closing the browser between tests does slow down the tests, but it saves in debug time because a test doesn't interact with the browser cache and history of a previous test.

    0 讨论(0)
  • 2020-12-18 08:00

    use setUpClass and tearDownClass with @classmethod decorator.

    0 讨论(0)
  • 2020-12-18 08:01

    use setUpClass and tearDownClass

    class HomePageTest(unittest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Firefox()
    
        def setUp(self):
            self.base_url = "https://somewebsite.com"
            self.verificationErrors = []
    
        def tearDown(self):
            self.driver.get(self.base_url)
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
    
    
    if __name__ == "__main__":
        unittest.main()
    
    0 讨论(0)
  • 2020-12-18 08:15

    I think WebDriver Plus solves your problem. Here it is. http://webdriverplus.org/en/latest/browsers.html You can reuse the same browser instance across all unit tests using this .

    0 讨论(0)
提交回复
热议问题