使用MSTEST在多个浏览器上运行Selenium测试

5
我正在使用Selenium和MSTest来控制它。我的问题是我想让我的整个测试套件在三种不同的浏览器(IE,Firefox和Chrome)上运行。
我无法弄清楚如何在套件级别上进行数据驱动测试或如何使用不同的参数重新运行套件。
我知道我可以为所有测试添加数据源,并使单个测试针对多个浏览器运行,但这样我将不得不为每个测试复制数据源的两行代码,我认为这不是一个很好的解决方案。
所以有人知道如何数据驱动我的程序集初始化吗?或者是否有其他解决方案。
3个回答

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;
            }

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 ) //浏览器驱动程序列表 - Firefox,Chrome等。您需要实现此功能。
        {
            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();
            //运行具有相同名称但只接受单个IWebDriver参数的私有方法
            dynMethod.Invoke( this, new object[] { driverWrapper.WebDriver } ); 
        }
    } 
    
    //获取层次结构中的所有私有方法的辅助方法,用于上述方法
    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;
    }
    
    //从通用方法中删除格式
    string FormatReflectionName( string nameIn )
    {            
        return Regex.Replace( nameIn, "(`.+)", match => "" );
    }
    
  4. 使用如下:

    [TestMethod]
    public void RunSomeKindOfTest()
    {
        RunBrowserTest(); //调用上述步骤3中的方法,位于基础类中
    }
    private void RunSomeKindOfTest( IWebDriver driver )
    {
        //测试。每个浏览器都会传递适当的驱动程序进行调用
        ...            
    }     
    

0

这个想法更适合自动化CI场景而不是交互式UI,但您可以使用runsettings文件并在其中声明一个参数:

<?xml version='1.0' encoding='utf-8'?>
<RunSettings>
    <TestRunParameters>
        <Parameter name="SELENIUM_BROWSER" value="Firefox" />
    </TestRunParameters>
</RunSettings>

你的测试类需要一个 TestContext

public TestContext TestContext { get; set; }

然后在您的 MSTest 中初始化驱动程序时,您可以检查要运行的浏览器。

switch (TestContext.Properties["SELENIUM_BROWSER"]?.ToString())
{
    case BrowserType.Chrome:
        return new ChromeDriver();
    case BrowserType.Edge:
        return new EdgeDriver();
    case BrowserType.Firefox:
        return new FirefoxDriver();
}

然后,您将运行测试套件n次,每次运行一个runsettings文件


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接