Take screenshot on test failure + exceptions

25,033

Solution 1

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.

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
    }
}

Solution 2

In C# I use NUnit 3.4. This offeres the OneTimeTearDown method that is able to access the TestContext including the state of the previous executed test. Do not use TearDown because it is not executed after a test fails ;)

using OpenQA.Selenium;
using System.Drawing.Imaging;

...

[OneTimeTearDown]
public void OneTimeTearDown()
{
    if (TestContext.CurrentContext.Result.Outcome != ResultState.Success)
    {
        var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
        screenshot.SaveAsFile(@"C:\TEMP\Screenshot.jpg", ImageFormat.Jpeg);
    }
}

Solution 3

For greater justice here is the code for the MSTest

public TestContext TestContext { get; set; }

[TestCleanup]
public void TestCleanup()
{
  if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
  {
    var screenshotPath = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss.fffff}.png";
    MyDriverInstance.TakeScreenshot().SaveAsFile(screenshotPath);
    TestContext.AddResultFile(screenshotPath);
  }
}

Solution 4

YOu can achieve this easily in TestNG suite FIle Create a ScreenShot method like Below

public static void CaptureDesktop (String imgpath)
    {
        try
        {

            Robot robot = new Robot();
            Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screenRect = new Rectangle(screensize);
            BufferedImage screenshot = robot.createScreenCapture(screenRect);
            //RenderedImage screenshot = robot.createScreenCapture(screenRect);
        ImageIO.write(screenshot, "png" , new File(imgpath));

        }

In above method i used robot class so that you can take screen shot of Dekstop also(window+WebPage) and you can call this method in different Listener class which will implements ITestListener Interface. call your screen Shot method in OntestFailure() of that Listener Class

@Override
    public void onTestFailure(ITestResult arg0) {


        String methodname = arg0.getMethod().getMethodName();
        String imgpath = "./Screenshot/"+methodname+".jpg";
        Guru99TakeScreenshot.CaptureDesktop(imgpath);

    }

This code is working for me. But this code is written in JAVA. I hope this will work in C# if not i wish this code can help you

Share:
25,033
Tomasz Tarnowski
Author by

Tomasz Tarnowski

Updated on July 25, 2020

Comments

  • Tomasz Tarnowski
    Tomasz Tarnowski almost 4 years

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

    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);
    

    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.

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

  • Frank H.
    Frank H. about 7 years
    It would be nice if you could capture the name of the test and include that in the filename of the screenshot. But I don't see how that can be done with this method?
  • Erik Öjebo
    Erik Öjebo about 7 years
    @FrankH. You should be able to to that via reflection in the UITest method in this example. Since the UITest method is called from the test method, you should be able to get the name of the calling method from the UITest method. Try something along these lines: stackoverflow.com/questions/171970/…
  • kirbycope
    kirbycope over 6 years
    So long as any SetUp method runs without error, the TearDown method is guaranteed to run. Source. I just found your answer and used the if statement in a TearDown without any issues. IMO this should be the accepted answer, though.
  • AussieJoe
    AussieJoe almost 6 years
    Are you sure your exceptions are logging the line number correctly? Is it logging exceptions from the correct assembly? We do something similar but have lost our line numbers because of AssemblyInfo is different when using wrappers. To clarify, we get Exceptions logged with the correct messages, but the line number info is missing and always 0. Is this the same case for you?
  • trailmax
    trailmax almost 5 years
    Please correct me if I'm wrong, but I thought OneTimeTearDown is only executed after all the tests in the fixture are done.So it'll be too late to get any screenshots.
  • pabrams
    pabrams over 3 years
    Where do you get the extent object?
  • Nate R
    Nate R almost 3 years
    I agree with trailmax. It'd be too late to capture when something else or some other tests clicked other stuff. It should be done in the catch block and I like the global try catch example from the top example, in addition you can use it in extension methods instead.