-
-
Save beatngu13/8d5f60355d7fbef143198f020b1efca3 to your computer and use it in GitHub Desktop.
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 ); | |
} | |
} |
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
@ParameterizedTest(name = "{displayName}")
@Certificate("curuser-6")
@Issue("RMC-6335")
@TmsLink("RMC-T6354")
@ValueSource(strings = {"chromium","firefox"})
@DisplayName("Создание и удаление объекта с ролью \"Бизнес-администратор\"")
public void shouldCreateAndDeleteSubjectByFeedAdmin(String browser) {
setupBrowser(browser);
shouldCreateAndDeleteSubject();
}
Fixture
protected void setupBrowser(String browserType) {
playwright = Playwright.create();
if (browserType.equals("chromium")) {
browser = playwright.chromium().launch();
} else if (browserType.equals("firefox")) {
browser = playwright.firefox().launch();
} else {
throw new IllegalArgumentException();
}
createContextAndPage();
}
Really want to have ability to use BeforeEach/AfterAll annotations for better readability and non-verbose reusability
@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
Yes. Here is a class with tests:
And a fixture class: