目前,我的经理希望我运行一次 Specflow 场景测试用例一次,但可以生成 2 个范围报告(一个用于 Chrome 驱动程序,一个用于 Edge 驱动程序),而无需更改旧的步骤定义文件。
我的代码示例
依赖容器
[ScenarioDependencies]
public static IServiceCollection BuildDependencyContainer()
{
var services = new ServiceCollection();
AppConfig.SettingAppConfig(); // it can change Constants.PROJECT_BROWSER to Chrome or Edge from reading appsettings.json
var driver = DriverInitializer.Initialize(Constants.PROJECT_BROWSER);
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().PageLoad.Add(TimeSpan.FromSeconds(Constants.WAIT_TIMES_TIMEOUT));
services.AddScoped<IWebDriver>(provider => driver);
var projectConfig = new ProjectConfig();
services.AddSingleton<IProjectConfig>(projectConfig);
return services;
}
报告挂钩
[Binding]
public sealed class ReportHook : ExtentReport
{
private readonly IWebDriver _driver;
public ReportHook(IWebDriver driver)
{
_driver = driver;
}
// Various stuff to create extent report
}
示例功能
Feature: YoutubeSearch
Simple demo to test the project
@demo
Scenario: Search Zoetrope in youtube
Given open the browser
When enter the youtube url
Then search for Zoetrope
示例步骤定义
[Binding]
public sealed class YoutubeSearchStepDefinitions
{
private IWebDriver driver = null!;
public YoutubeSearchStepDefinitions(IWebDriver driver)
{
this.driver = driver;
}
[When(@"enter the youtube url")]
public void WhenEnterTheYoutubeUrl()
{
driver.Url = "https://www.youtube.com/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(5000);
}
[Then(@"search for Zoetrope")]
public void ThenSearchForZoetrope()
{
driver.FindElement(By.XPath("//*[@name='search_query']")).SendKeys("Zoetrope");
driver.FindElement(By.XPath("//*[@name='search_query']")).SendKeys(Keys.Enter);
Thread.Sleep(5000);
driver.Quit();
}
}
我想要什么,如果我运行测试这个场景一次,它应该创建 2 个报告。
我能想到的一种方法是使用命令行再次强制运行场景测试+在此之前编辑appsetting.json,但我认为这不是一个好主意。
命令行示例: “C:\ Program Files \ Microsoft Visual Studio�2 \ Professional \ Common7 \ IDE \ Extensions \ TestPlatform stest.console.exe”“./bin/Debug/net7.0/V5VNTraining.DemoAutoTest.dll”--TestCaseFilter:“ V5VNTraining.DemoAutoTest.Features.YoutubeSearch”
再次询问我的经理,他说我可以编辑功能文件,但仍然需要使用这个DI 但问题是,如果我使用此 libs 或示例中的标签,我无法在静态方法 BuildDependencyContainer()
中捕获浏览器变量谢谢。
您可以在功能文件中使用 SpecFlow 标签,然后使用不同的标签运行两次测试,以分离 Chrome 和 Edge 运行,从而为同一 SpecFlow 场景生成两个单独的范围报告,一份用于 Chrome 驱动程序,一份用于 Edge 驱动程序,而无需更改旧的步骤定义文件。将 Chrome 和 Edge 情况的标签添加到您的功能文件中。
Feature: YoutubeSearch
Simple demo to test the project
@chrome
Scenario: Search Zoetrope in youtube using Chrome
Given open the browser
When enter the youtube url
Then search for Zoetrope
@edge
Scenario: Search Zoetrope in youtube using Edge
Given open the browser
When enter the youtube url
Then search for Zoetrope
更改
ReportHook
绑定类中的逻辑以根据标签划分报告。您可以通过检查测试场景标签并启动适当的报告来完成此任务。要根据标签过滤和执行场景,请使用 vstest.console.exe 命令中的 --filter
选项。
对于 Chrome:
"C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" "./bin/Debug/net7.0/V5VNTraining.DemoAutoTest.dll" --TestCaseFilter:"V5VNTraining.DemoAutoTest.Features.YoutubeSearch&Chrome"
对于边缘:
"C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" "./bin/Debug/net7.0/V5VNTraining.DemoAutoTest.dll" --TestCaseFilter:"V5VNTraining.DemoAutoTest.Features.YoutubeSearch&Edge"