Closing a webdriver instance automatically after a failed test

My English is very poor, but I will try my best to describe the problem I am facing.

I used selenium webdriver to test the website, and the language I used to write my script is python. Because of this, I used Pyunit.

I know that if there are no exceptions in my test suite, the webdriver instance will close correctly (by the way, I used chrome), however, as soon as the exception is thrown, the script will be closed, and I will manually close the chrome.

I wonder how I can achieve this when the python process exits, all other open WebDriver instances will also be closed.

By the way, I used the page object design template, and the code below is part of my script:

class personalcenter(unittest.TestCase):

    def setUp(self):

        self.driver = webdriver.Chrome()
        self.page = personalCenter(self.driver,"admin","123456")

    def testAddWorkExp(self):

        blahblahblah...

    def tearDown(self):

        self.page.quit()
        self.driver.quit()

if __name__ == "__main__":

    unittest.main()

, java junit testNG. Pyunit?

.

+3
1

tearDown() :

, . , , . , , . , setUp() , . .

, , tearDown , - setUp . setUp, :

def setUp(self):
    self.driver = webdriver.Chrome()
    try:
        self.page = personalCenter(self.driver,"admin","123456")
    except Exception:
        self.driver.quit()
        raise
+1

All Articles