Skip to content

Instantly share code, notes, and snippets.

@beatngu13
Last active May 14, 2024 13:43
Show Gist options
  • Save beatngu13/8d5f60355d7fbef143198f020b1efca3 to your computer and use it in GitHub Desktop.
Save beatngu13/8d5f60355d7fbef143198f020b1efca3 to your computer and use it in GitHub Desktop.
Dead-simple cross-browser testing with Selenium and JUnit 5 parameterized tests
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.WebDriver;
class CrossBrowserTest {
@ParameterizedTest
@MethodSource( "my.package.WebDriverFactory#getAll" )
void cross_browser_test( final WebDriver driver ) {
System.out.println( "Test with " + driver.getClass().getSimpleName() );
}
}
import java.util.Arrays;
import java.util.stream.Stream;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebDriverFactory {
enum Driver {
CHROME,
FIREFOX
// add more drivers
}
public static WebDriver get( final Driver driver ) {
// add default options
switch ( driver ) {
case CHROME:
return new ChromeDriver();
case FIREFOX:
return new FirefoxDriver();
default:
throw new IllegalArgumentException( "No '" + driver + "' driver available." );
}
}
public static Stream<WebDriver> getAll() {
return Arrays.stream( Driver.values() ).map( WebDriverFactory::get );
}
}
@beatngu13
Copy link
Author

@standbyoneself I just had a look at the Playwright docs. Are you aware that there is (experimental) Junit 5 support? Apparently, it does all the lifecycle management for you. And for cross-browser testing, maybe OptionsFactory can help?

@standbyoneself
Copy link

@beatngu13 I am using a custom fixture, so I am afraid of experimental fixtures, also i need to customize it

@standbyoneself
Copy link

Ended up with running scripts in parallel for local development

test.sh

#!/bin/sh

BROWSER=chromium mvn test & BROWSER=firefox mvn test & BROWSER=webkit mvn test & wait

PageTestFixtures.java

    private void updateAllureTestNameAndHistoryId(String baseName) {
        Allure.getLifecycle().updateTestCase(testResult -> {
            String name = String.format("%s: %s", baseName, testResult.getName());
            String historyId = UUID.nameUUIDFromBytes(name.getBytes()).toString();
            testResult.setName(name);
            testResult.setHistoryId(historyId);
        });
    }
    
    @BeforeAll
    public void launchBrowser() {
        playwright = Playwright.create();
        BrowserFactory browserFactory = new BrowserFactoryImpl(playwright);
        String browserName = System.getenv().getOrDefault("BROWSER", "chromium");
        browserType = browserFactory.createBrowserType(browserName);
        BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions();
        Optional<String> browserPath = Optional.ofNullable(System.getenv("BROWSER_PATH"));
        if (browserPath.isPresent()) {
            launchOptions.setExecutablePath(Paths.get(browserPath.get()));
        }
        browser = browserType.launch(launchOptions);
    }
    
    @BeforeEach
    public void createContextAndPage(TestInfo testInfo) {
        updateAllureTestNameAndHistoryId(String.format("%s %s", browserType.name(), browser.version()));
        context = browser.newContext(new Browser.NewContextOptions()
            .setBaseURL(System.getenv().getOrDefault("BASE_URL", "https://rmp-ift.sberbank.ru/lenta/"))
            .setIgnoreHTTPSErrors(true));
        page = context.newPage();

        try {
            String certificateName = getCertificateName(testInfo);
            List<Cookie> cookies = authClient.getSessionCookies(certificateName);
            context.addCookies(cookies);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Keeping consistent historyId allows to view retries in Allure Report

In CI/CD e.g. Jenkins we are gonna using Jenkins Matrix

Hope it'll be helpful for somebody

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment