黄瓜未与Maven并行运行

时间:2019-07-17 14:35:48

标签: maven selenium cucumber cucumber-jvm maven-surefire-plugin

我正在尝试使用Maven与Selenium并行运行黄瓜测试。 我尝试了maven surefire插件,failsafe插件,甚至是旧的黄瓜jvm并行插件。我得到了相同的结果。我的测试按顺序运行,没有并行化。

我尝试配置maven-surefire-plugin,然后再配置maven-failsafe-plugin。没有任何帮助。

这是我的pom.xml文件

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <fork>true</fork>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <forkCount>2</forkCount>
                    <reuseForks>true</reuseForks>
                    <includes>
                        <include>**/RunnerTest.class</include>
                    </includes>

                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>4.2.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.2.6</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.2.6</version>
        </dependency>

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.3.0.jre8-preview</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>4.7.0</version>
        </dependency>
    </dependencies>

这是我的故障安全插件配置。但是它禁用了

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
                <executions>
                    <execution>
                        <id>acceptance-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <rerunFailingTestsCount>1</rerunFailingTestsCount>
                            <testFailureIgnore>true</testFailureIgnore>
                            <includes>
                                <include>**/RunnerTest.class</include>
                                <include>**/*IT.class</include>
                            </includes>
                            <forkCount>2</forkCount>
                            <reuseForks>true</reuseForks>
                            <perCoreThreadCount>false</perCoreThreadCount>
                            <reportsDirectory>target</reportsDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

还有我的Runner班

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/features",
        glue = "step_definitions",
        plugin = {"pretty", "html:target/cucumberHtmlReport", "json:target/cucumber-report.json"},
        monochrome = true,
        strict = true
)
public class RunnerTest {

}

要在Maven中运行测试,请使用以下命令:

mvn clean verify "-Dcucumber.options=--tags @Test-1,@Test-2" -Dbrowser=chrome

此外,我可以说我正在使用静态webDriver和Singleton类来处理属性。我不知道它会如何影响,但仅供参考。如果需要,我可以提供代码。

目前,我希望在fork中运行测试,因为线程处理是另一个大问题。

1 个答案:

答案 0 :(得分:0)

请按照以下步骤操作。

关键点:

  • 我们将不会特别混合直接和传递依赖项的版本!这样做可能会导致不可预测的结果。
  • 当cucumber版本大于或等于4.0.0时,我们不需要使用cucumber-jvm-parallel或cucable插件。

通过JUnit进行黄瓜并行执行

首先-用一组正确的io.cucumber依赖关系更新POM.xml。

 <dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.6</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.2.6</version>
</dependency>

要注意的地方-可能会出现类似一切正常的问题,但是测试不能并行执行,并且可能是pom.xml具有testng的直接/传递依赖关系。由于testNG导致Surefire忽略JUnit包装器类。如果您有testng依赖关系,请删除TestNG依赖关系,或者您可以调用2定义2执行-对于TestNG和JUnit并根据需要禁用一个。

第二-根据您的框架需要自定义Runner类

package com.jacksparrow.automation.suite.runner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features/functional/",
                     glue = {"com.jacksparrow.automation.steps_definitions.functional" },
                   plugin = { "pretty","json:target/cucumber-json/cucumber.json",
                            "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
                   tags = { "@BAMS_Submitted_State_Guest_User" },
                   junit ={ "--step-notifications"},
                   strict = false,
                   dryRun = false,
               monochrome = true)

public class RunCukeTest {
}

第三-实现了maven surefire插件,该插件实际上可以并行运行测试

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <parallel>methods</parallel>
        <threadCount>2</threadCount>
        <reuserForks>false</reuserForks>
        <testFailureIgnore>true</testFailureIgnore>
        <includes>
            <include>**/*RunCukeTest.java</include>
        </includes>
    </configuration>
</plugin>

第四-实现Hooks.java

import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.After;

public class Hooks {

    @Before
    public void setUpScenario(String browser){
        //BaseSteps.getInstance().getBrowserInstantiation(browser); your browser setup method
    }
    @After
    public void afterScenario(Scenario scenario){
    // more code goes here  
    }
   }