栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

(从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}",    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);        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号