Playwright & Java
Playwright for Java provides a powerful API for browser automation that can be used for end-to-end testing.
See the example below on how to use Java with Playwright to run tests on the TestingBot browser grid.
More information about Playwright for Java can be found on the Playwright Java documentation.
Installation
To get started, add the Playwright dependency to your Maven project:
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.42.0</version>
</dependency>
Or if you're using Gradle:
Now you can create a basic test script. Below is an example using JUnit 5:
package com.example;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PlaywrightTest {
static Playwright playwright;
static Browser browser;
Page page;
@BeforeAll
static void launchBrowser() {
// Create the capabilities object with authentication information
JsonObject capabilities = new JsonObject();
JsonObject tbOptions = new JsonObject();
tbOptions.addProperty("key", "YOUR_KEY");
tbOptions.addProperty("secret", "YOUR_SECRET");
capabilities.add("tb:options", tbOptions);
capabilities.addProperty("browserName", "chrome");
capabilities.addProperty("browserVersion", "latest");
capabilities.addProperty("platform", "WIN10");
// Create a custom browser type to connect to TestingBot
playwright = Playwright.create();
String wsEndpoint = "wss://cloud.testingbot.com/playwright?capabilities=" +
java.net.URLEncoder.encode(capabilities.toString(),
java.nio.charset.StandardCharsets.UTF_8);
browser = playwright.chromium().connect(new BrowserType.ConnectOptions()
.setWsEndpoint(wsEndpoint));
}
@AfterAll
static void closeBrowser() {
if (playwright != null) {
playwright.close();
playwright = null;
}
}
@BeforeEach
void createContextAndPage() {
BrowserContext context = browser.newContext();
page = context.newPage();
}
@AfterEach
void closeContext() {
if (page != null) {
// Take a screenshot after the test
page.screenshot(new Page.ScreenshotOptions()
.setPath(java.nio.file.Paths.get("screenshot-" +
java.util.UUID.randomUUID() + ".png")));
page.context().close();
page = null;
}
}
@Test
void shouldCheckPageTitle() {
page.navigate("https://testingbot.com/");
String title = page.title();
assertEquals("Cross Browser Testing and Mobile App Testing | TestingBot", title);
}
}
This example will use Playwright to connect to a Chrome browser in the TestingBot cloud.
It will open the TestingBot website, retrieve the title of the page and verify with JUnit assertions if the title is correct.
You can also use test frameworks like TestNG with Playwright, following a similar approach. Just adapt the test annotations according to your preferred test framework.
After each test, we'll take a screenshot and close the browser context to clean up resources.
Parallel Testing with Java
One of the great advantages of the TestingBot service is that you can run multiple tests simultaneously.
This drastically shortens the total duration of your test suite, as multiple tests will run concurrently.
Here's how you can set up parallel test execution using JUnit 5:
package com.example;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
// Enable parallel execution for the class
@Execution(ExecutionMode.CONCURRENT)
class ParallelPlaywrightTests {
// Helper method to set up the browser
private Browser setupBrowser(String browserName) {
// Create capabilities
JsonObject capabilities = new JsonObject();
JsonObject tbOptions = new JsonObject();
tbOptions.addProperty("key", "YOUR_KEY");
tbOptions.addProperty("secret", "YOUR_SECRET");
capabilities.add("tb:options", tbOptions);
capabilities.addProperty("browserName", browserName);
capabilities.addProperty("browserVersion", "latest");
// Create the playwright instance
Playwright playwright = Playwright.create();
String wsEndpoint = "wss://cloud.testingbot.com/playwright?capabilities=" +
java.net.URLEncoder.encode(capabilities.toString(),
java.nio.charset.StandardCharsets.UTF_8);
// Connect based on browser type
BrowserType browserType;
if (browserName.equals("chrome")) {
browserType = playwright.chromium();
} else if (browserName.equals("firefox")) {
browserType = playwright.firefox();
} else if (browserName.equals("webkit")) {
browserType = playwright.webkit();
} else {
throw new IllegalArgumentException("Unsupported browser: " + browserName);
}
return browserType.connect(new BrowserType.ConnectOptions()
.setWsEndpoint(wsEndpoint));
}
@Test
void testHomepageInChrome() {
// Each test manages its own browser instance
try (Playwright playwright = Playwright.create();
Browser browser = setupBrowser("chrome")) {
BrowserContext context = browser.newContext();
Page page = context.newPage();
try {
page.navigate("https://testingbot.com/");
String title = page.title();
assertEquals("Cross Browser Testing and Mobile App Testing | TestingBot", title);
page.screenshot(new Page.ScreenshotOptions()
.setPath(java.nio.file.Paths.get("chrome-homepage.png")));
} finally {
context.close();
}
}
}
@Test
void testPricingPageInFirefox() {
// Each test manages its own browser instance
try (Playwright playwright = Playwright.create();
Browser browser = setupBrowser("firefox")) {
BrowserContext context = browser.newContext();
Page page = context.newPage();
try {
page.navigate("https://testingbot.com/pricing");
String title = page.title();
assertEquals("Cross Browser Testing - Prices and plans available for test online.", title);
page.screenshot(new Page.ScreenshotOptions()
.setPath(java.nio.file.Paths.get("firefox-pricing.png")));
} finally {
context.close();
}
}
}
}
To enable parallel execution with JUnit 5, you need to configure it in your junit-platform.properties
file:
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent
With TestNG, you can configure parallel execution in your test XML file:
<suite name="Parallel Test Suite" parallel="methods" thread-count="4">
<test name="Playwright Tests">
<classes>
<class name="com.example.PlaywrightTest"/>
</classes>
</test>
</suite>
The examples above demonstrate how to run multiple tests concurrently against different browsers on the TestingBot infrastructure. Each test manages its own browser instance, allowing multiple tests to run in parallel.
Depending on how many tests you run in parallel, you can drastically shorten your total test duration and get faster feedback from your tests.