Last active
May 14, 2024 13:43
-
-
Save beatngu13/8d5f60355d7fbef143198f020b1efca3 to your computer and use it in GitHub Desktop.
Dead-simple cross-browser testing with Selenium and JUnit 5 parameterized tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} | |
} |
@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?
@beatngu13 I am using a custom fixture, so I am afraid of experimental fixtures, also i need to customize it
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
I have used @ParameterizedTest in couple with @ValueSource(strings = {"chromium","firefox"}), but I have to explicitly call setupBrowser() in each test, which is really annoying. Like that:
Test
Fixture
Really want to have ability to use BeforeEach/AfterAll annotations for better readability and non-verbose reusability