Skip to content

Commit

Permalink
Add screenshot to testresult for XML report (#535)
Browse files Browse the repository at this point in the history
* Add screenshot to testresult for XML report

[L143-L145] I have added the AddTestAttachment method in order to include taken screenshot to the report and, therefore, the screenshot will be available in XML report, generated at the end of execution. This is interesting when parsing XML to generate, for example, an HTML report.

[L278-L286] I have extracted from TakeScreenShot the generation of the full path so that I can use it in previously explained code.

* Update FlaUITestBase.cs

Requested changes by @tomersalem in #535 (review)

* Sanitizing folder path
  • Loading branch information
igorrecioh committed Oct 30, 2023
1 parent 9e58990 commit bf949b6
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/FlaUI.TestUtilities/FlaUITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@ public abstract class FlaUITestBase
/// </summary>
protected virtual VideoRecordingMode VideoRecordingMode => VideoRecordingMode.OnePerTest;

/// <summary>
/// static member which holds the current execution date and time
/// </summary>
private static string _testDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");

/// <summary>
/// Path of the directory for the screenshots and videos for the tests.
/// Defaults to c:\temp\testsmedia.
/// </summary>
protected virtual string TestsMediaPath => @"c:\temp\testsmedia";
protected virtual string TestsMediaPath => $@"c:\temp\testsmedia\{SanitizeFileName(TestContext.CurrentContext.Test.Name)}\{_testDateTime}";

/// <summary>
/// Gets the automation instance that should be used.
Expand Down Expand Up @@ -140,6 +145,9 @@ public virtual void UITestBaseTearDown()
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
{
TakeScreenShot(TestContext.CurrentContext.Test.FullName);
TestContext.AddTestAttachment(
CreateScreenShotPath(TestContext.CurrentContext.Test.FullName),
TestContext.CurrentContext.Test.FullName);
}

if (ApplicationStartMode == ApplicationStartMode.OncePerTest)
Expand Down Expand Up @@ -251,9 +259,7 @@ private void StopVideoRecorder()
/// </summary>
private void TakeScreenShot(string testName)
{
var imageName = SanitizeFileName(testName) + ".png";
imageName = imageName.Replace("\"", String.Empty);
var imagePath = Path.Combine(TestsMediaPath, imageName);
var imagePath = CreateScreenShotPath(testName);
try
{
Directory.CreateDirectory(TestsMediaPath);
Expand All @@ -273,5 +279,15 @@ private string SanitizeFileName(string fileName)
fileName = string.Join("_", fileName.Split(Path.GetInvalidFileNameChars()));
return fileName;
}

/// <summary>
/// Generates full path for screenshot.
/// </summary>
private string CreateScreenShotPath(string testName)
{
var imageName = SanitizeFileName(testName) + ".png";
imageName = imageName.Replace("\"", String.Empty);
return Path.Combine(TestsMediaPath, imageName);
}
}
}

0 comments on commit bf949b6

Please sign in to comment.