Playwright Java Usage
1 min
The framework-independent base implementation is shown below. This structure is also the foundation for the Cucumber and Gauge integrations.
import com.microsoft.playwright.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TraceExample {
public static void main(String[] args) {
Path tracePath = Paths.get("test-results", "trace-example.zip");
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(true)
);
BrowserContext context = browser.newContext();
// Tracing must start before creating the Page.
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true)
.setSources(true));
try {
Page page = context.newPage();
page.navigate("https://playwright.dev");
page.getByRole(AriaRole.LINK,
new Page.GetByRoleOptions().setName("Get started")).click();
} finally {
context.tracing().stop(new Tracing.StopOptions()
.setPath(tracePath));
context.close();
browser.close();
}
}
}
}