(从https://groups.google.com/forum/#!topic/specrun/8-G0TgOBUbY转发)
是的,这是可能的。您必须执行以下步骤:
- 将屏幕快照保存到输出文件夹(这是运行测试的当前工作文件夹)。
- 从测试中向控制台写一个文件行:Console.WriteLine(“ file:/// C: fullpath-of-the-file.png”);
- 确保生成的图像也作为工件保存在构建服务器上
在生成报告的过程中,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}", FeatureContext.Current.FeatureInfo.Title.ToIdentifier(), ScenarioContext.Current.ScenarioInfo.Title.ToIdentifier(), DateTime.Now.ToString("yyyyMMdd_HHmmss")); var artifactDirectory = Path.Combine(Directory.GetCurrentDirectory(), "testresults"); if (!Directory.Exists(artifactDirectory)) Directory.CreateDirectory(artifactDirectory); string pageSource = driver.PageSource; string sourceFilePath = Path.Combine(artifactDirectory, fileNamebase + "_source.html"); File.WriteAllText(sourceFilePath, pageSource, Encoding.UTF8); Console.WriteLine("Page source: {0}", new Uri(sourceFilePath)); ITakesScreenshot takesScreenshot = driver as ITakesScreenshot; if (takesScreenshot != null) { var screenshot = takesScreenshot.GetScreenshot(); string screenshotFilePath = Path.Combine(artifactDirectory, fileNamebase + "_screenshot.png"); screenshot.SaveAsFile(screenshotFilePath, ImageFormat.Png); Console.WriteLine("Screenshot: {0}", new Uri(screenshotFilePath)); } } catch(Exception ex) { Console.WriteLine("Error while taking screenshot: {0}", ex); } }


