🎭️ Playwright

Updated at 2024-06-09 03:15

Playwright is an end-to-end testing library. It allows you to write tests that interact with your web application in a browser to ensure that it works as expected.

You can use JavaScript, Python, Java or C#. I've used it with Python so the examples are in Python.


# go to a page and wait for everything to load

# do something and assume a certain URL change
page.get_by_text("Let me in!").click()

# but, in general, Playwright is smart enough to wait for the element to load
page.get_by_text("Let me in!").click()
expect(page.get_by_role("heading", name="Welcome!")).to_be_visible()

Do not use with page.expect_navigation() as it's not reliable. Use page.wait_for_url.


Finding Elements

You use get_by_* and locate functions to find elements on the page.

  1. For form fields, use label > placeholder > testid
  2. For images, use alt_text > testid
  3. For everything else, use role+name > text > testid.
# etc.

Avoid locating elements with CSS or XPath. They are the least resilient and can break on unrelated DOM changes.

You use filter to narrow down the elements you're looking for.

    has_text="Product 2"
    "button", name="Add to cart"

You can also filter with "one that has a child like this".

    has=page.get_by_role("heading", name="Product 2")
    name="Add to cart"

Performing Actions


page.get_by_label('I agree').check()
expect(page.get_by_label('Subscribe to newsletter')).to_be_checked()

page.get_by_label('Choose a color').select_option('blue')

# the name here is "the accessible name" of the element
page.get_by_role("button", name="Submit", exact=True).click()
page.get_by_role("button", name=re.compile("submit", re.IGNORECASE)).click()

Checking Assumptions

expect(page.get_by_role("heading", name="Welcome!")).to_be_visible()

expect(page.get_by_role("button", name="Submit")).not_to_be_disabled()

expect(page.locator("list > .component")).to_have_count(3)

expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text 3"])

Browser Context

Each test runs in a separate browser context. A kin to an incognito tab in a browser.

You can emulate a lot with the context like setting viewport, timezone, geolocation, permissions, etc.

When you notice that registration and login per test becomes slow, use context.storage_state. It snapshots cookies and local storage so you can restore them later.

# Save storage state into the file.
storage = context.storage_state(path="playwright/.auth/state.json")


# Create a new context with the saved storage state.
context = browser.new_context(storage_state="playwright/.auth/state.json")

You'd normally use playwright/.auth directory to save this.