我在src / test / resources / feature /中有以下功能文件(单独的功能文件),我想并行运行它们。喜欢:一个特征文件必须在chrome中执行,另一个必须在firefox中执行,如@Tags名称所述。
Feature: Refund item
@chrome
Scenario: Jeff returns a faulty microwave
Given Jeff has bought a microwave for $100
And he has a receipt
When he returns the microwave
Then Jeff should be refunded $100
Feature: Refund Money
@firefox
Scenario: Jeff returns the money
Given Jeff has bought a microwave for $100
And he has a receipt
When he returns the microwave
Then Jeff should be refunded $100
有人可以协助我实现这个目标。我正在使用cucumber-java 1.2.2版本,而AbstractTestNGCucumberTests则用作跑步者。另外,让我知道如何使用功能文件动态创建测试运行器并使它们并行运行。
答案 0 :(得分:12)
更新:maven中央存储库中提供了4.0.0版本,其中包含大量更改。for more details go here.
更新:maven中央存储库提供2.2.0版本。
您可以使用开源插件cucumber-jvm-parallel-plugin,它比现有解决方案具有许多优势。可在maven repository
获取 <dependency>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>2.1.0</version>
</dependency>
首先,您需要在项目pom文件中添加具有所需配置的此插件。
<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>
<!-- Mandatory -->
<!-- comma separated list of package names to scan for glue code -->
<glue>foo, bar</glue>
<outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory>
<!-- The directory, which must be in the root of the runtime classpath, containing your feature files. -->
<featuresDirectory>src/test/resources/features/</featuresDirectory>
<!-- Directory where the cucumber report files shall be written -->
<cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
<!-- comma separated list of output formats json,html,rerun.txt -->
<format>json</format>
<!-- CucumberOptions.strict property -->
<strict>true</strict>
<!-- CucumberOptions.monochrome property -->
<monochrome>true</monochrome>
<!-- The tags to run, maps to CucumberOptions.tags property you can pass ANDed tags like "@tag1","@tag2" and ORed tags like "@tag1,@tag2,@tag3" -->
<tags></tags>
<!-- If set to true, only feature files containing the required tags shall be generated. -->
<filterFeaturesByTags>false</filterFeaturesByTags>
<!-- Generate TestNG runners instead of default JUnit ones. -->
<useTestNG>false</useTestNG>
<!-- The naming scheme to use for the generated test classes. One of 'simple' or 'feature-title' -->
<namingScheme>simple</namingScheme>
<!-- The class naming pattern to use. Only required/used if naming scheme is 'pattern'.-->
<namingPattern>Parallel{c}IT</namingPattern>
<!-- One of [SCENARIO, FEATURE]. SCENARIO generates one runner per scenario. FEATURE generates a runner per feature. -->
<parallelScheme>SCENARIO</parallelScheme>
<!-- This is optional, required only if you want to specify a custom template for the generated sources (this is a relative path) -->
<customVmTemplate>src/test/resources/cucumber-custom-runner.vm</customVmTemplate>
</configuration>
</execution>
</executions>
</plugin>
现在在插件上方添加以下插件,这将调用上面插件生成的跑步者类
<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>
以上两个插件将为并行运行的黄瓜测试提供魔力(假设您的机器也具有高级硬件支持)。
严格提供<forkCount>n</forkCount>
此处&#39; n&#39;与1)高级硬件支持和2)您可用的节点,即已注册的浏览器实例到HUB相关。
一个主要且最重要的变化是您的WebDriver类必须 SHARED ,您应该不实现driver.quit()方法,因为关闭是照顾通过shutdown hook。
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class SharedDriver extends EventFiringWebDriver {
private static WebDriver REAL_DRIVER = null;
private static final Thread CLOSE_THREAD = new Thread() {
@Override
public void run() {
REAL_DRIVER.close();
}
};
static {
Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
}
public SharedDriver() {
super(CreateDriver());
}
public static WebDriver CreateDriver() {
WebDriver webDriver;
if (REAL_DRIVER == null)
webDriver = new FirefoxDriver();
setWebDriver(webDriver);
return webDriver;
}
public static void setWebDriver(WebDriver webDriver) {
this.REAL_DRIVER = webDriver;
}
public static WebDriver getWebDriver() {
return this.REAL_DRIVER;
}
@Override
public void close() {
if (Thread.currentThread() != CLOSE_THREAD) {
throw new UnsupportedOperationException("You shouldn't close this WebDriver. It's shared and will close when the JVM exits.");
}
super.close();
}
@Before
public void deleteAllCookies() {
manage().deleteAllCookies();
}
@After
public void embedScreenshot(Scenario scenario) {
try {
byte[] screenshot = getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
} catch (WebDriverException somePlatformsDontSupportScreenshots) {
System.err.println(somePlatformsDontSupportScreenshots.getMessage());
}
}
}
考虑到您要执行超过50个线程,即同样没有浏览器实例注册到HUB但如果没有足够的内存而Hub将会死,因此为了避免这种危急情况,您应该启动集线器-DPOOL_MAX = 512(或更大),如grid2 documentation中所述。
Really large (>50 node) Hub installations may need to increase the jetty threads by setting -DPOOL_MAX=512 (or larger) on the java command line.
java -jar selenium-server-standalone-<version>.jar -role hub -DPOOL_MAX=512
答案 1 :(得分:3)
Cucumber不支持开箱即用的并行执行。 我试过了,但不友好。
答案 2 :(得分:3)
如果您希望能够并行运行多个功能,那么您可以尝试执行以下操作:
parallel=true
设置为@DataProvider
带注释的方法。由于TestNG的默认dataprovider-thread-count
为10
,现在您已指示TestNG并行运行features
,您应该开始看到您的要素文件并行执行。
但据我所知,Cucumber报告本质上不是线程安全的,因此您的报告可能会出现乱码。
答案 3 :(得分:0)
要充分利用TestNG,可以使用Testng的第三方扩展QAF框架。它支持使用bdd syntax的多个GherkinFactory,包括嫩黄瓜。 在将BDD与QAF结合使用时,您可以利用每个TestNG功能,包括数据提供者,以不同方式(组/测试/方法)进行并行执行配置,TestNG侦听器。
QAF将每个方案视为TestNG测试,并将方案大纲视为TestNG数据驱动的测试。由于qaf提供内置的驱动程序管理和资源管理,因此您无需编写任何代码即可进行驱动程序管理或资源管理。您所需要做的就是根据您的要求创建TestNG xml配置文件,以在一个或多个浏览器上运行并行方法(场景)或组或xml测试。
它启用其他可能的configuration combinations。下面是用于解决此问题的xml 配置,它将在两个浏览器中并行运行方案。您还可以将每个浏览器的线程数配置为标准TestNG xml配置。
<suite name="AUT Test Automation" verbose="0" parallel="tests">
<test name="Test-on-chrome">
<parameter name="scenario.file.loc" value="resources/features" />
<parameter name="driver.name" value="chromeDriver" />
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
</test>
<test name="Test-on-FF">
<parameter name="scenario.file.loc" value="resources/features" />
<parameter name="driver.name" value="firefoxDriver" />
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
</test>
</suite>
更多来自最新的BDDTestFactory2
支持syntax,该支持源自QAF BDD,Jbehave和gherkin。它支持来自qaf bdd的元数据作为小黄瓜的标签和示例。您可以利用inbuilt data-providers来使用BDD中的元数据以XML / JSON / CSV / EXCEL / DB提供测试数据。
答案 4 :(得分:0)
我使用 courgette-jvm
实现了黄瓜并行性。它开箱即用并在场景级别运行并行测试
只需在黄瓜中包含类似的跑步者类。我的测试进一步使用 RemoteWebdriver
在 selenium 网格上打开多个实例。确保网格已启动并正在运行,并且节点已注册到网格。
import courgette.api.CourgetteOptions;
import courgette.api.CourgetteRunLevel;
import courgette.api.CucumberOptions;
import courgette.api.testng.TestNGCourgette;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@Test
@CourgetteOptions(
threads = 10,
runLevel = CourgetteRunLevel.SCENARIO,
rerunFailedScenarios = true,
rerunAttempts = 1,
showTestOutput = true,
reportTitle = "Courgette-JVM Example",
reportTargetDir = "build",
environmentInfo = "browser=chrome; git_branch=master",
cucumberOptions = @CucumberOptions(
features = "src/test/resources/com/test/",
glue = "com.test.stepdefs",
publish = true,
plugin = {
"pretty",
"json:target/cucumber-report/cucumber.json",
"html:target/cucumber-report/cucumber.html"}
))
class AcceptanceIT extends TestNGCourgette {
}
RemoteWebdriver 配置是
protected RemoteWebDriver createDriver() throws MalformedURLException , IOException {
Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String hubURL = "http://192.168.1.7:65299/wd/hub";
System.setProperty("webdriver.gecko.driver", "/Users/amit/Desktop/amit/projects/misc/geckodriver");
FirefoxProfile profile = new FirefoxProfile();
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setPlatform(Platform.ANY);
FirefoxOptions options = new FirefoxOptions();
options.merge(capabilities);
driver.set(new RemoteWebDriver(new URL(hubURL),options));
return driver.get();
}