junit5junit5-extension-model

How can I control how many JUnit5 Test Instances are going to be generated in Runtime and apply separate ParameterResolver to each of the instance


I'd like to execute Selenium Grid tests using Maven like this:

mvn verify-Dtest=BaseTest -Dprop.selenium.server.url=http://localhost:4444/wd/hub -Dprop.browser=chrome
-Dprop.version=80.0.3987.106

I inject ChromeDriver into Test constructor using JUnit5 ParameterResolver interface

   @ExtendWith(ChromeRemoteWebDriverParameterResolver.class)
   @TestInstance(TestInstance.Lifecycle.PER_CLASS)
   public class MultiBrowserDemoTest {

    RemoteWebDriver driver;

    public MultiBrowserDemoTest(RemoteDriver driver) {
        this.driver = driver.getDriver();
    }


    @SneakyThrows
    @Test
    public void testGrid() {
        driver.get("https://www.google.com/");
        WebElement search = driver.findElement(By.name("q"));
        search.sendKeys("JUnit5 extensions");
        search.submit();
    }

    @AfterAll()
    void tearDown() {
        driver.quit();
    }
}

It works fine. But I can't see how to implement multi-browser test execution.

Let's say, I want to add multiple browsers -Dprop.browser=chrome, firefox, opera, ie11

I created multiple classes implementing ParameterResolver Interface. But JUnit5 does not let me inject them all into my Test Class. It does not create new instances of Test class either.

I tried to use TestInstanceFactory to create new Instances of my Test class and apply separate implementation of ParameterResolver interface, but it did not work for me.

End Result: I can run the same test in multiple browsers in parallel using Selenium Grid and only one Test Class that I will be able to instantiate multiple times with separate webdriver.


Solution

  • If I understood your scenario correctly, what you are asking for is support for what we call "parameterized containers" (i.e., something analogous to @ParameterizedTest but at the class level), which does not exist yet in JUnit Jupiter.

    See JUnit Jupiter issues #871 and #878 for details.