问题
I am running an automated test suite using c#, selenium, and specflow. If possible, I would like to be able to see what tag(s) are assigned to the current scenarios so I can instantiate a certain browser type per scenario. Is this even possible using XUnit??
Login Feature File:
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 file
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();
}
}
}
}
}
回答1:
You can get a list of tags of the Scenario in the ScenarioContext.
ScenarioContext.ScenarioInfo.Tags
see https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs
You can get the actual ScenarioContext via Context-Injection (http://specflow.org/documentation/Context-Injection/) or via ScenarioContext.Current (http://specflow.org/documentation/ScenarioContext/).
If possible get it via Context- Injection. That way, you will not have future problems if you want to run the tests in parallel.
来源:https://stackoverflow.com/questions/42400839/getting-specflow-tag-in-webdriver-method