Mstest C#Selenium的HTML截图记者

我正在使用Selenium C#MsTest框架来测试
HTML5应用程序.

我正在寻找一个好的报告格式.像量角器中的html-screenshot-reporter.

没有任何现成的插件可供使用.有关如何使用Selenium C#MsTest实现这一建议的任何建议.

我希望问题很明确!如果需要进一步澄清以使问题可以理解,请告诉我!

问候,
Sakshi

最佳答案 它远非完美(因为你必须在每个测试类中引用它并且文件并没有真正附加)但这就是我所做的.

>为所有测试创建了一个基类来处理全局清理逻辑
>添加了一个公共TestContext属性
>实现[TestCleanup]方法
>比较测试结果并保存截图
>使用AddResultFile将文件路径添加到测试报告中

考试班

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

基类

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

截图类

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

缺点是您可能不希望为所有测试继承单个基类,并且实际文件似乎没有附加在报表中,只包含路径.如果您在CI工具归档中使用它,那么它应该很简单.

此屏幕捕获类(got it from here)非常简单,并且依赖于Windows.Forms dll,我使用它,因为它很容易获得整个多屏幕桌面镜头. Here’s another example of how to do它即每个窗口.

点赞