0
votes

I'm working on UI automation tests using selenium c# + allurecore for reports generating. Im running them on a remote server.

My current setup for taking screenshots whenever the test fails is:

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

My screenshots are saved & I can see the steps where the test fails but whenever I generate Allure report I cannot see the attachments.

Maybe somebody could help me what syntax should be used to attach the screenshot to the report?

1

1 Answers

3
votes

Its just one line missing:

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

My csproj nuggets are:

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

And the Nunit test 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://stackoverflow.com");
    }

    [Test(Description = "This test will fail for the screenshot")]
    [AllureTag("Quick test")]
    [AllureSuite("Suite name")]
    public void FailedTest()
    {
        driver.Navigate().GoToUrl("https://stackoverflow.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();
    }
}
}

enter image description here