Search code examples
junit5selenium-jupiter

Standard approach for multi browser test execution in Selenium Jupiter


I went through Selenium Jupiter manual and still cannot get the idea of how I can set multiple browsers in Selenium Jupiter to run every test in every browser.

Should use Test Template for that purpose?

Again I did not see an example of how can I do it in Selenium Jupiter?

p.s. An example with RemoteDrivers on Selenium Grid.

Here is my attempt to do it:

public class BaseTestWithRemoteDrivers {

@RegisterExtension
static SeleniumExtension extension = new SeleniumExtension();

@BeforeAll
public static void setupAll() {
    extension.getConfig().setSeleniumServerUrl("http://localhost:4444/wd/hub");
    Browser chrome = BrowserBuilder.chrome().build();
    Browser firefox = BrowserBuilder.firefox().build();
    extension.addBrowsers(chrome, firefox);
}

@Test
public void testWithBrowser(WebDriver driver) {
    driver.get("https://www.google.com");
}

@AfterAll
public static void tearDownAll(WebDriver driver) {
    driver.quit();
}

Unfortunately, only the Chrome browser will open.

Upd: I also found that there is a message saying:

Browser list for context id is not found. Not sure how to set up Browsers List if it is needed.


Solution

  • So far I did not find multi browsers support except by explicitly putting the browsers type into mvn command like below:

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

     @ExtendWith(SeleniumExtension.class)
        public class BaseTest {
        
            @Test
            public void testNumber1(RemoteWebDriver driver) throws {
                driver.get("https://www.google.com/");
          
            }
        
        
            @AfterAll()
            public static void tearDown(RemoteWebDriver driver) {
                driver.quit();
            }
        }
    

    Update:

    I figured out the way I can do it with Test Template too.

    Below is the working example:

    public class MultiBrowserTestTemplate {
    
    @RegisterExtension
    static SeleniumExtension extension = new SeleniumExtension();
    
    @BeforeAll
    static void setup() {
        String browsersList = System.getProperty("prop.browsers.list");
        List<String> browsers = Arrays.asList(browsersList.split(","));
        if (browsers.contains("chrome")) {
            extension.addBrowsers(BrowserBuilder.chrome().version("80.0.3987.106").build());
        }
        if (browsers.contains("firefox")) {
            extension.addBrowsers(BrowserBuilder.firefox().version("73.0").build());
        }
    }
    }
    
    public class MultiBrowserDemoTest extends MultiBrowserTestTemplate {
    
        @TestTemplate
        public void testInMultipleBrowsers(WebDriver driver) {
            driver.get("https://www.google.com/");
            WebElement search = driver.findElement(By.name("q"));
            search.sendKeys("JUnit5 extensions");
            search.submit();
        }
    

    And the maven command goes like this:

    mvn verify -DMultiBrowserDemoTest
    -Dsel.jup.selenium.server.url=http://localhost:4444/wd/hub
    -Dprop.browsers.list=chrome,firefox