我目前正在尝试用黄瓜实现并行测试。我设法用sure-fire插件同时运行两个不同的跑步者。现在我想检查是否可以多次并行运行SingleRunner文件。
例如:我有SignUpRunnerTest.java所以我需要在几个平台上运行它。这有可能吗?
这是我的Runner文件
import cucumber.api.CucumberOptions;
import cucumber.api.cli.Main;
import cucumber.api.junit.Cucumber;
import java.util.List;
import javax.management.MXBean;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/html/", "json:target/cucumber.json", "junit:TEST-all.xml"},
features = "src/test/java/resources/features/Search.feature", glue = {"com.browserstack.stepdefs"})
public class SignUpeRunnerTest {
}
没有亚军方法
public class SignUpeRunnerTest {
@Test
public void test2() {
Main.main(new String[]{"--threads", "4","-g", "com.browserstack.stepdefs", "src/test/java/resources/features/"});
}
}
工厂类
`import org.openqa.selenium.WebDriver;
public final class DriverFactory {
private static ThreadLocal<WebDriver> drivers = new ThreadLocal();
//To quit the drivers and browsers at the end only.
private static List<WebDriver> storedDrivers = new ArrayList();
static {
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
storedDrivers.stream().forEach(WebDriver::quit);
}
});
}
private DriverFactory() {}
public static WebDriver getDriver() {
return drivers.get();
}
public static void addDriver(WebDriver driver) {
storedDrivers.add(driver);
drivers.set(driver);
}
public static void removeDriver() {
storedDrivers.remove(drivers.get());
drivers.remove();
}
}
`
步骤类
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;
public class SearchPage {private static WebDriver webDriver;
public SearchPage(WebDriver webDriver) {
this.webDriver = webDriver;
DriverFactory.addDriver(webDriver);
}
private By searchTermField = By.name("q");
private By submitSearch = By.id("_fZl");
public void enterSearchTerm(String searchTerm) {
DriverFactory.getDriver().findElement(searchTermField).sendKeys(searchTerm);
}
public void submitSearch() {
DriverFactory.getDriver().findElement(submitSearch).click();
}
}
这是我的POM文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.browserstack</groupId>
<artifactId>cucumber-jvm-java-browserstack</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cucumber-jvm-java-browserstack</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cucumber.jvm.parallel.version>2.2.0</cucumber.jvm.parallel.version>
<surefire.maven.plugin.version>2.19.1</surefire.maven.plugin.version>
<acceptance.test.parallel.count>4</acceptance.test.parallel.count>
</properties>
<dependencies>
<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>
<!-- https://mvnrepository.com/artifact/com.gfk.senbot/senbot-maven-plugin -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<parallel>methods</parallel>
<threadCount>4</threadCount>
<reuserForks>false</reuserForks>
<testErrorIgnore>true</testErrorIgnore>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*RunnerTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
info.cukes依赖(非常古老)支持黄瓜直到v 1.2.5并且在2016年9月12日之后不再支持黄瓜
另一方面,io.cucumber依赖支持Cucumber从v 2.0.x开始直到最新v 4.3.x(现在可用)(Cucumber-JVM 4.0为实现并行执行提供了很大的灵活性)以下是使用io实现并行执行的步骤.cucumber(这里你不需要为每个功能文件创建单独的运行程序,也不需要像jvm-parallel插件那样使用任何旧的插件)
1.添加正确的依赖集。我在实现过程中遵循了JUnit。
<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-picocontainer</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
2.在POM.XML下添加Maven-Surefire-Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</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>
使用main() method in class Main of package cucumber.api.cli
可以在不使用任何跑步者的情况下执行黄瓜特征文件。请参阅此cli usage和this article。使用它可能会起作用。但是需要一些改变。
NewBSTest
。该类将用于运行测试。ThreadLocal
中的NewBSTest
变量中。你可以参考这个class。NewBSTest
类中删除了第30行 - public WebDriver driver;
。更改第94行以将创建的RemoteWebDriver添加到ThreadLocal变量。像DriverFactory.addDriver(driver)
那样的东西。在方法DriverFactory.removeDriver()
第98行添加tearDown()
,也许是第一行。更改现有的driver.quit();到DriverFactory.getDriver().quit()
。5.删除DriverFactory中的shutdown hook,BrowserStack将退出实际的驱动程序。
DriverFactory.getDriver()
访问驱动程序。这会严重影响您现有的代码。NewBSTest
类中添加这样的测试方法。希望这将起作用,并且将在每个配置的browserstack环境中执行相同的功能。@Test public void test() { Main.main(new String[]{""-g", "stepdef", "src/test/resources/features/"}); }