2011-09-05 110 views
5

我使用硒並使用mstest來驅動它。我的問題是我想讓我的整個套件針對3種不同的瀏覽器(IE,Firefox和Chrome)運行。使用MSTEST針對多個瀏覽器運行硒

我無法弄清楚的是如何在套件級別上驅動我的測試數據,或者如何使用不同的paramateres重新運行套件。

我知道我可以添加一個數據源到我的所有測試,並有單獨的測試對多個瀏覽器運行,但然後我將不得不復制兩行數據源爲每一個測試,我不認爲是非常好的解。

因此,任何人都知道我可以如何驅動我的程序集初始化?或者如果有另一個解決方案。

回答

0

這就是我所做的。這種方法的好處是它可以用於任何測試框架(mstest,nunit等),並且測試本身不需要關心或瞭解瀏覽器。您只需確保方法名稱只出現在繼承層次中一次。我已經使用這種方法進行了數百次測試,並且適用於我。

  1. 是否所有測試都從基本測試類(例如BaseTest)繼承。
  2. BaseTest將數組中的所有驅動程序對象(IE,FireFox,Chrome)保存在一個數組中(下例中是multiDriverList)。
  3. 有以下幾種方法在BaseTest:

    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 => ""); 
    } 
    
  4. 使用方法如下:

    [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

要做到這一點,我們寫了周圍的webdriver的包裝,我們使用基於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; 
      } 
相關問題