使用MSTEST针对多个浏览器运行selenium

Mar*_*ann 5 selenium mstest webdriver

我使用硒并使用mstest来驱动它.我的问题是我希望我的整个套件能够针对3种不同的浏览器(IE,Firefox和Chrome)运行.

我无法弄清楚的是如何在套件级别上驱动我的测试数据或如何使用不同的paramateres重新运行套件.

我知道我可以为我的所有测试添加一个数据源,并针对多个浏览器运行单独的测试但是我必须为每个单独的测试重复数据源的2行,我认为这不是很好的解决方案.

所以有人知道我的数据如何驱动我的程序集初始化?或者是否有其他解决方案.

aca*_*lon 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 => "" );
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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
        ...            
    }     
    
    Run Code Online (Sandbox Code Playgroud)