playwright-testing-library
playwright + @testing-library/dom =
All your favorite user-centric querying functions from @testing-library/react & @testing-library/library available from Playwright!
β οΏ½? Note: this is a fork of pptr-testing-library modified to accomodate for some subtle API differences.
Contents
Install
npm install --save-dev playwright-testing-library
Use
const {webkit} = require('playwright') // or 'firefox' or 'chromium'
const {getDocument, queries, waitFor} = 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('[email protected]')
// waiting works too!
await waitFor(() => getByText($document, 'Loading...'))A little too un-playwright for you? We've got prototype-mucking covered too :)
const {webkit} = require('playwright') // or 'firefox' or 'chromium'
require('playwright-testing-library/extend')
const browser = await webkit.launch()
const page = await browser.newPage()
// getDocument is added to prototype of Page
const $document = await page.getDocument()
// query methods are added directly to prototype of ElementHandle
const $form = await $document.getByTestId('my-form')
// destructing works if you explicitly call getQueriesForElement
const {getByText} = $form.getQueriesForElement()
// ...API
Unique methods, not part of @testing-library/dom
getDocument(page: playwright.Page): ElementHandle- get an ElementHandle for the documentwait(conditionFn: () => {}): Promise<{}>- wait for the condition to not throw (wrapper aroundwaitForExpect)
@testing-library/dom API. All get*/query* methods are supported.
getQueriesForElement(handle: ElementHandle): ElementHandle & QueryUtils- extend the input object with the query API and return itgetNodeText(handle: ElementHandle): Promise<string>- get the text content of the elementqueries: QueryUtils- the query subset of@testing-library/domexportsqueryByPlaceholderTextqueryAllByPlaceholderTextgetByPlaceholderTextgetAllByPlaceholderTextfindByPlaceholderTextfindAllByPlaceholderTextqueryByTextqueryAllByTextgetByTextgetAllByTextfindByTextfindAllByTextqueryByLabelTextqueryAllByLabelTextgetByLabelTextgetAllByLabelTextfindByLabelTextfindAllByLabelTextqueryByAltTextqueryAllByAltTextgetByAltTextgetAllByAltTextfindByAltTextfindAllByAltTextqueryByTestIdqueryAllByTestIdgetByTestIdgetAllByTestIdfindByTestIdfindAllByTestIdqueryByTitlequeryAllByTitlegetByTitlegetAllByTitlefindByTitlefindAllByTitlequeryByDisplayValue,queryAllByDisplayValue,getByDisplayValue,getAllByDisplayValue,findByDisplayValue,findAllByDisplayValue,
Known Limitations
- Async utilities
waitForElement,waitForElementToBeRemovedandwaitForDomChangeare not exposed. Consider using afind*query. fireEventmethod is not exposed, use Playwright's built-ins instead.expectassertion extensions are not available.
Special Thanks
- pptr-testing-library
- @testing-library/dom of course!
Related Playwright Test Utilities
- jest-playwright
- expect-playwright
- Yours! Name TBD, PR welcome ;)
LICENSE
MIT