Closing the webdriver instance automatically after the test failed

justuno

My English is very poor but I'll try my best to describe the problem I encountered.

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

I know that if my test suite have no exceptions,the webdriver instance will be closed correctly,(by the way, I used chrome) however,once a exception was threw,the script will be shut down and I have to close chrome manually.

I wonder that how can I achieved that when a python process quits , any remaining open WebDriver instances will also be closed.

By the way, I used Page Object Design Pattern,and the code below is a 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()

I haved searched the solution of this problem for a long time ,but almost every answer is depended on java and junit or testNG.How can I deal with this issue with Pyunit?

Thanks for every answer.

E.Z.

From the tearDown() documentation:

Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly careful about checking internal state. Any exception raised by this method will be considered an error rather than a test failure. This method will only be called if the setUp() succeeds, regardless of the outcome of the test method. The default implementation does nothing.

So, the only case where tearDown will not be called is when setUp fails. Therefore I would simply catch the exception inside setUp, close the driver, and re-raise it:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Closing the webdriver instance automatically after the test failed

From Dev

program automatically closing after writing to socket

From Dev

program automatically closing after writing to socket

From Dev

Karma closing browsers after gulp test

From Dev

Closing server after mocha test, but data is still there

From Dev

TestNG browser not closing after each test

From Dev

Make Jenkins run failed test automatically

From Dev

AWS auto scaling test failed instance

From Dev

SRWebsocket connection is closing automatically after keeping app idle for sometime in iOS

From Dev

smiley popup window is not closing automatically after sending message

From Dev

Automatically build allure report after test completion

From Dev

Change a WebDriver::Wait instance's timeout after instantiation

From Dev

How to continue test execution after assertion failed?

From Dev

Restart activity from ActivityTestRule after failed test

From Dev

Action after failed coded ui test

From Dev

OCMock automatically use mocked instance in code under test?

From Dev

Place Picker automatically closing

From Dev

JavaFX alert automatically closing

From Dev

Form tag closing automatically

From Dev

Place Picker automatically closing

From Dev

Gradle execute task after test phase even if test has failed

From Dev

How to stay logged in after a test with PHPUnit, Selenium and php-webdriver

From Dev

How make webdriver not to close browser window after each test?

From Dev

Xunit create new instance of Test class for every new Test ( using WebDriver and C#)

From Dev

Closing the instance of that workbook excel?

From Dev

How to run multiple test methods in same browser instance without closing it (C#, SeleniumWebDriverz NUnit)?

From Dev

Detect which plugin automatically write the closing comment character just after I write the opening one

From Dev

android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed even after closing cursor

From Dev

android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed even after closing cursor

Related Related

  1. 1

    Closing the webdriver instance automatically after the test failed

  2. 2

    program automatically closing after writing to socket

  3. 3

    program automatically closing after writing to socket

  4. 4

    Karma closing browsers after gulp test

  5. 5

    Closing server after mocha test, but data is still there

  6. 6

    TestNG browser not closing after each test

  7. 7

    Make Jenkins run failed test automatically

  8. 8

    AWS auto scaling test failed instance

  9. 9

    SRWebsocket connection is closing automatically after keeping app idle for sometime in iOS

  10. 10

    smiley popup window is not closing automatically after sending message

  11. 11

    Automatically build allure report after test completion

  12. 12

    Change a WebDriver::Wait instance's timeout after instantiation

  13. 13

    How to continue test execution after assertion failed?

  14. 14

    Restart activity from ActivityTestRule after failed test

  15. 15

    Action after failed coded ui test

  16. 16

    OCMock automatically use mocked instance in code under test?

  17. 17

    Place Picker automatically closing

  18. 18

    JavaFX alert automatically closing

  19. 19

    Form tag closing automatically

  20. 20

    Place Picker automatically closing

  21. 21

    Gradle execute task after test phase even if test has failed

  22. 22

    How to stay logged in after a test with PHPUnit, Selenium and php-webdriver

  23. 23

    How make webdriver not to close browser window after each test?

  24. 24

    Xunit create new instance of Test class for every new Test ( using WebDriver and C#)

  25. 25

    Closing the instance of that workbook excel?

  26. 26

    How to run multiple test methods in same browser instance without closing it (C#, SeleniumWebDriverz NUnit)?

  27. 27

    Detect which plugin automatically write the closing comment character just after I write the opening one

  28. 28

    android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed even after closing cursor

  29. 29

    android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed even after closing cursor

HotTag

Archive