以截图的测试失败+异常 [英] Take screenshot on test failure + exceptions

查看:225
本文介绍了以截图的测试失败+异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何的你知道承担测试失败和异常截图可能的解决方案?

Does any of you know possible solution for taking screenshots on test failures and exceptions?

我添加下面的代码TearDown中的() 但作为一个结果,这也使得在通过测试截图,所以它不是最好的解决办法:

I've added following code in TearDown() but as a result it also makes screenshots on passed tests, so it is not the best solution:

DateTime time = DateTime.Now;
string dateToday = "_date_" + time.ToString("yyyy-MM-dd") + "_time_" + time.ToString("HH-mm-ss");
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile((settings.filePathForScreenShots + "Exception" + dateToday + ".png"), System.Drawing.Imaging.ImageFormat.Png);



我已经发现的想法:的 http://yizeng.me/2014/02/08/take-a-screenshot - 酮异常与硒-CSHARP-eventfiringwebdriver / ,使用 WebDriverExceptionEventArgs ,但由于某些原因,这也使得一些随机的截图,没有任何合理的解释。

I've already found that idea: http://yizeng.me/2014/02/08/take-a-screenshot-on-exception-with-selenium-csharp-eventfiringwebdriver/, to use WebDriverExceptionEventArgs, but for some reasons it makes also some random screenshots without any reasonable explanation.

我发现其他的想法是对Java而不是NUnit的我与硒使用的,所以他们是相当无用的。

Other ideas I found are for Java and not for NUnit which I use with Selenium, so they are pretty useless.

推荐答案

如果你把截图逻辑在tearDown方法都将被调用每次测试结束后,不管是成功还是失败。

If you put the screenshot logic in your TearDown method it will be called after each test finishes, no matter if it succeeded or failed.

我使用具有它包装了测试,并捕获所有异常的函数的基类。当一个测试失败异常被捕获并截图取。

I use a base class that has a function which wraps the tests and catches all exceptions. When a test fails the exception is caught and a screenshot is taken.

我用我所有的Selenium测试这个基类,它看起来是这样的:

I use this base class for all my Selenium tests and it looks something like this:

public class PageTestBase
{
    protected IWebDriver Driver;

    protected void UITest(Action action)
    {
        try
        {
            action();
        }
        catch (Exception ex)
        {
            var screenshot = Driver.TakeScreenshot();

            var filePath = "<some appropriate file path goes here>";

            screenshot.SaveAsFile(filePath, ImageFormat.Png);

            // This would be a good place to log the exception message and
            // save together with the screenshot

            throw;
        }
    }
}



测试类,然后像这样的:

The test classes then look like this:

[TestFixture]
public class FooBarTests : PageTestBase
{
    // Make sure to initialize the driver in the constructor or SetUp method,
    // depending on your preferences

    [Test]
    public void Some_test_name_goes_here()
    {
        UITest(() =>
        {
            // Do your test steps here, including asserts etc.
            // Any exceptions will be caught by the base class
            // and screenshots will be taken
        });
    }

    [TearDown]
    public void TearDown()
    {
        // Close and dispose the driver
    }
}

这篇关于以截图的测试失败+异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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