简体   繁体   English

无法使用 TestNG 和 SpringBootTest 并行运行 Cucumber 测试

[英]Cant run Cucumber tests in parallel using TestNG and SpringBootTest

I'm trying to run 2 Cucumber tests in parallel using TestNG and SpringBootTest but when my tests execute the following happens我正在尝试使用 TestNG 和 SpringBootTest 并行运行 2 个 Cucumber 测试,但是当我的测试执行时会发生以下情况

  • 2 browsers open and both navigate to the Wikipedia homepage. 2 个浏览器打开并且都导航到 Wikipedia 主页。
  • 1 browser continues the test, the other stays on the homepage 1个浏览器继续测试,其他留在首页
  • 1 test passes and the other fails 1个测试通过,另一个失败

I'm not sure why one test stops executing, any help would be welcome.我不确定为什么一个测试停止执行,欢迎任何帮助。

Repo: https://github.com/cmccarthyIrl/spring-cucumber-testng-parallel-test-harness回购: https://github.com/cmccarthyIrl/spring-cucumber-testng-parallel-test-harness

Test Runner测试赛跑者

@CucumberOptions(
        features = {
                "src/test/resources/feature/"
        },
        plugin = {
                "pretty",
                "html:target/cucumber/report-html",
                "json:target/cucumber/report.json",
                "junit:target/cucumber/junit_report.xml",
                "timeline:target/cucumber/timeline"
        })
public class ParallelRunner extends AbstractTestNGCucumberTests {

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

}

DriverManager Class DriverManager Class

@Component
public class DriverManager {

    private WebDriver webDriver;
    private Wait<WebDriver> webDriverWait;
    private static ThreadLocal<WebDriver> driverThreadLocal = new ThreadLocal<>();
    private static ThreadLocal<Wait<WebDriver>> driverWaitThreadLocal = new ThreadLocal<>();

    @Autowired
    private ApplicationProperties applicationProperties;

    public void driverManager() {
        if (getDriver() == null) {
            setLocalWebDriver();
        }
    }

    public void setLocalWebDriver() {
        switch (applicationProperties.getBrowser()) {
...
            case ("firefox"):
                System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/src/test/resources/geckodriver");
                FirefoxOptions firefoxOptions = new FirefoxOptions();
//                firefoxOptions.setHeadless(true);
                firefoxOptions.setCapability("marionette", true);

                webDriver = new FirefoxDriver(firefoxOptions);
                break;
       ...
            default:
                throw new NoSuchElementException("Failed to create an instance of WebDriver for: " + applicationProperties.getBrowser());
        }
        driverThreadLocal.set(webDriver);
        webDriverWait = new WebDriverWait(webDriver, 10, 500);
        driverWaitThreadLocal.set(webDriverWait);
        try {
            Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
        }catch(Exception ex){
            System.out.println("ex.getMessage() = " + ex.getMessage());
        }
    }

    ...

Change the PageObject annotation and add @Scope("prototype") Refer更改 PageObject 注释并添加 @Scope("prototype") 参考

@Retention(RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@PageFactoryFinder(FindBy.FindByBuilder.class)
@Lazy
@Component
@Scope("prototype")
public @interface PageObject {
}

Now for each scenario you should get the new instance of page object .现在,对于每个场景,您都应该获得页面对象的新实例。

Also I may prefer to have some changes in DriverManager eg此外,我可能更喜欢在 DriverManager 中进行一些更改,例如

driverThreadLocal.set(new FirefoxDriver(firefoxOptions));
driverWaitThreadLocal.set(new WebDriverWait(driverThreadLocal.get(), 10, 500));

You can remove this logic of cleaning the drivers您可以删除此清理驱动程序的逻辑

try { Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);尝试 { Runtime.getRuntime().addShutdownHook(CLOSE_THREAD); }catch(Exception ex){ System.out.println("ex.getMessage() = " + ex.getMessage()); }catch(Exception ex){ System.out.println("ex.getMessage() = " + ex.getMessage()); } }

Move Driver.Quit to Hooks.java https://github.com/cmccarthyIrl/spring-cucumber-test-harness/blob/testng/wikipedia/src/test/java/com/cmccarthy/step/Hooks.java将 Driver.Quit 移动到 Hooks.java https://github.com/cmccarthyIrl/spring-cucumber-test-harness/blob/testng/wikipedia/src/test/java/com/cmccarthy/step/Hooks.java

  @After
    public void afterScenario(Scenario scenario) {
        hookUtil.endOfTest(StringUtils.capitalize(scenario.getStatus().toString()));
        driverManager.getDriver().quit();
    }

I'm trying to run 2 Cucumber tests in parallel and sequential using TestNG and SpringBootTest but when my tests execute the following happens我正在尝试使用 TestNG 和 SpringBootTest 并行和顺序运行 2 个 Cucumber 测试,但是当我的测试执行时会发生以下情况

mvn test mvn 测试

2 browsers open and both navigate to the Wikipedia homepage. 2 个浏览器打开并且都导航到 Wikipedia 主页。

if you add 2 more scenarios it opens those many threads per scenario, I don't have any control over the number of threads to execute.如果您再添加 2 个场景,它会为每个场景打开那么多线程,我无法控制要执行的线程数。

How to control the number of threads and dataprovider count, any help is appreciated.如何控制线程数和数据提供者计数,任何帮助表示赞赏。

Repo: https://github.com/cmccarthyIrl/spring-cucumber-testng-parallel-test-harness回购: https://github.com/cmccarthyIrl/spring-cucumber-testng-parallel-test-harness

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM