Skip to content

Commit 806f940

Browse files
Add docs for addLocatorStrategy (webdriverio#4960)
* add docs for addLocatorStrategy * PR feedback
1 parent 3feba29 commit 806f940

File tree

9 files changed

+62
-4
lines changed

9 files changed

+62
-4
lines changed

docs/Selectors.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,23 @@ browser.react$$('MyComponent') // returns the WebdriverIO Elements for the array
424424
```
425425

426426
**Note:** If you have multiple instances of `MyComponent` and you use `react$$` to select these fragment components, you will be returned an one-dimensional array of all the nodes. In other words, if you have 3 `<MyComponent />` instances, you will be returned an array with six WebdriverIO elements.
427+
428+
## Custom Selector Strategies
429+
430+
If your app requires a specific way to fetch elements you can define yourself a custom selector strategy that you can use with `custom$` and `custom$$`. For that register your strategy once in the beginning of the test:
431+
432+
```js
433+
browser.addLocatorStrategy('myCustomStrategy', (selector) => {
434+
return document.querySelectorAll(selector)
435+
})
436+
```
437+
438+
The use it by calling:
439+
440+
```js
441+
browser.url('https://webdriver.io')
442+
const pluginWrapper = browser.custom$$('myStrat', '.pluginWrapper')
443+
console.log(pluginWrapper.length) // 4
444+
```
445+
446+
**Note:** this only works in an web environment in which the [`execute`](/docs/api/browser/execute.html) command can be run.

packages/wdio-reporter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Then you need to extend your reporter with the main `@wdio/reporter` class:
2020
```js
2121
import Reporter from '@wdio/reporter'
2222

23-
export default MyCustomeReporter extends Reporter {
23+
export default MyCustomReporter extends Reporter {
2424
constructor () {
2525
super()
2626
// your custom logic if necessary

packages/wdio-runner/src/reporter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ export default class BaseReporter {
149149
* check if reporter was passed in from a file, e.g.
150150
*
151151
* ```js
152-
* const MyCustomeReporter = require('/some/path/MyCustomeReporter.js')
152+
* const MyCustomReporter = require('/some/path/MyCustomReporter.js')
153153
* export.config = {
154154
* //...
155155
* reporters: [
156-
* MyCustomeReporter, // or
157-
* [MyCustomeReporter, { custom: 'option' }]
156+
* MyCustomReporter, // or
157+
* [MyCustomReporter, { custom: 'option' }]
158158
* ]
159159
* //...
160160
* }

packages/wdio-sync/webdriverio-core.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,14 @@ declare namespace WebdriverIO {
592592
func: (origCommand: Function, ...args: any[]) => any,
593593
attachToElement?: boolean
594594
): void;
595+
596+
/**
597+
* create custom selector
598+
*/
599+
addLocatorStrategy(
600+
name: string,
601+
func: (elementFetchingMethod: (selector: string) => any) => void
602+
): void
595603

596604
/**
597605
* The `$$` command is a short way to call the [`findElements`](/docs/api/webdriver.html#findelements) command in order

packages/webdriverio/webdriverio-core-v5.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,14 @@ declare namespace WebdriverIO {
592592
func: (origCommand: Function, ...args: any[]) => any,
593593
attachToElement?: boolean
594594
): void;
595+
596+
/**
597+
* create custom selector
598+
*/
599+
addLocatorStrategy(
600+
name: string,
601+
func: (elementFetchingMethod: (selector: string) => any) => void
602+
): void
595603

596604
/**
597605
* The `$$` command is a short way to call the [`findElements`](/docs/api/webdriver.html#findelements) command in order

packages/webdriverio/webdriverio-core.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,14 @@ declare namespace WebdriverIO {
592592
func: (origCommand: Function, ...args: any[]) => any,
593593
attachToElement?: boolean
594594
): void;
595+
596+
/**
597+
* create custom selector
598+
*/
599+
addLocatorStrategy(
600+
name: string,
601+
func: (elementFetchingMethod: (selector: string) => any) => void
602+
): void
595603

596604
/**
597605
* The `$$` command is a short way to call the [`findElements`](/docs/api/webdriver.html#findelements) command in order

scripts/templates/webdriverio.tpl.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ declare namespace WebdriverIO {
248248
func: (origCommand: Function, ...args: any[]) => any,
249249
attachToElement?: boolean
250250
): void;
251+
252+
/**
253+
* create custom selector
254+
*/
255+
addLocatorStrategy(
256+
name: string,
257+
func: (elementFetchingMethod: (selector: string) => any) => void
258+
): void
251259
// ... browser commands ...
252260
}
253261

tests/typings/sync/sync.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ browser.touchAction(touchAction)
9292
// dragAndDrop
9393
ele.dragAndDrop(ele, 0)
9494

95+
// addLocatorStrategy
96+
browser.addLocatorStrategy('myStrat', () => {})
97+
9598
// selenium-standalone-service
9699
const config: WebdriverIO.Config = {
97100
skipSeleniumInstall: true

tests/typings/webdriverio/async.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ async function bar() {
121121

122122
// dragAndDrop
123123
await ele.dragAndDrop(ele, 0)
124+
125+
// addLocatorStrategy
126+
browser.addLocatorStrategy('myStrat', () => {})
124127
}
125128

126129
// selenium-standalone-service

0 commit comments

Comments
 (0)