In this section, we have covered the essential Playwright Java concepts that are required to get started with browser automation. We walked through browser setup and launch, creating browser contexts and pages, working with different types of locators, performing common user actions, and validating application behavior using assertions. These examples form a quick reference for writing stable and readable Playwright Java automation scripts.
1. Setup and Browser Launch
|
Action |
Code |
|
Create Playwright |
Playwright playwright = Playwright.create(); |
|
Launch Chromium |
Browser browser = playwright.chromium().launch(); |
|
Launch with options |
chromium.launch(new LaunchOptions().setHeadless(false)); |
|
Firefox |
playwright.firefox().launch(); |
|
WebKit |
playwright.webkit().launch(); |
|
Close browser |
browser.close(); playwright.close(); |
2. Browser Context & Page
|
Action |
Code |
|
New context |
BrowserContext context = browser.newContext(); |
|
New page |
Page page = context.newPage(); |
|
Close context |
context.close(); |
|
Navigate |
page.navigate("https://example.com"); |
|
Get title |
page.title(); |
|
Current URL |
page.url(); |
3. Locators
|
Locator Type |
Example |
|
CSS |
page.locator("#login"); |
|
Text |
page.locator("text=Login"); |
|
XPath |
page.locator("//button[@id='save']"); |
|
Role |
page.getByRole(AriaRole.BUTTON, new Options().setName("Submit")); |
|
Placeholder |
page.getByPlaceholder("Email"); |
|
Label |
page.getByLabel("Password"); |
|
nth element |
page.locator(".item").nth(2); |
|
Locator Type |
Example |
4. Basic Actions
|
Action |
Code |
|
Click |
locator.click(); |
|
Double click |
locator.dblclick(); |
|
Right click |
locator.click(new ClickOptions().setButton(MouseButton.RIGHT)); |
|
Fill input |
locator.fill("value"); |
|
Type (slow) |
locator.type("text"); |
|
Clear input |
locator.fill(""); |
|
Press key |
locator.press("Enter"); |
|
Hover |
locator.hover(); |
|
Focus |
locator.focus(); |
5. Assertions
|
Validation |
Code |
|
Visible |
locator.isVisible(); |
|
Hidden |
locator.isHidden(); |
|
Enabled |
locator.isEnabled(); |
|
Disabled |
locator.isDisabled(); |
|
Checked |
locator.isChecked(); |
|
Text |
locator.textContent(); |
|
Attribute |
locator.getAttribute("value"); |
6. Dropdowns and Select
|
Action |
Code |
|
Select by value |
locator.selectOption("IN"); |
|
Select by label |
locator.selectOption(new SelectOption().setLabel("India")); |
|
Select multiple |
locator.selectOption(new String[]{"A","B"}); |
7. Alerts, Dialogs & Popups
|
Action |
Code |
|
Accept alert |
page.onDialog(d -> d.accept()); |
|
Dismiss alert |
page.onDialog(d -> d.dismiss()); |
|
Get message |
dialog.message(); |
8. Frames & IFrames
|
Action |
Code |
|
Get frame |
Frame frame = page.frame("frameName"); |
|
Frame locator |
page.frameLocator("#frame").locator("button"); |
|
Count frames |
page.frames().size(); |
9. Multiple Tabs / Windows
|
Action |
Code |
|
Wait for new tab |
Page newPage = context.waitForPage(() -> locator.click()); |
|
All pages |
context.pages(); |
|
Bring to front |
page.bringToFront(); |
10. File Upload & Download
|
Action |
Code |
|
Upload file |
locator.setInputFiles(Paths.get("file.txt")); |
|
Upload multiple |
setInputFiles(new Path[]{...}); |
|
Download |
Download download = page.waitForDownload(() -> click()); |
|
File path |
download.path(); |
11. Screenshots & Videos
|
Action |
Code |
|
Page screenshot |
page.screenshot(new Page.ScreenshotOptions().setPath(path)); |
|
Element screenshot |
locator.screenshot(); |
|
Enable video |
browser.newContext(new Options().setRecordVideoDir(path)); |
12. Keyboard & Mouse
|
Action |
Code |
|
Key press |
page.keyboard().press("Control+A"); |
|
Type |
page.keyboard().type("text"); |
|
Mouse click |
page.mouse().click(x, y); |
|
Mouse move |
page.mouse().move(x, y); |
|
Action |
Code |
13. Waits (Use Sparingly)
|
Wait Type |
Code |
|
Timeout |
page.waitForTimeout(2000); |
|
Selector |
page.waitForSelector("#id"); |
|
URL |
page.waitForURL("**/login"); |