gpt4 book ai didi

c# - 在 WebDriver 方法中获取 Specflow 标签

转载 作者:行者123 更新时间:2023-12-01 22:18:12 25 4
gpt4 key购买 nike

我正在使用 c#、selenium 和 specflow 运行自动化测试套件。如果可能的话,我希望能够看到为当前场景分配了哪些标签,以便我可以为每个场景实例化特定的浏览器类型。这甚至可以使用 XUnit 吗??

登录特征文件:

Feature: Login
In order to login to DRIVE
As a user
We have to enter login details

Background:
Given I am on the login page

@headless
Scenario: Logging in as a valid user
And I enter a valid user and password
When I submit the login form
Then The user should be logged in

WebDriverContext.cs文件

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;

namespace Drive.Acceptance.Tests
{
public interface IWebDriverContext {
IWebDriver GetDriver();
}

public class WebDriverContext : IWebDriverContext
{
private static volatile WebDriverContext _instance;
private static readonly object Lock = new object();

public static IWebDriverContext Instance
{
get
{
if (_instance == null)
{
lock (Lock)
{
if (_instance == null)
_instance = new WebDriverContext();
}
}

return _instance;
}

}

public IWebDriver GetDriver()
{
lock (Lock)
{
// TODO: create headless browser if scenario is tagged with @headless
if (!TagName.Contains("headless")) {
return new ChromeDriver();
}
else {
return new PhantomJSDriver();
}
}
}
}
}

最佳答案

在ScenarioContext中可以获取到Scenario的标签列表。

ScenarioContext.ScenarioInfo.Tags

参见 https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs

您可以通过上下文注入(inject)(http://specflow.org/documentation/Context-Injection/)或通过 ScenarioContext.Current(http://specflow.org/documentation/ScenarioContext/)获取实际的 ScenarioContext。
如果可能的话,通过 ContextInjection 获取它。这样,如果您想并行运行测试,您以后就不会遇到问题。

关于c# - 在 WebDriver 方法中获取 Specflow 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42400839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com