Home >Java >javaTutorial >How to execute Cucumber feature files in parallel with different browsers and tags?

How to execute Cucumber feature files in parallel with different browsers and tags?

Barbara Streisand
Barbara StreisandOriginal
2024-10-29 16:49:021000browse

How to execute Cucumber feature files in parallel with different browsers and tags?

Executing Cucumber Feature Files in Parallel

Problem: Several cucumber feature files, each associated with different tags, need to be executed in parallel, with specific files assigned to specific browsers.

Solution:

Utilizing the open-source cucumber-jvm-parallel-plugin provides a feature-rich solution for parallel cucumber test execution.

  1. Plugin Configuration:

Add the following plugin configuration to the project's Maven pom file:

<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>
  1. Second Plugin Addition:

Add another plugin that will invoke the runner classes generated by the previous plugin:

<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>

Shared WebDriver Class:

Implement a shared WebDriver class:

<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>

Considerations:

  • Number of fork counts (parallel threads) should align with the available hardware resources and registered nodes.
  • The grid hub must be started with -DPOOL_MAX=512 or higher for executing a large number (>50) of threads in parallel.

The above is the detailed content of How to execute Cucumber feature files in parallel with different browsers and tags?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn