From 89dd791904d84c8a15134a29ffc41376a099c7a1 Mon Sep 17 00:00:00 2001 From: Bret Johnson Date: Tue, 21 Nov 2023 10:37:51 -0500 Subject: [PATCH] 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). --- src/TestUtils/src/UITest.NUnit/UITestBase.cs | 28 +++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/TestUtils/src/UITest.NUnit/UITestBase.cs b/src/TestUtils/src/UITest.NUnit/UITestBase.cs index 568bfe31b..edc1df4e0 100644 --- a/src/TestUtils/src/UITest.NUnit/UITestBase.cs +++ b/src/TestUtils/src/UITest.NUnit/UITestBase.cs @@ -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; + } } } }