asc*_*ain 2 c# automated-tests xunit specflow
我正在使用 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
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在 ScenarioContext 中获取该场景的标签列表。
ScenarioContext.ScenarioInfo.Tags
Run Code Online (Sandbox Code Playgroud)
见https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs
您可以通过 Context-Injection ( http://specflow.org/documentation/Context-Injection/ ) 或通过 ScenarioContext.Current ( http://specflow.org/documentation/ScenarioContext/ )获取实际的 ScenarioContext 。
如果可能的话,通过 Context-Injection 获得它。这样,如果您想并行运行测试,将来就不会遇到问题。