Sakshi Singla
Sakshi Singla

Reputation: 2502

HTML screenshot reporter for Mstest C# Selenium

I am using Selenium + C# + MsTest framework for Testing HTML5 application.

I am looking for a good reporting format. Something like html-screenshot-reporter in Protractor.

There isn't any readymade plug-in available for the same. Any suggestions on how can this be implemented using Selenium + C# + MsTest.

I hope the question is clear! Please let me know if any more clarification is needed to make the question understandable!

Regards,
Sakshi

Upvotes: 4

Views: 1186

Answers (1)

Almund
Almund

Reputation: 6226

It's far from perfect (as you would have to reference it per test class and the file doesn't really get attached) but this is what I did.

  1. Created a base class for all tests to handle global cleanup logic
  2. Added a public TestContext property
  3. Implemented [TestCleanup] method
  4. Compared test result and saved screenshot
  5. Used the AddResultFile to add the file path to the test report

Test class

[TestClass]
public class FailingTest : TestsBase
{
    [TestMethod]
    public void Failing()
    {
        throw new Exception("Fail");
    }
}

Base class

[TestClass]
public abstract class TestsBase
{
    public TestContext TestContext { get; set; }

    [TestCleanup]
    public void SaveScreenshotOnFailure()
    {
        if (TestContext.CurrentTestOutcome == UnitTestOutcome.Passed)
            return;

        var filename = Path.GetRandomFileName() + ".png";
        using (var screenshot = ScreenCapture.CaptureDesktop())
            screenshot.Save(filename, ImageFormat.Png);

        TestContext.AddResultFile(filename);
    }
}

Screenshot class

public class ScreenCapture
{
    public static Image CaptureDesktop()
    {
        // Determine the size of the "virtual screen", which includes all monitors.
        var screenLeft = SystemInformation.VirtualScreen.Left;
        var screenTop = SystemInformation.VirtualScreen.Top;
        var screenWidth = SystemInformation.VirtualScreen.Width;
        var screenHeight = SystemInformation.VirtualScreen.Height;

        // Create a bitmap of the appropriate size to receive the screenshot.
        var screenshot = new Bitmap(screenWidth, screenHeight);

        // Draw the screenshot into our bitmap.
        using (Graphics g = Graphics.FromImage(screenshot))
            g.CopyFromScreen(screenLeft, screenTop, 0, 0, screenshot.Size);

        return screenshot;
    }
}

Drawbacks are that you might not want to inherit a single base class for all tests and that the actual file doesn't seem to be attached in the report, only the path. If you're using this in a CI tool archiving it should be simple enough though.

Also this screen capture class (got it from here) is very simplistic and has a dependency on the Windows.Forms dll, I used it because it was simple to get the entire multi screen desktop shot. Here's another example of how to do it i.e. per window.

Upvotes: 2

Related Questions