簡體   English   中英

Mstest C#Selenium的HTML截圖記者

[英]HTML screenshot reporter for Mstest C# Selenium

我正在使用Selenium + C# + MsTest框架來測試HTML5應用程序。

我正在尋找一個好的報告格式。 像Protractor中的html-screenshot-reporter之類的東西。

沒有任何現成的插件可供使用。 關於如何使用Selenium + C# + MsTest實現這一建議的任何建議。

我希望問題很明確! 如果需要進一步澄清以使問題可以理解,請告訴我!

問候,
Sakshi

它遠非完美(因為你必須在每個測試類中引用它並且文件並沒有真正附加)但這就是我所做的。

  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工具歸檔中使用它,那么它應該很簡單。

此屏幕捕獲類( 從這里得到它 )非常簡單,並且依賴於Windows.Forms dll,我使用它,因為它很容易獲得整個多屏幕桌面鏡頭。 這是另一個如何做到的例子,即每個窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM