如何在 Maven Surefire 插件并行执行中控制实例与黄瓜 testng 执行

问题描述

目标是将我的黄瓜功能场景运行到并行实例。但我无法控制并行实例。

示例 - 我有 5 个特征文件并运行 2 个并行实例,但是如果我运行我的项目,它会直接运行 5 个实例。 如果我希望它先运行 2 个功能文件,然后完成其中一个,则该功能可以选择下一个实例。

我正在使用带有 testng 的黄瓜来处理它。

这是我的跑步者文件

import managers.FileReader;
import org.apache.log4j.PropertyConfigurator;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
        publish = true,features = "classpath:parallel",glue = "parallel",plugin = {"junit:target/cucumber-results.xml","rerun:target/rerun.txt","json:target/cucumber-reports/CucumberTestReport.json","html:target/cucumber-report.html","timeline:test-output-thread/"},tags="@MOM",monochrome = true
)
public class ParallelRun extends AbstractTestNGCucumberTests {

    @BeforeClass
    public static void setup(){
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("mac")) {
            PropertyConfigurator.configure(System.getProperty("user.dir")+"/src/test/resources/log4j.properties");
        }
        else {
            PropertyConfigurator.configure(FileReader.getInstance().getConfigReader().getlog4jpath());
        }
    }

    @AfterClass
    public static void writeExtentReport() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("mac")) {
        }
        else {
        }
    }

    @Override
    @DataProvider(parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();
    }

这是我的 maven 插件配置。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.surefire.version}</version>
    <configuration>
        <includes>
            <include>**/ParallelRun.java</include>
        </includes>
        <forkCount>1</forkCount>
        <reuseForks>true</reuseForks>
        <parallel>methods</parallel>
        <threadCount>1</threadCount>
        <perCoreThreadCount>false</perCoreThreadCount>
        <useUnlimitedThreads>false</useUnlimitedThreads>
        <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
    </configuration>
</plugin>

驱动工厂类

public static ThreadLocal<WebDriver> tlDriver = new ThreadLocal<>();
public WebDriver createParaLLELDriver() throws MalformedURLException {
    //Initializing the chrome driver
    System.out.println("The thread ID for Chrome is "+ Thread.currentThread().getId());
    chromedriver().setup();
    tlDriver.set(new ChromeDriver());
}

/**
* this is used to get the driver with ThreadLocal
*
* @return
*/
public static synchronized WebDriver setDriver() {
    System.out.println("Parallel Selenium instances driver factory");
    return tlDriver.get();
}

这是我在运行时打印的实例线程数的示例输出。 Chrome 的线程 ID 为 15 Chrome 的线程 ID 为 16 Chrome 的线程 ID 为 17 Chrome 的线程 ID 为 18 Chrome 的线程 ID 为 19

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)