在异常时执行屏幕截图 [英] Perform a screenshot upon an Exception

查看:32
本文介绍了在异常时执行屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,有没有办法在发生异常时捕获屏幕截图,任何异常.我的失败"解决方案放置在 BaseTestCase, unittest.TestCase 子类中:

Hey is there a way to capture a screenshot upon a an Exception, any Exception. My 'failed' solution which is placed in the BaseTestCase, unittest.TestCase subclass:

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 但它只会捕获断言错误,而我对其他类型的错误更感兴趣

Originally it was AssertionError instead of the Exception but it would only catch assertion errors and I'm more interested in other kinds of errors

推荐答案

要在发生错误或失败时进行截图,请检查tearDown方法中当前是否有正在处理的异常:

To take a screenshot when an error or failure occurs, check if there is an exception currently being handled in the tearDown method:

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:

You could also override the methods TestResult.addError and TestResult.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()

这篇关于在异常时执行屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆