【问题标题】:HTML screenshot reporter for Mstest C# SeleniumMstest C# Selenium 的 HTML 屏幕截图报告器
【发布时间】:2015-07-19 07:33:03
【问题描述】:

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

我正在寻找一种好的报告格式。类似于 Protractor 中的html-screenshot-reporter

没有任何现成的插件可用。关于如何使用Selenium + C# + MsTest 实现此功能的任何建议。

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

问候,
飒飒

【问题讨论】:

  • 你看过this 吗?这似乎是一篇很有帮助的文章。
  • @TomNijs:感谢分享链接。但是,我正在寻找一名记者。我的测试已经对失败进行了截图(根据我的要求)

标签: c# selenium protractor mstest


【解决方案1】:

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

  1. 为所有测试创建了一个基类来处理全局清理逻辑
  2. 添加了公共 TestContext 属性
  3. 已实现 [TestCleanup] 方法
  4. 比较测试结果并保存截图
  5. 使用 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 即每个窗口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多