Skip to content

Commit d628ac1

Browse files
docs: add webdriverio-testing-library intro (testing-library#766)
1 parent 4c34f57 commit d628ac1

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
id: intro
3+
title: WebdriverIO Testing Library
4+
---
5+
6+
[`webdriverio-testing-library`][gh] allows the use of dom-testing-library
7+
queries in [WebdriverIO](https://webdriver.io/) for end-to-end web testing.
8+
9+
## Install
10+
11+
> Be sure to follow the
12+
> [WebdriverIO install & config instructions first](https://webdriver.io/docs/gettingstarted/)
13+
14+
then just
15+
16+
```
17+
npm install -D @testing-library/webdriverio
18+
```
19+
20+
- [webdriverio-testing-library on GitHub][gh]
21+
22+
## API
23+
24+
### `setupBrowser`
25+
26+
Accepts a WebdriverIO [browser object](https://webdriver.io/docs/browserobject)
27+
and returns dom-testing-library [queries](/docs/queries/about) modifed to return
28+
WebdriverIO elements like normal
29+
[selectors](https://webdriver.io/docs/selectors/). **All queries are async** and
30+
are bound to `document.body` by default.
31+
32+
```javascript
33+
import { setupBrowser } from '@testing-library/webdriverio'
34+
35+
it('can click button', async () => {
36+
const { getByText } = setupBrowser(browser)
37+
38+
const button = await getByText('Button Text')
39+
await button.click()
40+
41+
expect(await button.getText()).toEqual('Button Clicked')
42+
})
43+
```
44+
45+
`setupBrowser` also adds queries as commands to the browser and to all
46+
WebdriverIO elements. The browser commands are scoped to `document.body`. The
47+
element commands are scoped to the element in the same way as if it was passed
48+
to [`within`](#within).
49+
50+
```javascript
51+
it('adds queries as browser commands', async () => {
52+
setupBrowser(browser)
53+
54+
expect(await browser.getByText('Page Heading')).toBeDefined()
55+
})
56+
57+
it('adds queries as element commands scoped to element', async () => {
58+
setupBrowser(browser)
59+
60+
const nested = await browser.$('*[data-testid="nested"]')
61+
const button = await nested.getByText('Button Text')
62+
await button.click()
63+
64+
expect(await button.getText()).toEqual('Button Clicked')
65+
})
66+
```
67+
68+
When using [sync mode](https://webdriver.io/docs/sync-vs-async#sync-mode) these
69+
commands are also synchronous:
70+
71+
```javascript
72+
it('adds queries as browser commands', async () => {
73+
setupBrowser(browser)
74+
75+
expect(browser.getByText('Page Heading')).toBeDefined()
76+
})
77+
78+
it('adds queries as element commands scoped to element', async () => {
79+
setupBrowser(browser)
80+
81+
const nested = browser.$('*[data-testid="nested"]')
82+
const button = nested.getByText('Button Text')
83+
button.click()
84+
85+
expect(button.getText()).toEqual('Button Clicked')
86+
})
87+
```
88+
89+
### `within`
90+
91+
Accepts a WebdriverIO element and returns queries scoped to that element
92+
93+
```javascript
94+
import { within } from '@testing-library/webdriverio'
95+
96+
it('within scopes queries to element', async () => {
97+
const nested = await browser.$('*[data-testid="nested"]')
98+
99+
const button = await within(nested).getByText('Button Text')
100+
await button.click()
101+
102+
expect(await button.getText()).toEqual('Button Clicked')
103+
})
104+
```
105+
106+
### `configure`
107+
108+
Lets you [configure dom-testing-library][config]
109+
110+
```javascript
111+
import { configure } from '@testing-library/webdriverio'
112+
113+
beforeEach(() => {
114+
configure({ testIdAttribute: 'data-automation-id' })
115+
})
116+
afterEach(() => {
117+
configure(null)
118+
})
119+
120+
it('lets you configure queries', async () => {
121+
const { getByTestId } = setupBrowser(browser)
122+
123+
expect(await getByTestId('testid-in-data-automation-id-attr')).toBeDefined()
124+
})
125+
```
126+
127+
## Typescript
128+
129+
This library comes with full typescript definitions. To use the commands added
130+
by [`setupBrowser`](#setupbrowser) the `Browser` and `Element` interfaces in the
131+
global `WebdriverIO` namespace need to be extended. Add the following to a
132+
typescript module:
133+
134+
```typescript
135+
import { WebdriverIOQueries } from '@testing-library/webdriverio'
136+
137+
declare global {
138+
namespace WebdriverIO {
139+
interface Browser extends WebdriverIOQueries {}
140+
interface Element extends WebdriverIOQueries {}
141+
}
142+
}
143+
```
144+
145+
If you are using the `@wdio/sync` package you will need to use the
146+
`WebdriverIOQueriesSync` type to extend the interfaces:
147+
148+
```typescript
149+
import { WebdriverIOQueriesSync } from '@testing-library/webdriverio'
150+
151+
declare global {
152+
namespace WebdriverIO {
153+
interface Browser extends WebdriverIOQueriesSync {}
154+
interface Element extends WebdriverIOQueriesSync {}
155+
}
156+
}
157+
```
158+
159+
Additional information about using typescript with WebdriverIO can be found
160+
[here](https://webdriver.io/docs/typescript/)
161+
162+
[config]: ../dom-testing-library/api-configuration.mdx
163+
[gh]: https://github.com/testing-library/webdriverio-testing-library

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ module.exports = {
166166
'pptr-testing-library/intro',
167167
'testcafe-testing-library/intro',
168168
'nightwatch-testing-library/intro',
169+
'webdriverio-testing-library/intro',
169170
],
170171
},
171172
{

0 commit comments

Comments
 (0)