Automatic screenshots when test fail by Selenium Webdriver in Python

前端 未结 7 1218
小蘑菇
小蘑菇 2020-12-13 04:42

I want to automatic capturing screenshots if my webdriver tests failed (any exception or assertion error). I am using Python unittest and Selenium Webdriver. Does anyone hav

7条回答
  •  心在旅途
    2020-12-13 05:00

    Another method would be to add the following to your tearDown method:

    if sys.exc_info()[0]:
        test_method_name = self._testMethodName
        self.driver.save_screenshot("Screenshots/%s.png" % test_method_name)
    

    This would be assuming a test class like this:

    class SeleniumTest(unittest2.TestCase):
        ...
    
        def tearDown(self):
            if sys.exc_info()[0]:
                test_method_name = self._testMethodName
                self.driver.save_screenshot("Screenshots/%s.png" % test_method_name)
            super(SeleniumTest, self).tearDown()
    
        def test_1(self):
            ...
    
        def test_2(self):
            ...
    

提交回复
热议问题