问题: 需要并行执行多个 Cucumber 特征文件,每个特征文件与不同的标签相关联,将特定文件分配给特定浏览器。
解决方案:
利用开源cucumber-jvm-parallel-plugin提供了一个功能-用于并行黄瓜测试执行的丰富解决方案。
将以下插件配置添加到项目的 Maven pom 文件中:
<code class="xml"><dependency> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>2.1.0</version> </dependency> <plugin> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <id>generateRunners</id> <phase>generate-test-sources</phase> <goals> <goal>generateRunners</goal> </goals> <configuration> <glue>foo, bar</glue> <outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory> <featuresDirectory>src/test/resources/features/</featuresDirectory> <cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir> <format>json</format> <strict>true</monochrome>true</strict> <tags>@chrome,@firefox</tags> <filterFeaturesByTags>false</filterFeaturesByTags> <useTestNG>false</useTestNG> <namingScheme>simple</namingScheme> <parallelScheme>SCENARIO</parallelScheme> </configuration> </execution> </executions> </plugin></code>
添加另一个插件,该插件将调用上一个插件生成的运行器类:
<code class="xml"><plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <forkCount>5</forkCount> <reuseForks>true</reuseForks> <includes> <include>**/*IT.class</include> </includes> </configuration> </plugin></code>
共享 WebDriver 类:
实现共享 WebDriver 类:
<code class="java">public class SharedDriver extends EventFiringWebDriver { // Singleton WebDriver instance private static WebDriver REAL_DRIVER = null; // Closing hook for WebDriver private static final Thread CLOSE_THREAD = new Thread() { @Override public void run() { REAL_DRIVER.close(); } }; // WebDriver setup static { Runtime.getRuntime().addShutdownHook(CLOSE_THREAD); } public SharedDriver() { super(CreateDriver()); } public static WebDriver CreateDriver() { if (REAL_DRIVER == null) REAL_DRIVER = new FirefoxDriver(); setWebDriver(REAL_DRIVER); return REAL_DRIVER; } public static void setWebDriver(WebDriver webDriver) { SharedDriver.REAL_DRIVER = webDriver; } public static WebDriver getWebDriver() { return SharedDriver.REAL_DRIVER; } // Disable closing for shared driver @Override public void close() { if (Thread.currentThread() != CLOSE_THREAD) { throw new UnsupportedOperationException("Closing is handled by shutdown hook."); } super.close(); } }</code>
注意事项:
以上是如何在不同浏览器和标签下并行执行Cucumber特征文件?的详细内容。更多信息请关注PHP中文网其他相关文章!