Skip to content

Commit 2c0cdb8

Browse files
authored
Merge pull request #37 from Georgegriff/webdriver
web driver io custom locator strategy
2 parents 13cfb71 + ff31d79 commit 2c0cdb8

File tree

6 files changed

+1169
-121
lines changed

6 files changed

+1169
-121
lines changed

README.md

+49-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,57 @@ import { querySelectorAllDeep, querySelectorDeep } from 'query-selector-shadow-d
1717

1818
Both of the methods above accept a 2nd parameter, see section `Provide alternative node`. This will change the starting element to search from i.e. it will find ancestors of that node that match the query.
1919

20-
## Integrations
20+
## Plugins
2121

22-
### CodeceptJS
22+
### WebdriverIO
23+
This plugin implements a custom selector strategy: https://webdriver.io/docs/selectors.html#custom-selector-strategies
2324

24-
More details here: https://github.com/Georgegriff/query-selector-shadow-dom/blob/master/plugins/codeceptjs
25+
```javascript
26+
27+
// make sure you have selenium standalone running
28+
const { remote } = require('webdriverio');
29+
const { locatorStrategy } = require('query-selector-shadow-dom/plugins/webdriverio');
30+
31+
(async () => {
32+
const browser = await remote({
33+
logLevel: 'error',
34+
path: '/wd/hub',
35+
capabilities: {
36+
browserName: 'chrome'
37+
}
38+
})
39+
40+
// All elements on the page
41+
await browser.waitUntil(() => browser.custom$("shadow", ".btn-in-shadow-dom"));
42+
const elements = await browser.$$("*");
43+
const elementsShadow = await browser.custom$$("shadow", "*");
44+
console.log("All Elements on Page Excluding Shadow Dom", elements.length);
45+
console.log("All Elements on Page Including Shadow Dom", elementsShadow.length);
46+
47+
// registry custom strategy
48+
browser.addLocatorStrategy('shadow', locatorStrategy);
49+
await browser.url('http://127.0.0.1:5500/test/')
50+
// find input element in shadow dom
51+
const input = await browser.custom$('shadow', '#type-to-input');
52+
// type to input ! Does not work in firefox, see above.
53+
await input.setValue('Typed text to input');
54+
// Firefox workaround
55+
// await browser.execute((input, val) => input.value = val, input, 'Typed text to input')
56+
57+
await browser.deleteSession()
58+
})().catch((e) => console.error(e))
59+
```
60+
61+
#### Known issues
62+
- https://webdriver.io/blog/2019/02/22/shadow-dom-support.html#browser-support
63+
64+
- From the above, firefox `setValue` does NOT currently work.
65+
`. A workaround for now is to use a custom command (or method on your component object) that sets the input field's value via browser.execute(function).`
66+
67+
- Safari pretty much doesn't work, not really a surprise.
68+
69+
There are some webdriver examples available in the examples folder of this repository.
70+
[WebdriverIO examples](https://github.com/Georgegriff/query-selector-shadow-dom/blob/master/examples/webdriverio)
2571

2672
### Puppeteer
2773

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { remote } = require('webdriverio')
2+
const { locatorStrategy } = require('query-selector-shadow-dom/plugins/webdriverio');
3+
4+
;(async () => {
5+
const browser = await remote({
6+
logLevel: 'error',
7+
path: '/wd/hub',
8+
capabilities: {
9+
browserName: 'firefox'
10+
}
11+
})
12+
13+
browser.addLocatorStrategy('shadow', locatorStrategy);
14+
15+
16+
await browser.url('http://127.0.0.1:5500/test/')
17+
await browser.waitUntil(() => browser.custom$("shadow", ".btn-in-shadow-dom"));
18+
const elements = await browser.$$("*");
19+
const elementsShadow = await browser.custom$$("shadow", "*");
20+
console.log("All Elements on Page Excluding Shadow Dom", elements.length);
21+
console.log("All Elements on Page Including Shadow Dom", elementsShadow.length);
22+
23+
await browser.deleteSession()
24+
})().catch((e) => console.error(e))
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { remote } = require('webdriverio')
2+
const { locatorStrategy } = require('query-selector-shadow-dom/plugins/webdriverio');
3+
4+
;(async () => {
5+
const browser = await remote({
6+
logLevel: 'error',
7+
path: '/wd/hub',
8+
capabilities: {
9+
browserName: 'firefox'
10+
}
11+
})
12+
13+
browser.addLocatorStrategy('shadow', locatorStrategy);
14+
15+
16+
await browser.url('http://127.0.0.1:5500/test/')
17+
const input = await browser.custom$('shadow', '#type-to-input');
18+
await input.setValue('Typed text to input');
19+
// Firefox workaround
20+
// await browser.execute((input, val) => input.value = val, input, 'Typed text to input')
21+
console.log(await input.getValue())
22+
23+
await browser.deleteSession()
24+
})().catch((e) => console.error(e))

0 commit comments

Comments
 (0)