tfs - 在 Nunit BDD 规范流中截取屏幕截图并附加到 DevOps 测试结果
问题描述
我正在使用 NUnit.net 核心 BDD 规范流程我需要帮助来捕获失败场景步骤的屏幕截图并附加到 DevOps 中的 TFS 结果报告。我有下面的代码。
public bool loginpageelementpresent()
{
try
{
return loginpageelement.Displayed;
}
catch(Exception e)
{
var filePath = $"{TestContext.CurrentContext.TestDirectory}\\{TestContext.CurrentContext.Test.MethodName}.jpg";
((ITakeScreenshot)_driver).GetScreenshot().SaveAsFile(filePath);
TestContext.AddTestAttachment(filePath);
return false;
}
}
使用此代码,我可以在 DevOps 中看到屏幕截图,但我想使用此代码适用于所有失败的场景步骤,有人可以向我解释如何使其更具动态性,因此如果任何场景步骤失败,它会自动执行截图并附在 DevOps 测试结果中
解决方案
您可能可以在after 场景挂钩中完成此操作:
[Binding]
public class SpecFlowHooks
{
private readonly IObjectContainer container;
private readonly ScenarioContext scenario;
public TestContext TestContext { get; set; }
private bool IsFailingScenario => scenario.TestError != null;
public SpecFlowHooks(IObjectContainer container, ScenarioContext scenario)
{
this.container = container;
this.scenario = scenario;
}
[BeforeScenario]
public void CreateWebDriver()
{
// Create and configure the Selenium IWebDriver object
var driver = new ChromeDriver() or FirefoxDriver() or EdgeDriver() or whatever you normally do
// Make the web driver available to all step definitions, including this
// hooks classes. Additionally flag this object as something BoDi should
// dispose of at the end of each test.
container.RegisterInstanceAs<IWebDriver>(driver, null, true);
}
[AfterScenario]
public void RecordTestFailure()
{
if (IsFailingScenario)
{
var driver = container.Resolve<IWebDriver>();
var photographer = (ITakeScreenshot)driver;
var filePath = $"{TestContext.CurrentContext.TestDirectory}\\{TestContext.CurrentContext.Test.MethodName}.jpg";
photographer.GetScreenshot()
.SaveAsFile(filePath);
TestContext.AddTestAttachment(filePath);
}
}
}
假设在测试上下文中添加附件确实会将屏幕截图添加到 DevOps 中的测试结果中。顺便说一句,这也是了解 SpecFlow 中的依赖注入以及如何在 SpecFlow 测试中正确初始化和管理 IWebDriver 对象的好机会。
我回答了另一个问题,它为您提供了初始化 Web 驱动程序的更完整图片,然后在您的步骤定义和 Selenium 页面模型中使用它:https ://stackoverflow.com/a/56427437/3092298
推荐阅读
- python - 使用 128 的 Batch 大小为 Pytorch-LSTM 提供时间序列输入
- java - Prime Factorization Java Array
- go - 为什么 goroutine 不会两次处理同一个文件?
- python - 无法使用 pandas 正确读取文件夹文件
- function - Why do I need to put any random string as a function parameter to make my function work?
- sql - 尝试在 case 语句中执行下一个多个选择查询时出现语法错误
- php - 我无法检查列中是否存在该值
- excel - 设置范围不适用于内部的 Cells()
- vue.js - 如何使用 vue-cli 3 配置环境变量?
- ios - 在 Swift 中使用按钮启动和停止动画