playwright/docs/src/writing-tests-python.md
Debbie O'Brien 54f7141877
docs: debug docs rework for simpler debug intro (#15836)
Co-authored-by: Max Schmitt <max@schmitt.mx>
2022-07-27 13:12:01 +02:00

3.9 KiB

id title
writing-tests Writing Tests

Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with auto-wait built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides an expect function to write assertions.

Take a look at the example test below to see how to write a test using web first assertions, locators and selectors.

import re
from playwright.sync_api import Page, expect


def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(
    page: Page, foo
):
    page.goto("https://playwright.dev/")

    # Expect a title "to contain" a substring.
    expect(page).to_have_title(re.compile("Playwright"))

    # create a locator
    get_started = page.locator("text=Get Started")

    # Expect an attribute "to be strictly equal" to the value.
    expect(get_started).to_have_attribute("href", "/docs/intro")

    # Click the get started link.
    get_started.click()

    # Expects the URL to contain intro.
    expect(page).to_have_url(re.compile(".*intro"))

Assertions

Playwright provides the expect function which will wait until the expected condition is met.

import re
from playwright.sync_api import expect

expect(page).to_have_title(re.compile("Playwright"))

Locators

Locators are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as .click .fill etc. Custom locators can be created with the [method: Page.locator] method.

from playwright.sync_api import expect

get_started = page.locator("text=Get Started")

expect(get_started).to_have_attribute("href", "/docs/installation")
get_started.click()

Selectors are strings that are used to create Locators. Playwright supports many different selectors like Text, CSS, XPath and many more. Learn more about available selectors and how to pick one in this in-depth guide.

from playwright.sync_api import expect

expect(page.locator("text=Installation")).to_be_visible()

Test Isolation

The Playwright Pytest plugin is based on the concept of test fixtures such as the built in page fixture, which is passed into your test. Pages are isolated between tests due to the Browser Context, which is equivalent to a brand new browser profile, where every test gets a fresh environment, even when multiple tests run in a single Browser.

from playwright.sync_api import Page

def test_basic_test(page: Page):
  # ...

Using Test Hooks

You can use various fixtures to execute code before or after your tests and to share objects between them. A function scoped fixture e.g. with autouse behaves like a beforeEach/afterEach. And a module scoped fixture with autouse behaves like a beforeAll/afterAll which runs before all and after all the tests.

import pytest
from playwright.sync_api import Page


@pytest.fixture(scope="function", autouse=True)
def before_each_after_each(page: Page):
    print("beforeEach")
    # Go to the starting url before each test.
    page.goto("https://playwright.dev/")
    yield
    print("afterEach")

def test_main_navigation(page: Page):
    # Assertions use the expect API.
    expect(page).to_have_url("https://playwright.dev/")

What's Next