我正在使用 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
see https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs
您可以通过 Context-Injection 获取实际的 ScenarioContext (http://specflow.org/documentation/Context-Injection/ http://specflow.org/documentation/Context-Injection/) 或通过 ScenarioContext.Current (http://specflow.org/documentation/ScenarioContext/ http://specflow.org/documentation/ScenarioContext/).
如果可能的话,通过上下文注入获取它。这样,如果您想并行运行测试,将来就不会出现问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)