Parallel Test run with cucumber

蓝咒 提交于 2019-12-06 04:20:44

info.cukes dependency (are pretty old) which support cucumber till v 1.2.5 only and No more support for this after 12 September 2016

Other side, io.cucumber dependency supports Cucumber starting from v 2.0.x till latest v 4.3.x available as of now (Cucumber-JVM 4.0 gives you much flexibility for implementing parallel execution) and below are the steps to implement parallel execution using io.cucumber (Here you do not need to create individual runners per feature file & No need to use any old plug in like jvm-parallel plug in)

1.Adding correct set of dependency. I have followed JUnit during the implementation.

<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.Adding Maven-Surefire-Plugin under POM.XML

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

Cucumber feature files can be executed without using any runner by using the main() method in class Main of package cucumber.api.cli. Refer to this cli usage and this article. Using this maybe it will work. Need some changes though.

  1. Copy the code of BrowserStackJUnitTest into a new class rename it to something else say NewBSTest. This class will be used for running the test.

  2. Some change will be required in how the driver created for each thread will be accessed by selenium and cucumber code.

  3. Need to use a factory to store the drivers in a ThreadLocal variable in NewBSTest. U can refer to this class.

  4. In the NewBSTest class that u have created remove line no 30 - public WebDriver driver;. Change line 94 to add the created RemoteWebDriver to the ThreadLocal variable. SOmething like DriverFactory.addDriver(driver). Add DriverFactory.removeDriver() in method tearDown() line 98, maybe the first line. Change existing driver.quit(); to DriverFactory.getDriver().quit().

5.Remove the shutdown hook in the DriverFactory, BrowserStack will be quitting the actual driver anyways.

  1. Now in selenium or cucumber code u can access the driver using DriverFactory.getDriver(). This will impact your existing code pretty heavily.

  2. In the NewBSTest class add a test method like this. Hopefully this will work and the same features will be executed in each of the configured browserstack environments.

@Test
public void test() {
Main.main(new String[]{""-g", "stepdef", "src/test/resources/features/"});
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!