|
29 | 29 |
|
30 | 30 | All of your favorite user-centric querying functions from **@testing-library/react** and **@testing-library/dom** available from Playwright! |
31 | 31 |
|
32 | | -- Standalone queries — **`playwright-testing-library`** or... |
33 | | -- Playwright extensions — **`playwright-testing-library/extend`** |
| 32 | +- Playwright Test [fixture](https://playwright.dev/docs/test-fixtures) — **`@playwright-testing-library/test/fixture`** or... |
| 33 | +- Standalone queries — **`playwright-testing-library`**/**`@playwright-testing-library/test`** |
34 | 34 | - Asynchronous assertion helper (via **[wait-for-expect](https://github.com/TheBrainFamily/wait-for-expect)**) |
35 | 35 |
|
36 | 36 | ## 🌱 Getting Started |
|
47 | 47 | yarn add --dev playwright-testing-library |
48 | 48 | ``` |
49 | 49 |
|
50 | | -### 2a. Use _standalone queries_ |
| 50 | +### 2a. Use _Playwright Test [fixture](https://playwright.dev/docs/test-fixtures)_ |
51 | 51 |
|
52 | | -```js |
53 | | -const {webkit} = require('playwright') // or 'firefox' or 'chromium' |
54 | | -const {getDocument, queries} = require('playwright-testing-library') |
| 52 | +```ts |
| 53 | +import {test as baseTest} from '@playwright/test' |
| 54 | +import {fixtures, TestingLibraryFixtures} from '@playwright-testing-library/test/fixture' |
55 | 55 |
|
56 | | -const {getByTestId, getByLabelText} = queries |
| 56 | +// As only fixture |
| 57 | +const test = baseTest.extend<TestingLibraryFixtures>(fixtures) |
57 | 58 |
|
58 | | -const browser = await webkit.launch() |
59 | | -const page = await browser.newPage() |
| 59 | +// Alternatively, with other fixtures |
| 60 | +interface Fixtures extends TestingLibraryFixtures { |
| 61 | + // ... additional fixture types |
| 62 | +} |
60 | 63 |
|
61 | | -// Grab ElementHandle for document |
62 | | -const $document = await getDocument(page) |
| 64 | +const test = baseTest.extend<Fixtures>({ |
| 65 | + ...fixtures, |
| 66 | + // ... additional fixtures |
| 67 | +}) |
63 | 68 |
|
64 | | -// Your favorite query methods are available |
65 | | -const $form = await getByTestId($document, 'my-form') |
| 69 | +const {expect} = test |
66 | 70 |
|
67 | | -// Returned elements are ElementHandles too! |
68 | | -const $email = await getByLabelText($form, 'Email') |
| 71 | +// Query methods are available in `test` blocks |
| 72 | +test('my form', ({queries: {getByTestId}}) => { |
| 73 | + const $form = await getByTestId('my-form') |
69 | 74 |
|
70 | | -// Interact with playwright like usual |
71 | | -await $email.type('playwright@example.com') |
| 75 | + // Scope queries with `getQueriesForElement` |
| 76 | + const {getByLabelText} = $form.getQueriesForElement() |
72 | 77 |
|
73 | | -// ... |
| 78 | + const $email = await getByLabelText('Email') |
| 79 | + |
| 80 | + // Interact with Playwright like usual |
| 81 | + await $email.type('playwright@example.com') |
| 82 | + |
| 83 | + expect($email).toHaveValue('playwright@example.com') |
| 84 | + |
| 85 | + // ... |
| 86 | +}) |
74 | 87 | ``` |
75 | 88 |
|
76 | | -### 2b. Use _extensions_ |
| 89 | +### 2b. Use _standalone queries_ |
77 | 90 |
|
78 | 91 | ```js |
79 | 92 | const {webkit} = require('playwright') // or 'firefox' or 'chromium' |
80 | | -require('playwright-testing-library/extend') |
| 93 | +const {getDocument, queries} = require('playwright-testing-library') |
| 94 | + |
| 95 | +const {getByTestId, getByLabelText} = queries |
81 | 96 |
|
82 | 97 | const browser = await webkit.launch() |
83 | 98 | const page = await browser.newPage() |
84 | 99 |
|
85 | | -// Grab document with `getDocument`, which is added to the prototype of `Paqe` |
86 | | -const $document = await page.getDocument() |
87 | | - |
88 | | -// Query methods are added directly to prototype of `ElementHandle` |
89 | | -const $form = await $document.getByTestId('my-form') |
| 100 | +// Grab ElementHandle for document |
| 101 | +const $document = await getDocument(page) |
90 | 102 |
|
91 | | -// Scope queries with `getQueriesForElement` |
92 | | -const {getByLabelText} = $form.getQueriesForElement() |
| 103 | +// Your favorite query methods are available |
| 104 | +const $form = await getByTestId($document, 'my-form') |
93 | 105 |
|
94 | | -const $email = await getByLabelText('Email') |
| 106 | +// Returned elements are ElementHandles too! |
| 107 | +const $email = await getByLabelText($form, 'Email') |
95 | 108 |
|
96 | | -// Interact with Playwright like usual |
| 109 | +// Interact with playwright like usual |
97 | 110 | await $email.type('playwright@example.com') |
98 | 111 |
|
99 | 112 | // ... |
|
0 commit comments