0% found this document useful (0 votes)
15 views4 pages

Cheat Sheet Playwright Java

The document provides a comprehensive guide on setting up and using Playwright for browser automation, covering essential classes like Playwright, Browser, and Page. It details various actions such as navigating to URLs, interacting with elements, handling popups, and managing screenshots and videos. Additionally, it explains how to handle dropdowns, frames, and browser contexts for multi-page testing, along with methods for waiting for elements and closing resources.

Uploaded by

Karthik Perfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Cheat Sheet Playwright Java

The document provides a comprehensive guide on setting up and using Playwright for browser automation, covering essential classes like Playwright, Browser, and Page. It details various actions such as navigating to URLs, interacting with elements, handling popups, and managing screenshots and videos. Additionally, it explains how to handle dropdowns, frames, and browser contexts for multi-page testing, along with methods for waiting for elements and closing resources.

Uploaded by

Karthik Perfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Setting Up Playwright

import com.microsoft.playwright.*;

public class PlaywrightSetup {

public static void main(String[] args) {

Playwright playwright = Playwright.create();

Browser browser = playwright.chromium().launch(new


BrowserType.LaunchOptions().setHeadless(false));

BrowserContext context = browser.newContext();

Page page = context.newPage();

page.navigate("https://example.com");

browser.close();

2. Basic Classes

• Playwright: The main entry point to start using Playwright.

• Browser: Represents a browser instance (Chromium, Firefox, Webkit).

• BrowserContext: Isolates cookies, localStorage, and sessionStorage for each


browser context.

• Page: Represents a single tab or page in the browser.

3. Selectors

Playwright uses CSS selectors for locating elements on the page.

• By CSS Selector: page.locator("css=selector")

• By XPath: page.locator("xpath=//div[@class='example']")

• By Text: page.locator("text=Some Text")

• By Role: page.locator("role=button[name='submit']")

4. Common Actions

• Navigating to a URL:
page.navigate("https://example.com");

• Clicking on an element:

page.locator("button#submit").click();

• Typing into an input field:

page.locator("input#username").type("myusername");

• Filling in a form:

page.locator("input[name='username']").fill("user1");

page.locator("input[name='password']").fill("password123");

page.locator("button[type='submit']").click();

• Waiting for an element to be visible:

page.locator("div#result").waitFor(new
Locator.WaitForOptions().setState(Locator.WaitForState.VISIBLE));

• Extracting text from an element:

String text = page.locator("h1").textContent();

5. Assertions

Using PlaywrightAssertions to verify conditions.

• Assert visibility:

page.locator("button#submit").isVisible();

• Assert text content:

page.locator("h1").innerText().equals("Welcome");

• Assert element is not visible:

page.locator("div#hidden").isHidden();

6. Handling Popups and Alerts

• Accepting a prompt:

page.onDialog(dialog -> dialog.accept());

page.locator("button#trigger-prompt").click();

• Handling an alert:

page.onDialog(dialog -> {dialog.accept();});


page.locator("button#alert-trigger").click();

7. Screenshots & Videos

• Take a screenshot:

page.screenshot(new
Page.ScreenshotOptions().setPath(Paths.get("screenshot.png")));

• Record video:

BrowserContext context = browser.newContext(new


Browser.NewContextOptions().setRecordVideoDir(Paths.get("videos")));

8. Mouse and Keyboard Actions

• Clicking using mouse:

page.mouse().click(100, 200); // Click at x=100, y=200 coordinates

• Moving the mouse:

page.mouse().move(200, 300);

• Keyboard Input:

page.keyboard().type("Hello Playwright!");

page.keyboard().press("Enter");

9. Handling Dropdowns (Selects)

• Selecting an option from a dropdown:

page.locator("select#dropdown").selectOption(new
SelectOption().setLabel("Option 1"));

10. Handling Frames

• Accessing an iframe:

Frame frame = page.frameLocator("iframe#frame");

frame.locator("button#submit").click();

11. Browser Contexts & Multi-Page Testing

• Creating a new browser context:

javaBrowserContext context = browser.newContext();

• Opening multiple pages:

Page page1 = context.newPage();


Page page2 = context.newPage();

12. Waiting for Elements

• Wait for an element to appear:

page.locator("button#submit").waitFor();

• Wait for the page to load:

page.waitForLoadState(LoadState.DOMCONTENTLOADED);

13. Closing Resources

• Closing the browser:

browser.close();

• Closing a page:

page.close();s

Common Classes:

• Locator: Represents a single or multiple elements on the page. Used for actions
like click, fill, textContent, etc.

• Page: Represents the browser page and includes methods for interacting with
elements, navigation, and other actions.

• Browser: The browser itself (e.g., Chromium, Firefox, Webkit).

• BrowserContext: Manages the session (cookies, localStorage) of a page.

• Frame: Used to interact with iframes on a page.

• Dialog: Represents popups like alert, prompt, and confirm dialogs.

• Request: Represents HTTP requests made by the page.

• Response: Represents the response received from an HTTP request.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy