C# 在WebDriver方法中获取Specflow标记

C# 在WebDriver方法中获取Specflow标记,c#,automated-tests,specflow,xunit,C#,Automated Tests,Specflow,Xunit,我正在运行一个使用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 p

我正在运行一个使用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中获得场景的标记列表

ScenarioContext.ScenarioInfo.Tags

您可以通过上下文注入()或通过ScenarioContext.Current()获取实际的ScenarioContext。

如果可能的话,通过上下文注入获得它。这样,如果您希望并行运行测试,您将不会有未来的问题。

如果您希望在功能级别获得标记,您可以使用

FeatureContext.Current.FeatureInfo.Tags