我使用硒并使用mstest来驱动它。我的问题是我希望我的整个套件能够针对3种不同的浏览器(IE,Firefox和Chrome)运行。
我无法弄清楚的是如何在套件级别上驱动我的测试数据或如何以不同的paramateres重新运行套件。
我知道我可以为我的所有测试添加一个数据源,并针对多个浏览器运行单独的测试但是我必须为每个测试重复数据源的2行,我认为这不是很好的解决方案
所以有人知道我的数据如何驱动程序集初始化?或者是否有另一种解决方案。
答案 0 :(得分:0)
这就是我所做的。这种方法的好处是它适用于任何测试框架(mstest,nunit等),测试本身并不需要关注或了解任何有关浏览器的知识。您只需要确保方法名称仅在继承层次结构中出现一次。我已经将这种方法用于数百次测试,它对我有用。
在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 => "" );
}
使用方法如下:
[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
...
}
答案 1 :(得分: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;
}
答案 2 :(得分:0)
对于自动CI场景,此想法比交互式UI更好,但是您可以使用runsettings文件并在其中声明参数:
<?xml version='1.0' encoding='utf-8'?>
<RunSettings>
<TestRunParameters>
<Parameter name="SELENIUM_BROWSER" value="Firefox" />
</TestRunParameters>
</RunSettings>
您在Test类上需要一个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次,对于每个运行设置文件一次。