Fix "Test attachment file path could not be found" (#18911)

Properrly account for App.Screenshot appending a ".png"
extension (always) to the passed in file path, fixing so
that the attachment isn't added twice.

This fixes the "Test attachment file path could not be found"
error that could show for UITest when they fail and SaveDiagnosticLogs
tries to attach screenshots (caused because the actually file
had a ".png.png" extension, so the file with just a ".png"
extension wasn't found).
This commit is contained in:
Bret Johnson 2023-11-21 10:37:51 -05:00 коммит произвёл GitHub
Родитель 955e0a3b19
Коммит 89dd791904
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 25 добавлений и 3 удалений

Просмотреть файл

@ -118,13 +118,35 @@ namespace UITest.Appium.NUnit
{
string name = TestContext.CurrentContext.Test.MethodName ?? TestContext.CurrentContext.Test.Name;
var screenshotPath = Path.Combine(logDir, $"{name}-{_testDevice}{note}ScreenShot.png");
var screenshotPath = Path.Combine(logDir, $"{name}-{_testDevice}{note}ScreenShot");
_ = App.Screenshot(screenshotPath);
TestContext.AddTestAttachment(screenshotPath, Path.GetFileName(screenshotPath));
// App.Screenshot appends a ".png" extension always, so include that here
var screenshotPathWithExtension = screenshotPath + ".png";
AddTestAttachment(screenshotPathWithExtension, Path.GetFileName(screenshotPathWithExtension));
var pageSourcePath = Path.Combine(logDir, $"{name}-{_testDevice}{note}PageSource.txt");
File.WriteAllText(pageSourcePath, App.ElementTree);
TestContext.AddTestAttachment(pageSourcePath, Path.GetFileName(pageSourcePath));
AddTestAttachment(pageSourcePath, Path.GetFileName(pageSourcePath));
}
}
void AddTestAttachment(string filePath, string? description = null)
{
try
{
TestContext.AddTestAttachment(filePath, description);
}
catch (FileNotFoundException e)
{
// Add the file path to better troubleshoot when these errors occur
if (e.Message == "Test attachment file path could not be found.")
{
throw new FileNotFoundException($"{e.Message}: {filePath}");
}
else
{
throw;
}
}
}
}