我正在尝试使用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>
所以这个想法是,假设我给Thread count或Fork count 3,它将调用3个浏览器,并运行测试。使用CUcumber JVM并行插件,我为所有场景创建了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。我曾经使用Runner文件,该文件扩展到SetupTearDown类以进行非并行执行。但是显然不能使用它,因为运行时类在运行时生成。也尝试使用集成前,集成后阶段。哪个没有运行。可能是我没有正确使用该阶段。
答案 0 :(得分:1)
可以逐步移动。首先,我们将从4.0开始使用Cucumber V,因为它支持并行执行,并且我们将在下面添加依赖项以实现它。
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>datatable</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
然后,我们将开始使用maven-surefire插件,以实际使用并行和threadCount属性并行运行测试,如下所述-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire.plugin.version}</version>
<configuration>
<parallel>methods</parallel>
<threadCount>1</threadCount>
<reuserForks>false</reuserForks>
<testErrorIgnore>true</testErrorIgnore>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*RunCukeTest.java</include>
</includes>
</configuration>
</plugin>
取决于实现方式,它将在不同的浏览器中运行测试用例并关闭全部。现在,如果您只想在执行之前或之后执行一次特定的操作,则应在RunCuke Java文件中使用@BeforeClass或@AfterClass。事情是线程安全的。
答案 1 :(得分:-1)
查看您现有的代码,它不是线程安全的,并且由于静态Browser
而无法并行执行。
您应该尝试使用QAF - Pure TestNG implementation for BDD/Gherkin,它提供了gherkin implementation,使您可以使用所有TestNG侦听器,并为驱动程序和元素使用其他listeners。此外,qaf会注意驱动程序管理,因此您可能无需编写其他代码来管理驱动程序或其他特定于测试自动化的需求。 qaf将在测试后关闭所有浏览器实例,并允许您进行场景级别的并行执行。