如何并行执行黄瓜特征文件

问题描述 投票:8回答:4

我在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则用作跑步者。另外,让我知道如何使用功能文件动态创建测试运行器并使它们并行运行。

java cucumber testng cucumber-jvm
4个回答
12
投票

更新:4.0.0版本可在maven中央存储库中获得,其中包含大量更改.for more details go here.

更新:maven中央存储库提供2.2.0版本。

您可以使用开源插件qazxsw poi,它比现有解决方案有许多优点。可在maven qazxsw poi购买

cucumber-jvm-parallel-plugin
  1. 首先,您需要在项目pom文件中添加具有所需配置的此插件。 repository
  2. 现在在插件上面添加下面的插件,它将调用上面插件生成的跑步者类 <dependency> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>2.1.0</version> </dependency>
  3. 以上两个插件将为黄瓜测试并行运行提供魔力(前提是您的机器也具有高级硬件支持)。
  4. 严格提供<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> 这里'n'与1)高级硬件支持和2)您可用的节点,即注册浏览器实例到HUB成正比。
  5. 一个主要且最重要的变化是你的WebDriver类必须是SHARED而你不应该实现driver.quit()方法,因为关闭是由shutdown hook来处理的。 <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>
  6. 考虑到您要执行超过50个线程,即同样没有浏览器实例注册到HUB但如果没有足够的内存,Hub将会死亡,因此为了避免这种危急情况,您应该使用-DPOOL_MAX = 512(或更大)来启动集线器正如<forkCount>n</forkCount>所述。 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()); } } } grid2 documentation

3
投票

如果你所期望的是能够并行运行多个功能,那么你可以尝试执行以下操作:

  • 在测试项目中复制类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设置为AbstractTestNGCucumberTests注释方法。

由于TestNG的默认parallel=true@DataProvider,现在您已指示TestNG并行运行dataprovider-thread-count,您应该开始看到您的要素文件并行执行。

但据我所知,Cucumber报告本质上不是线程安全的,因此您的报告可能会出现乱码。


3
投票

黄瓜不支持开箱即用的并行执行。我试过了,但不友好。

  1. 我们必须使用maven的功能来并行调用它。请参阅10
  2. 还有一个github项目,它使用自定义插件并行执行。请参阅features

0
投票

要充分利用TestNG,您可以使用Testng的第三方扩展link框架。它支持多种cucumber-jvm-parallel-plugin,包括使用QAF的小黄瓜。在使用带有QAF的BDD时,您可以利用每个TestNG功能,包括数据提供程序,以不同方式(组/测试/方法)和TestNG侦听器的并行执行配置。

QAF将每个场景视为TestNG测试,并将场景大纲视为TestNG数据驱动测试。由于qaf提供了内置的驱动程序管理和资源管理,因此您无需为驱动程序管理或资源管理编写单行代码。您需要做的就是根据您的要求创建TestNG xml配置文件,以便在一个或多个浏览器上运行并行方法(方案)或组或xml测试。

它使不同的可能bdd syntax。下面是解决此问题的xml配置,它将在两个浏览器中并行运行场景。您还可以将每个浏览器的线程数配置为标准TestNG xml配置。

GherkinFactory

更多关于最新的configuration combinations支持<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> ,它来自QAF BDD,Jbehave和小黄瓜。它支持来自qaf bdd的元数据作为标记和来自小黄瓜的例子。您可以利用BDDTestFactory2在BDD中使用元数据在XML / JSON / CSV / EXCEL / DB中提供测试数据。

© www.soinside.com 2019 - 2024. All rights reserved.