Skip to content

Commit 6588bc6

Browse files
docs: add wdio testing library chainable queries (#1167)
1 parent f8c520a commit 6588bc6

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

docs/webdriverio-testing-library/intro.mdx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,44 @@ it('adds queries as element commands scoped to element', async () => {
8686
})
8787
```
8888

89+
When using v7.19 or higher of WebdriverIO you can also use chainable queries.
90+
Chainable queries are added to the browser and element as commands with the
91+
format `{query}$`.
92+
93+
```javascript
94+
it('can chain browser getBy queries', async () => {
95+
setupBrowser(browser)
96+
97+
await browser.getByTestId$('nested').getByText$('Button Text').click()
98+
99+
const buttonText = await browser
100+
.getByTestId$('nested')
101+
.getByText$('Button Text')
102+
.getText()
103+
104+
expect(buttonText).toEqual('Button Clicked')
105+
})
106+
107+
it('can chain element getBy queries', async () => {
108+
const {getByTestId} = setupBrowser(browser)
109+
110+
const nested = await getByTestId('nested')
111+
await nested.getByText$('Button Text').click()
112+
113+
const buttonText = await browser.getByText$('Button Clicked').getText()
114+
115+
expect(buttonText).toEqual('Button Clicked')
116+
})
117+
118+
it('can chain getAllBy queries', async () => {
119+
setupBrowser(browser)
120+
121+
await browser.getByTestId$('nested').getAllByText$('Button Text')[0].click()
122+
123+
expect(await browser.getAllByText('Button Clicked')).toHaveLength(1)
124+
})
125+
```
126+
89127
### `within`
90128

91129
Accepts a WebdriverIO element and returns queries scoped to that element
@@ -156,6 +194,29 @@ declare global {
156194
}
157195
```
158196

197+
To add chainable query types you need to extend the above interfaces as well as
198+
`ChainablePromiseElement` with `WebdriverIOQueriesChainable`:
199+
200+
```typescript
201+
import {WebdriverIOQueriesChainable, WebdriverIOQueries} from '../../src'
202+
203+
declare global {
204+
namespace WebdriverIO {
205+
interface Browser
206+
extends WebdriverIOQueries,
207+
WebdriverIOQueriesChainable<Browser> {}
208+
interface Element
209+
extends WebdriverIOQueries,
210+
WebdriverIOQueriesChainable<Element> {}
211+
}
212+
}
213+
214+
declare module 'webdriverio' {
215+
interface ChainablePromiseElement<T extends WebdriverIO.Element | undefined>
216+
extends WebdriverIOQueriesChainable<T> {}
217+
}
218+
```
219+
159220
If you are finding an error similar to this:
160221

161222
```typescript

0 commit comments

Comments
 (0)