我有40个Webdriver测试分为两组 - 一组必须连续运行(其中每个都有元数据组标签“crm_import”),另一组可以并行运行。这两个组由以下XML文件引用:
TestNG.xml
<suite name = "Suite" thread-count="2" parallel="methods">
<test name = "TestSet">
<groups>
<run>
<exclude name = "crm_import"/>
</run>
</groups>
<classes>
<class name = "com.dcp.test.suite.TestSuite"/>
</classes>
</test>
</suite>
TestNG_includeOnly.xml
<suite name = "Suite2" verbose="1" >
<test name = "TestSet">
<groups>
<run>
<include name = "crm_import"/>
</run>
</groups>
<classes>
<class name = "com.dcp.test.suite.TestSuite"/>
</classes>
</test>
</suite>
这两个在POM文件中引用如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
<suiteXmlFile>testng-includeOnly.xml</suiteXmlFile>
</suiteXmlFiles>
...
</configuration>
因为当我让方案工作时我需要在Jenkins中运行它们,我使用命令行版本来运行它们,使用以下命令尝试运行并行集:
mvn clean test -DsuiteXmlFile=testng.xml site
出现两个问题......
如果我转到POM文件并注释掉一个套件或另一个套件并运行...
mvn clean test site
...然后一切都很好 - 没有窗户打开,并行测试并行运行。
如果您想知道我如何打开和关闭浏览器,这里是TestSuite.java文件扩展的基类的骨架......
public class TestSuiteBase implements IInvokedMethodListener {
...
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod()) {
System.setProperty("webdriver.firefox.marionette", "/opt/geckodriver");
ProfilesIni profile = new ProfilesIni();
String ffProfileName = properties.getProperty("ffProfileName");
FirefoxProfile myprofile = profile.getProfile(ffProfileName);
System.out.println("--> creating WebDriver instance..." + "Thread id = " + Thread.currentThread().getId());
WebDriver driver = new FirefoxDriver(myprofile);
LocalDriverManager.setWebDriver(driver);
}
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod()) {
WebDriver driver = LocalDriverManager.getDriver();
if (driver != null) {
System.out.println("After Test breakdown, quitting driver..." + "Thread id = " + Thread.currentThread().getId());
driver.quit();
}
}
}
如果你对我在哪里获得一些架构感到好奇,我从这里借了它...... https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/
答案 0 :(得分:0)
您分享的博客是我的博客:)
如果您要考虑传递多个TestNG套件,那么您应该查看我的this博客文章。这基本上应该照顾你的问题#1。
对于您的问题#2,即您打开了多个浏览器窗口,您可以在afterInvocation
方法中执行以下操作:
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod()) {
WebDriver driver = LocalDriverManager.getDriver();
if (driver != null) {
System.out.println("After Test breakdown, quitting driver..." + "Thread id = " + Thread.currentThread().getId());
driver.quit();
}
LocalDriverManager.setWebDriver(null);
}
}
这里我们明确地取消了当前线程的webdriver上下文。
我不太确定它是否真的能解决这个问题,但你可以尝试一下,看看是否有帮助。