在例外情况下执行屏幕截图

时间:2016-06-03 07:45:32

标签: python selenium testing

嘿有没有办法在异常,任何例外情况下捕获屏幕截图。我的'失败'解决方案放在BaseTestCaseunittest.TestCase子类中:

class BaseTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""some code"""

@property 
def failureException(self):
    class MyFailureException(Exception):
        def __init__(self_, *args, **kwargs):
            screenshot_dir = '../error_log'
            self.driver.save_screenshot('{0}/{1}.jpeg'.format(screenshot_dir, self.id()))
            return super().__init__(*args, **kwargs)

    MyFailureException.__name__ = Exception.__name__
    return MyFailureException

最初它是AssertionError而不是Exception,但它只能捕获断言错误,我对其他类型的错误更感兴趣

4 个答案:

答案 0 :(得分:4)

要在发生错误或失败时截取屏幕截图,请检查当前是否在tearDown方法中处理了异常:

import unittest, sys, exceptions
from selenium import webdriver


class TestCaseBase(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def tearDown(self):
        type, value, traceback = sys.exc_info()
        if type is exceptions.AssertionError :
            self.driver.save_screenshot(r'screenshot-failure.png')
        elif type is exceptions.Exception :
            self.driver.save_screenshot(r'screenshot-error.png')

        self.driver.quit()

class MyTestCase(TestCaseBase):

    def test_should_take_screenshot_on_failure(self):
        self.driver.get("http://stackoverflow.com/")
        self.assertTrue(False)

    def test_should_take_screenshot_on_error(self):
        self.driver.get("http://stackoverflow.com/")
        raise Exception("my exception")


if __name__ == '__main__':
    unittest.main()

您还可以覆盖方法TestResult.addErrorTestResult.addFailure

import unittest
from selenium import webdriver


class TestCaseBase(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def tearDown(self):
        self.driver.quit()

    def run(self, result=None):
        super(TestCaseBase, self).run(TestResultEx(result, self))


class MyTestCase(TestCaseBase):

    def test_should_take_screenshot_on_failure(self):
        self.driver.get("http://stackoverflow.com/")
        self.assertTrue(False)

    def test_should_take_screenshot_on_error(self):
        self.driver.get("http://stackoverflow.com/")
        raise Exception("my exception")


class TestResultEx(object):

    def __init__(self, result, testcase):
        self.result = result
        self.testcase = testcase

    def __getattr__(self, name):
        return object.__getattribute__(self.result, name)

    def addError(self, test, err):
        self.result.addError(test, err)
        self.testcase.driver.save_screenshot(r'screenshot-error.png')

    def addFailure(self, test, err):
        self.result.addFailure(test, err)
        self.testcase.driver.save_screenshot(r'screenshot-failure.png')


if __name__ == '__main__':
    unittest.main()

答案 1 :(得分:2)

如果测试用例函数引发异常,您可以使用装饰器来捕获。

为此,您首先需要创建一个装饰器函数,该函数返回您希望将测试用例包装起来的函数,如下例所示。

def screenshot(func):
    def screenshot_exception(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except:
            self.driver.save_screenshot('{0}/{1}.jpeg'.format(screenshot_dir, self.id()))
            raise
    return screenshot_exception

然后,对于您想获取截图的测试用例,您只需要在测试用例上方添加装饰器。

@screenshot
def test_case_1():
    raise NameError('Test Error')

答案 2 :(得分:0)

最好将异常日志保存到文件中,而不是截取屏幕截图。练习时请查看以下代码:

>>> s = [1, 2]
>>> try:
...  s[3]
... except IndexError as e:
...  print(e)
...
list index out of range

如您所见,可以捕获(和打印)错误消息。尝试使用open()代替print()来简单地将错误消息保存到文件中。这是一个例子:

s = [1, 2]
try:
    s[3]
except IndexError as e:
    file = open("logfile", "a")  # 'a' means append to file
    file.write(e)
    file.close()

这是解决问题的最简单方法。

此外,捕捉可能发生的任何异常并不明智。最好尝试捕捉最有可能发生的2,3。

答案 3 :(得分:0)

您正在寻找enter image description here

try:
    # your code (which may raise an Exception) goes here
except:
    # this code only runs if the code in the try block raised an exception
    driver.save_screenshot(screenshot_dir)
    raise  # this will propagate the exception