失败测试的 AllureReport 截图附件

AllureReport screenshot attachment for failed tests

我正在使用 selenium c# + allurecore 进行 UI 自动化测试以生成报告。我 运行 他们在远程服务器上。

我当前在测试失败时截取屏幕截图的设置是:

[TearDown]
public void CloseBrowser()
{
    if (TestContext.CurrentContext.Result.Outcome != ResultState.Success)
    {
        var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
        var filename = TestContext.CurrentContext.Test.MethodName + "_screenshot_" + DateTime.Now.Ticks + ".png";
        var path = ScreenShotsFolder + filename;
        screenshot.SaveAsFile(path, ScreenshotImageFormat.Png);
        TestContext.AddTestAttachment(path);
    }
    driver.Close();
    driver.Quit();
}

我的屏幕截图已保存并且我可以看到测试失败的步骤但是每当我生成 Allure 报告时我看不到附件。

也许有人可以帮助我将屏幕截图附加到报告中应该使用什么语法?

只少了一行:

AllureLifecycle.Instance.AddAttachment(filename, "image/png", path);

我的 csproj 块是:

 <ItemGroup>
    <PackageReference Include="NUnit" Version="3.13.1" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"/>
    <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
    <PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="90.0.4430.2400" />
    <PackageReference Include="NUnit.Allure" Version="1.0.14-beta2" />
  </ItemGroup>

和 Nunit 测试 class:

using System;
using System.IO;
using Allure.Commons;
using NUnit.Allure.Attributes;
using NUnit.Allure.Core;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace Selenium
{
    [AllureNUnit]
    public class Tests
    {
        public IWebDriver driver;

    [SetUp]
    public void Setup()
    {
        driver = new ChromeDriver(".");
    }

    [Test(Description = "This test will be a success")]
    [AllureTag("Quick test")]
    [AllureSuite("Suite name")]
    public void PassedTest()
    {
        driver.Navigate().GoToUrl("https://whosebug.com");
    }

    [Test(Description = "This test will fail for the screenshot")]
    [AllureTag("Quick test")]
    [AllureSuite("Suite name")]
    public void FailedTest()
    {
        driver.Navigate().GoToUrl("https://whosebug.com");
        driver.FindElement(By.Id("doesnt_exist"));
    }

    [TearDown]
    public void CloseBrowser()
    {

        if (TestContext.CurrentContext.Result.Outcome != ResultState.Success)
        {
            var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
            var filename = TestContext.CurrentContext.Test.MethodName + "_screenshot_" + DateTime.Now.Ticks + ".png";
            var path = "D:\" + filename;
            screenshot.SaveAsFile(path, ScreenshotImageFormat.Png);
            TestContext.AddTestAttachment(path);
            AllureLifecycle.Instance.AddAttachment(filename, "image/png", path);
        }
        driver.Close();
        driver.Quit();
    }
}
}