AutomateBrowserPlaywright
First test in Java
A complete Playwright test in Java you can copy, run, and watch on a real browser.
One file. Copy it, put your access key in, run it, and watch it happen in your dashboard.
What you need
Java 11 or newer, and one dependency:
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.55.0</version>
</dependency>Nothing of ours to install. This is the ordinary Playwright library.
The whole test
import com.microsoft.playwright.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class FirstTest {
static final String ACCESS_KEY = "xyz_your_access_key_here";
static final String TEST_NAME = "example.com opens";
public static void main(String[] args) {
String capabilities = """
{
"browserName": "chrome",
"browserVersion": "latest",
"testkase:options": {
"accessKey": "%s",
"platformName": "windows 11",
"name": "%s",
"build": "first-test",
"runId": "%s",
"recording": true
}
}
""".formatted(ACCESS_KEY, TEST_NAME, UUID.randomUUID());
String ws = "wss://hub.testkase.com/playwright?capabilities="
+ URLEncoder.encode(capabilities, StandardCharsets.UTF_8);
try (Playwright pw = Playwright.create()) {
Browser browser = pw.chromium().connect(ws);
Page page = browser.newPage();
page.navigate("https://example.com");
String title = page.title();
if (!title.contains("Example Domain")) {
throw new AssertionError("wrong title: " + title);
}
System.out.println(" title: " + title);
browser.close();
System.out.println("PASS " + TEST_NAME);
}
}
}What each part does
Six things carry the whole file. Read down the middle column and you have its shape.
| The part | What it does | |
|---|---|---|
| 1 | wss://hub.testkase.com/playwright?capabilities=… | Where everything goes. Playwright has no capability argument, so the whole object is URL-encoded onto the connect address. |
| 2 | accessKey | Signs you in. Copy it from the Access key page. There is no username here — the key is the only credential. |
| 3 | platformName | Which operating system. Set it. Leave it out and Chrome, Edge and Firefox fall back to linux, which no machine of ours runs, so the session is refused. |
| 4 | recording: true | Says what to keep. name is how you will recognise the session in a list; recording is the video, and you get none unless you ask. Every option. |
| 5 | pw.chromium().connect(ws) | Ordinary Playwright from here on. Every command behaves exactly as it does locally — we only carry it to the browser and back. You call chromium.connect whichever brand you asked for; the address decides what you get. |
| 6 | browser.close() | Ends the session and frees the machine. Forget it and the session runs until a timeout stops it — which you are paying for. |
Run it
mvn -q compile exec:java -Dexec.mainClass=FirstTestThe browser opens on our machines, not yours. Open your dashboard while it runs and the session is there live; when it ends, the video is on it.
