2011-09-05 83 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; 
      } 
相关问题