2017-02-22 38 views
0

我正在運行一個使用c#,selenium和specflow的自動化測試套件。如果可能的話,我希望能夠看到爲當前場景分配了哪些標記,以便我可以爲每個場景實例化某種瀏覽器類型。這甚至可以使用XUnit?在WebDriver方法中獲取Specflow標記

登錄功能文件:

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(); 
       } 
      } 
     } 
    } 
} 

回答

2

你可以在ScenarioContext場景的標籤列表。

ScenarioContext.ScenarioInfo.Tags 

看到https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs

你可以通過上下文注入(http://specflow.org/documentation/Context-Injection/)實際ScenarioContext或通過ScenarioContext.Current(http://specflow.org/documentation/ScenarioContext/)。
如果可能通過上下文注入獲取它。這樣,如果你想並行運行測試,你將不會遇到未來的問題。