Replies: 3 comments 4 replies
-
There was #499 feature added in Atata v1.11.0. That feature attaches all test artifact files, together with screenshots, to NUnitContext, basically after a test is finished (passed/failed). If you use SpecFlow with NUnit, But in case you don't use NUnit with SpecFlow, here is the solution: using System.IO;
using Atata;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Infrastructure;
namespace AtataSamples.SpecFlow
{
[Binding]
public sealed class SpecFlowHooks
{
private readonly ISpecFlowOutputHelper _outputHelper;
public SpecFlowHooks(ISpecFlowOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}
[BeforeTestRun]
public static void SetUpTestRun()
{
AtataContext.GlobalConfiguration
.UseChrome()
.WithArguments("start-maximized")
.UseBaseUrl("https://demo.atata.io/")
.UseCulture("en-US")
.AddScreenshotFileSaving()
.WithArtifactsFolderPath();
AtataContext.GlobalConfiguration.AutoSetUpDriverToUse();
}
[BeforeScenario]
public void SetUpScenario()
{
AtataContext.Configure().Build();
}
[AfterScenario]
public void TearDownScenario()
{
if (AtataContext.Current != null)
{
AddFileAttachments();
AtataContext.Current.CleanUp();
}
}
private void AddFileAttachments()
{
DirectoryInfo directory = AtataContext.Current.Artifacts;
if (directory.Exists)
{
var files = directory.EnumerateFiles("*", SearchOption.AllDirectories);
// NOTE: You can also use "*.png" instead of "*" to add only image files.
foreach (var file in files)
_outputHelper.AddAttachment(file.FullName);
}
}
}
} Please let me know if such solution will not work for you. |
Beta Was this translation helpful? Give feedback.
-
I got it, @shack05. I will try to add "screenshot file saved" event functionality in next Atata v1.14. Until then, if you want, you can try to use a kind of dirty workaround provided below that I was able to implement. It doesn't look nice but seems to do what you need. You can inherit using System;
using System.IO;
using System.Threading;
using Atata;
namespace AtataSamples.SpecFlow
{
public class NotifiableFileScreenshotConsumer : FileScreenshotConsumer, IScreenshotConsumer
{
private static ThreadLocal<string> currentFilePath = new ThreadLocal<string>();
private readonly Action<ScreenshotInfo, string> _screenshotTakenAction;
public NotifiableFileScreenshotConsumer(Action<ScreenshotInfo, string> screenshotTakenAction)
{
_screenshotTakenAction = screenshotTakenAction;
}
void IScreenshotConsumer.Take(ScreenshotInfo screenshotInfo)
{
Take(screenshotInfo);
string filePath = currentFilePath.Value;
filePath = filePath.SanitizeForPath();
filePath += ImageFormat.GetExtension();
if (!Path.IsPathRooted(filePath))
filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath);
_screenshotTakenAction.Invoke(screenshotInfo, filePath);
}
protected override string BuildFilePath(ScreenshotInfo screenshotInfo)
{
return currentFilePath.Value = base.BuildFilePath(screenshotInfo);
}
}
} Then use it in [BeforeScenario]
public void SetUpScenario()
{
AtataContext.Configure()
.AddScreenshotConsumer<FileScreenshotConsumer>(new NotifiableFileScreenshotConsumer(OnScrenshotTaken))
.WithArtifactsFolderPath()
.Build();
}
private void OnScrenshotTaken(ScreenshotInfo screenshot, string filePath)
{
_outputHelper.AddAttachment(filePath); // Assuming there's a field: ISpecFlowOutputHelper _outputHelper;
} |
Beta Was this translation helpful? Give feedback.
-
Hey @shack05. Now you can use "global events" feature of Atata v1.14.0. You can subscribe to AtataContext.Configure()
.EventSubscriptions.Add<ScreenshotFileSavedEvent>(eventData => _outputHelper.AddAttachment(eventData.FilePath)) Here is a full |
Beta Was this translation helpful? Give feedback.
-
Hello,
When using SpecFlow, I'd like to use the new ISpecFlowOutputHelper api to attach screenshots to the test execution logs (documentation), however I'm not sure how best to determine the path of the file.
Perhaps Atata could allow users to configure an action (such as
Action<ScreenshotInfo, string>
, wherestring
is the file path of the screenshot) that is invoked inFileScreenshotConsumerBase.Take
after the screenshot is saved to disk?If there is already a way for Atata to handle this use case please let me know :)
Thanks
Beta Was this translation helpful? Give feedback.
All reactions