在SpecRun / SpecFlow测试执行报告中插入屏幕截图

我正在使用SpeclenSelenium WebDriverSpecRun作为测试运行器来创建和执行自动化测试用例,我正在寻找一种在测试执行报告中插入屏幕截图的解决方案。

我写了一个方法来在每个Assert函数之后创建截图。 图像保存到特定位置,但是当我进行结果分析时,我必须遵循报告和图像。 将它们放在同一位置(恰好在报告html中)会很不错。

有没有办法执行此操作(类似于控制台输出)?

(转发自https://groups.google.com/forum/#!topic/specrun/8-G0TgOBUbY )

是的,这是可能的。 您必须执行以下步骤:

  1. 将屏幕截图保存到输出文件夹(这是运行测试的当前工作文件夹)。
  2. 从测试中向控制台写出一个文件行:Console.WriteLine(“file:/// C:\ fullpath-of-the-file.png”);
  3. 确保生成的图像也作为工件保存在构建服务器上

在生成报告期间,SpecRun会扫描测试输出以查找此类文件URL,并将它们转换为具有相对路径的锚标记,以便在分发报告文件的任何位置(只要图像位于其旁边)都可以使用这些文件。 您当然可以将图像添加到子文件夹中。

这是一个与Selenium WebDriver一起使用的代码片段。 这也将HTML源与屏幕截图一起保存。

  [AfterScenario] public void AfterWebTest() { if (ScenarioContext.Current.TestError != null) { TakeScreenshot(cachedWebDriver); } } private void TakeScreenshot(IWebDriver driver) { try { string fileNameBase = string.Format("error_{0}_{1}_{2}"https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, FeatureContext.Current.FeatureInfo.Title.ToIdentifier()https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, ScenarioContext.Current.ScenarioInfo.Title.ToIdentifier()https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, DateTime.Now.ToString("yyyyMMdd_HHmmss")); var artifactDirectory = Path.Combine(Directory.GetCurrentDirectory()https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, "testresults"); if (!Directory.Exists(artifactDirectory)) Directory.CreateDirectory(artifactDirectory); string pageSource = driver.PageSource; string sourceFilePath = Path.Combine(artifactDirectoryhttps://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, fileNameBase + "_source.html"); File.WriteAllText(sourceFilePathhttps://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, pageSourcehttps://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, Encoding.UTF8); Console.WriteLine("Page source: {0}"https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, new Uri(sourceFilePath)); ITakesScreenshot takesScreenshot = driver as ITakesScreenshot; if (takesScreenshot != null) { var screenshot = takesScreenshot.GetScreenshot(); string screenshotFilePath = Path.Combine(artifactDirectoryhttps://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, fileNameBase + "_screenshot.png"); screenshot.SaveAsFile(screenshotFilePathhttps://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, ImageFormat.Png); Console.WriteLine("Screenshot: {0}"https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, new Uri(screenshotFilePath)); } } catch(Exception ex) { Console.WriteLine("Error while taking screenshot: {0}"https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, ex); } } 

尽管这篇文章已有3年历史,但它确实帮助了我,我发现如何将图像直接嵌入到HTML报告中,而不仅仅是显示为链接,从而无需从报告中逐个打开图像。

我认为这对其他人也有用。 这是通过更新默认报告模板来完成的。 请参阅教程:-Customising-Reports中的 “包括屏幕截图”部分。 我跟着一样,但我遇到了一些问题。 我通过将报告模板中的行修复为

@Raw(FormatTechMessages(traceEvent.TechMessages.TrimEnd()).Replace("SCREENSHOT[  ]SCREENSHOT"https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, ""))

我已将图像的宽度更新为100%,因为这将适合单元格中的图像。

希望完全有帮助。

我今天遇到了完全相同的问题

使用带有“[”符号的Screenshot方法另外破坏了模板我必须分别使用’和’来模板工作,而在我的情况下,最终的模板代码看起来如下:

 
@Raw(FormatTechMessages(traceEvent.TechMessages.TrimEnd()).Replace("SCREENSHOTXX"https://stackoverflow.com/questions/18512918/insert-screenshots-in-specrun-specflow-test-execution-reports/, ""))

和课堂上的代码:

 Console.WriteLine($"SCREENSHOTXX {tempFileName} XXSCREENSHOT");