playwright-test-commons - v0.8.0
    Preparing search index...

    Class BasePage<L>

    Base class for page objects. Page methods call these helpers (this.click, this.fill, ...) rather than raw Playwright, so waits stay consistent. Add a helper here only when every page could use it and it takes locators as arguments; anything with page-specific names belongs on the page object.

    export class LoginPage extends BasePage<LoginLocators> {
    constructor(page: Page) {
    super(page, loginLocators);
    }

    async login(email: string, password: string) {
    await this.fill(this.locators.emailInput, email);
    await this.fill(this.locators.passwordInput, password);
    await this.click(this.locators.loginButton);
    }
    }

    Type Parameters

    • L extends object = Record<string, Locator>

      the page's locators, usually ReturnType<typeof homeLocators>

    Index
    • Type Parameters

      • L extends object = Record<string, Locator>

        the page's locators, usually ReturnType<typeof homeLocators>

      Parameters

      • page: Page

        the Playwright page

      • OptionallocatorFactory: LocatorFactory<L>

        builds the page's locators; omit for a page with none

      Returns BasePage<L>

    locators: L

    The page's locators, built by the factory passed to the constructor.

    page: Page

    The Playwright page this object drives.

    • Waits for an element to be visible, then clicks it.

      Parameters

      • locator: Locator

        the element to click

      Returns Promise<void>

    • Clicks an element that opens a browser dialog, answers the dialog and returns what it said. If no dialog opens, it waits for the project's actionTimeout, or the test timeout when no actionTimeout is set (Playwright's default), then fails.

      Parameters

      • locator: Locator

        the element to click

      • action: DialogAction = "accept"

        accept (OK) or dismiss (Cancel). Default "accept"

      • OptionalpromptText: string

        the text to enter when accepting a prompt

      Returns Promise<DialogInfo>

      the dialog's type and message

      const dialog = await this.clickAndHandleDialog(this.locators.submitButton, "dismiss");
      
    • Clicks an element and waits for the response it triggers, for actions whose only visible result comes after a request (an item removed, a list refreshed).

      Parameters

      • locator: Locator

        the element to click

      • url: string | RegExp | ((response: Response) => boolean | Promise<boolean>)

        the response to wait for: a URL, a pattern, or a predicate

      Returns Promise<Response>

      the response

      await this.clickAndWaitForResponse(this.locators.removeButton(name), (r) => r.url().endsWith(`/delete_cart/${id}`));
      
    • Asserts a locator matches at least one element, e.g. that a results list isn't empty.

      Parameters

      • locator: Locator

        matches zero or more elements

      • Optionalmessage: string

        shown if the check fails

      • options: ExpectOptions = {}

        soft by default; see ExpectOptions

      Returns Promise<void>

    • Asserts how many elements a locator matches.

      Parameters

      • locator: Locator

        matches zero or more elements

      • count: number

        the expected number; 0 checks that none are on the page

      • Optionalmessage: string

        shown if the check fails

      • options: ExpectOptions = {}

        soft by default; see ExpectOptions

      Returns Promise<void>

    • Asserts the names of the files chosen in a file input.

      Parameters

      • locator: Locator

        the <input type="file">

      • names: string[]

        the expected file names, in order; [] for none

      • Optionalmessage: string

        shown if the check fails

      Returns Promise<void>

    • Asserts an element is hidden or not on the page.

      Parameters

      • locator: Locator

        the element to check

      • Optionalmessage: string

        shown if the check fails

      • options: ExpectOptions = {}

        soft by default; see ExpectOptions

      Returns Promise<void>

    • Asserts a form control fails the browser's own validation (required, type="email", ...), i.e. its validationMessage is not empty. The wording differs per browser and language, so it is never compared.

      Parameters

      • locator: Locator

        the input, select or textarea

      • Optionalmessage: string

        shown if the check fails

      • options: ExpectOptions = {}

        soft by default; see ExpectOptions

      Returns Promise<void>

    • Asserts (soft) an element's text.

      Parameters

      • locator: Locator

        the element, or elements, to check

      • text: string | RegExp | readonly (string | RegExp)[]

        the expected text; an array checks each matched element in order

      Returns Promise<void>

    • Asserts (soft) the page's title.

      Parameters

      • title: string | RegExp

        the full title, or a pattern

      Returns Promise<void>

    • Asserts (soft) the page's URL.

      Parameters

      • pattern: string | RegExp

        the full URL, or a pattern such as /[?&]q=shoes/

      Returns Promise<void>

    • Asserts a form control's current value.

      Parameters

      • locator: Locator

        the input, textarea or select

      • value: string | RegExp

        the expected value; "" for an empty field

      • Optionalmessage: string

        shown if the check fails

      • options: ExpectOptions = {}

        soft by default; see ExpectOptions

      Returns Promise<void>

    • Asserts an element is visible.

      Parameters

      • locator: Locator

        the element to check

      • Optionalmessage: string

        shown if the check fails

      • options: ExpectOptions = {}

        soft by default, so one run reports every failed check; see ExpectOptions

      Returns Promise<void>

    • Waits for a field to be editable, then replaces its value.

      Parameters

      • locator: Locator

        the input, textarea or editable element

      • value: string | number

        the text to enter; numbers are converted to text

      Returns Promise<void>

    • Returns the text of every element a locator matches. Does not wait: use it after the elements are known to be on the page.

      Parameters

      • locator: Locator

        matches zero or more elements

      Returns Promise<string[]>

      each element's visible text, trimmed

    • Waits for an element to be visible and returns its text.

      Parameters

      • locator: Locator

        the element to read

      Returns Promise<string>

      the element's visible text, trimmed

    • Navigates to a path relative to the project's baseURL. Waits for domcontentloaded, not load: a stalled third-party script (ads, analytics) can hold back the load event indefinitely, and web-first assertions wait for the elements a test needs anyway.

      Parameters

      • path: string = "/"

        e.g. "/login". Default "/"

      Returns Promise<Response | null>

      the main resource response, or null (see Playwright's page.goto)

    • From now on, dismisses every dialog the page opens and records its message, for checking later that none opened. Without a listener Playwright dismisses dialogs silently, so they would go unnoticed.

      Returns string[]

      a list that fills as dialogs open; assert it is empty when no dialog was expected

    • Reloads the page. Waits for domcontentloaded, like BasePage.goto.

      Returns Promise<Response | null>

      the main resource response, or null (see Playwright's page.reload)

    • The current query string, for checking URL state after a search or filter.

      Returns Record<string, string>

      e.g. { q: "shoes", page: "2" }

    • Selects a dropdown option by its visible label.

      Parameters

      • locator: Locator

        the <select> element

      • label: string

        the option's visible text

      Returns Promise<void>

    • Chooses files in a file input.

      Parameters

      • locator: Locator

        the <input type="file">

      • files: string | string[]

        one path or several; [] clears the input

      Returns Promise<void>

    • Waits for the page's load event. BasePage.goto only waits for domcontentloaded, so use this before acting on a control whose handler is bound by a script at the end of the page: until that script runs, the control is visible but a click does nothing.

      Returns Promise<void>