【发布时间】:2019-04-04 18:23:06
【问题描述】:
我正在尝试使用 Selenium 和 Cucumber 进行并行测试。所以我们有大约 130 个测试场景,大约需要 1.5 小时才能运行。现在通过并行运行所有这些测试,我可以减少时间。
我正在使用以下 2 个插件:
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>generate-test-sources</phase>
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<outputDirectory>runner</outputDirectory>
<glue>
<package>${PackageName}</package>
</glue>
<featuresDirectory>${FeaturesDirectory}</featuresDirectory>
<cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
<format>json</format>
<plugins>
<plugin>
<name>json</name>
</plugin>
</plugins>
<useTestNG>true</useTestNG>
<parallelScheme>SCENARIO</parallelScheme>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>acceptance-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<includes>
<include>**/Parallel*IT.class</include>
</includes>
<properties>
<property>
<name>junit</name>
<value>false</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
所以想法是,假设我给线程数或叉数 3,它将调用 3 个浏览器,并运行测试。使用 CUcumber JVM Parallel Plug in 我为所有场景创建了 131 个运行器。使用 Cucumber 的 @Before 注释,我可以调用一次浏览器,并且对于所有下一个实例,它将使用相同的浏览器实例(从而节省了一次又一次地打开和关闭浏览器的时间。)
现在,如果我们可以使用@BeforeClass 或@AfterClass,这将是一个更简单的解决方案。但我们不能。我不得不使用@Before,因此在每种情况下一次又一次地调用相同的方法。
import cucumber.api.java.Before;
public class stepdefs(){
static Browser browser;
public static Scenario scenario;
static TestConfiguration configuration = TestConfiguration.getConfiguration();
@Before
public void before(Scenario scenario) {
if (browser == null) {
if (System.getProperty("seleniumRemoteWebDriver") == null) {
WebTestSettings webTestSettings = configuration.getWebTestSettings();
browser = Browser.LOAD_LOCAL_BROWSER(webTestSettings.externalBrowser, webTestSettings.driverPath);
} else {
if (System.getProperty("version") == null) {
browser = new RemoteBrowser(System.getProperty("browser"), System.getProperty("seleniumRemoteWebDriver"), System.getProperty("platform"));
} else {
browser = new RemoteBrowser(System.getProperty("browser"), System.getProperty("seleniumRemoteWebDriver"), System.getProperty("platform"), System.getProperty("version"));
}
}
TestContext.getContext().setBrowser(browser);
}
Browser browser = TestContext.getContext().getBrowser();
this.scenario = scenario;
browser.driver.manage().window().maximize();
}}
所以这适用于@Before,但这不适用于@After。我在这里要实现的是在测试完成后关闭所有浏览器实例。但是我怎么知道哪个 1 是最后一个实例?或者我如何关闭所有实例?有没有办法使用 @AfterClass 或 @BeforeCLass 或将 SetupTeardown 与 JUnit/TestNG + Maven-Failsafe-plugin 一起使用?
我尝试过同时使用 Junit 和 TestNG。我曾经使用扩展至 SetupTearDown 类的 Runner 文件来进行非并行执行。但显然不能使用它,因为运行时会生成运行器类。也尝试使用 pre-integration , post-integration 阶段。哪个没有运行。可能是我没有正确使用那个阶段。
【问题讨论】:
-
如果你使用的黄瓜版本大于 4,那么你应该使用内置的并行执行支持,无论是 junit 还是 testng。 cucumber.io/blog/announcing-cucumber-jvm-4-0-0。它更干净,更简单。关于驱动程序重用,您可以将驱动程序存储在静态 ThreadLocal 变量中。这可能会有所帮助。 -stackoverflow.com/questions/55266736/…。事实上cucumber-jvm-parallel-plugin的插件创建者建议停止使用插件github.com/temyers/cucumber-jvm-parallel-plugin
-
好吧,现在更新黄瓜文件不是一个选项,因为我们的项目需要很多更改,即使这样,我也尝试过使用 Cucumber 4。我尝试使用链接中提到的步骤已经为 Cucumber 4 提供了,但它根本没有创建多个浏览器实例。 :( 我会再试一次,TestNg 可能可行。但这有点棘手,因为我需要在这个项目中进行很多更改以使其与 Cucumber 4 兼容。
-
不过,感谢您的回复。 :)
-
所以我尝试使用 TestNg 和 CUcumber 4.2.6,但它继续并行运行所有测试,但在同一个浏览器中。不确定如何对浏览器实例进行线程安全。有没有更简单的方法来做到这一点?
标签: java maven junit cucumber testng