2017-02-22 5 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 

あなたはコンテキスト注射を介して実際のScenarioContextを取得する(http://specflow.org/documentation/Context-Injection/)またはScenarioContext.Current(http://specflow.org/documentation/ScenarioContext/)を経由してすることができますhttps://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs

を参照してください。
可能であれば、Context-Injectionで入手してください。そうすれば、テストを並行して実行したい場合、将来の問題は発生しません。

関連する問題