2011-09-05 9 views
5

セレンを使用し、mstestを使用してドライブしています。私の問題は、全スイートが3つの異なるブラウザ(IE、Firefox、およびクロム)に対して実行されるようにすることです。MSTESTを使用して複数のブラウザに対してセレンを実行する

私が理解できないことは、データをスイートレベルでテストする方法、または異なるパラメタでスイートを再実行する方法です。

私はすべてのテストにデータソースを追加し、複数のブラウザに対して個別のテストを実行することができますが、その後、私は非常に良いとは思わない1回のテストごとに2行のデータソースを複製する必要があります溶液。

誰かが私のアセンブリの初期化をどのようにデータ駆動できるか知っていますか?または別の解決方法がある場合

答えて

0

これは私がやったことです。このアプローチの利点は、どのテストフレームワーク(mstest、nunitなど)でも動作することであり、テスト自体はブラウザに関して何か気にする必要も、知る必要もありません。メソッド名は、継承階層で一度しか発生しないようにしてください。私はこのアプローチを何百ものテストに使用してきました。

  1. すべてのテストをベーステストクラス(BaseTestなど)から継承しましたか?
  2. BaseTestは、すべてのドライバオブジェクト(IE、FireFox、Chrome)を配列(以下の例ではmultiDriverList)に保持します。次のように

    public void RunBrowserTest([CallerMemberName] string methodName = null) 
    {    
        foreach(IDriverWrapper driverWrapper in multiDriverList) //list of browser drivers - Firefox, Chrome, etc. You will need to implement this. 
        { 
         var testMethods = GetAllPrivateMethods(this.GetType()); 
         MethodInfo dynMethod = testMethods.Where(
           tm => (FormatReflectionName(tm.Name) == methodName) && 
            (FormatReflectionName(tm.DeclaringType.Name) == declaringType) && 
            (tm.GetParameters().Where(pm => pm.GetType() == typeof(IWebDriver)) != null)).Single(); 
         //runs the private method that has the same name, but taking a single IWebDriver argument 
         dynMethod.Invoke(this, new object[] { driverWrapper.WebDriver }); 
        } 
    } 
    
    //helper method to get all private methods in hierarchy, used in above method 
    private MethodInfo[] GetAllPrivateMethods(Type t) 
    { 
        var testMethods = t.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance); 
        if(t.BaseType != null) 
        { 
         var baseTestMethods = GetAllPrivateMethods(t.BaseType); 
         testMethods = testMethods.Concat(baseTestMethods).ToArray(); 
        } 
        return testMethods; 
    } 
    
    //Remove formatting from Generic methods 
    string FormatReflectionName(string nameIn) 
    {    
        return Regex.Replace(nameIn, "(`.+)", match => ""); 
    } 
    
  3. 使用を::

  4. はBaseTestに以下のメソッドを持ってこれを行うために、我々はwebdriverをラッパーを書いた

    [TestMethod] 
    public void RunSomeKindOfTest() 
    { 
        RunBrowserTest(); //calls method in step 3 above in the base class 
    } 
    private void RunSomeKindOfTest(IWebDriver driver) 
    { 
        //The test. This will be called for each browser passing in the appropriate driver in each case 
        ...    
    }  
    
0

、我々はベースのswitchステートメントを使用しますブラウザの種類を選択するためのプロパティで

ここにスニペットがあります。 DesiredCapabilitiesを使用すると、実行するブラウザをグリッドに指示できます。

switch (Controller.Instance.Browser) 
      { 
       case BrowserType.Explorer: 
        var capabilities = DesiredCapabilities.InternetExplorer(); 
        capabilities.SetCapability("ignoreProtectedModeSettings", true); 
        Driver = new ScreenShotRemoteWebDriver(new Uri(uri), capabilities, _commandTimeout); 
        break; 
       case BrowserType.Chrome: 
        Driver = new ScreenShotRemoteWebDriver(new Uri(uri), DesiredCapabilities.Chrome(), _commandTimeout); 
        break; 
      } 
関連する問題