Skip to content

testing-library/playwright-testing-library

Repository files navigation



playwright-testing-library

πŸ” Find elements in playwright like your users with queries from @testing-library/dom

Package Version Build Status Test Coverage Dependencies Status MIT License
Code Style Conventional Commits Maintenance


✨ Features

⚠️ Note: this is a fork of pptr-testing-library modified to accomodate for some subtle API differences.

All of your favorite user-centric querying functions from @testing-library/react and @testing-library/dom available from Playwright!

  • Standalone queries β€” playwright-testing-library or...
  • Playwright extensions β€” playwright-testing-library/extend
  • Asynchronous assertion helper (via wait-for-expect)

🌱 Getting Started

1. Install

npm install --save-dev playwright-testing-library

or

yarn add --dev playwright-testing-library

2a. Use standalone queries

const {webkit} = require('playwright') // or 'firefox' or 'chromium'
const {getDocument, queries} = require('playwright-testing-library')

const {getByTestId, getByLabelText} = queries

const browser = await webkit.launch()
const page = await browser.newPage()

// Grab ElementHandle for document
const $document = await getDocument(page)

// Your favorite query methods are available
const $form = await getByTestId($document, 'my-form')

// Returned elements are ElementHandles too!
const $email = await getByLabelText($form, 'Email')

// Interact with playwright like usual
await $email.type('playwright@example.com')

// ...

2b. Use extensions

const {webkit} = require('playwright') // or 'firefox' or 'chromium'
require('playwright-testing-library/extend')

const browser = await webkit.launch()
const page = await browser.newPage()

// Grab document with `getDocument`, which is added to the prototype of `Paqe`
const $document = await page.getDocument()

// Query methods are added directly to prototype of `ElementHandle`
const $form = await $document.getByTestId('my-form')

// Scope queries with `getQueriesForElement`
const {getByLabelText} = $form.getQueriesForElement()

const $email = await getByLabelText('Email')

// Interact with Playwright like usual
await $email.type('playwright@example.com')

// ...

πŸ”Œ API

Unique methods, not part of @testing-library/dom

  • Get an ElementHandle for the document

    getDocument(page: playwright.Page): ElementHandle
  • Wait for an assertion (wrapper around wait-for-expect)

    waitFor(
      expectation: () => void | Promise<void>,
      timeout?: number,
      interval?: number
    ): Promise<{}>

The @testing-library/dom β€” All get* and query* methods are supported.

  • getQueriesForElement(handle: ElementHandle): ElementHandle & QueryUtils - extend the input object with the query API and return it
  • getNodeText(handle: ElementHandle): Promise<string> - get the text content of the element
  • queries: QueryUtils - the query subset of @testing-library/dom exports
    • queryByPlaceholderText
    • queryAllByPlaceholderText
    • getByPlaceholderText
    • getAllByPlaceholderText
    • findByPlaceholderText
    • findAllByPlaceholderText
    • queryByText
    • queryAllByText
    • getByText
    • getAllByText
    • findByText
    • findAllByText
    • queryByLabelText
    • queryAllByLabelText
    • getByLabelText
    • getAllByLabelText
    • findByLabelText
    • findAllByLabelText
    • queryByAltText
    • queryAllByAltText
    • getByAltText
    • getAllByAltText
    • findByAltText
    • findAllByAltText
    • queryByTestId
    • queryAllByTestId
    • getByTestId
    • getAllByTestId
    • findByTestId
    • findAllByTestId
    • queryByTitle
    • queryAllByTitle
    • getByTitle
    • getAllByTitle
    • findByTitle
    • findAllByTitle
    • queryByDisplayValue,
    • queryAllByDisplayValue,
    • getByDisplayValue,
    • getAllByDisplayValue,
    • findByDisplayValue,
    • findAllByDisplayValue,

Known Limitations

  • Async utilities waitForElement, waitForElementToBeRemoved and waitForDomChange are not exposed. Consider using a find* query.
  • fireEvent method is not exposed, use Playwright's built-ins instead.
  • expect assertion extensions are not available.

Special Thanks

Related Playwright Test Utilities

LICENSE

MIT

About

πŸ” Find elements in Playwright with queries from Testing Library

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors 6