Skip to content

Commit 6831fdf

Browse files
authored
Merge pull request #19 from Georgegriff/playwright
add a plugin for playwright selector engine
2 parents dd77f00 + c651158 commit 6831fdf

File tree

6 files changed

+141
-12
lines changed

6 files changed

+141
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
dist
33
.vscode
4-
coverage
4+
coverage
5+
.DS_Store

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,28 @@ Both of the methods above accept a 2nd parameter, see section `Provide alternati
1818

1919
## Examples
2020

21+
### Playwright
22+
23+
Playwright works really nicely with this package.
24+
25+
This module exposes a playwright `selectorEngine`: https://github.com/microsoft/playwright/blob/master/docs/api.md#selectorsregisterenginefunction-args
26+
27+
```javascript
28+
const { selectorEngine } = require("query-selector-shadow-dom/plugins/playwright");
29+
const playwright = require('playwright');
30+
...
31+
await playwright.selectors.register(selectorEngine, { name: 'shadow' })
32+
...
33+
await page.goto('chrome://downloads');
34+
// shadow= allows a css query selector that automatically pierces shadow roots.
35+
await page.waitForSelector('shadow=#no-downloads span', {timeout: 3000})
36+
```
37+
38+
For a full example see: https://github.com/Georgegriff/query-selector-shadow-dom/blob/master/examples/playwright
39+
2140
### Puppeteer
2241

23-
There are some puppeteer examples available in the exampes folder of this repository.
42+
There are some puppeteer examples available in the examples folder of this repository.
2443

2544
[Puppeteer examples](https://github.com/Georgegriff/query-selector-shadow-dom/blob/master/examples/puppeteer)
2645

examples/playwright/custom-engine.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { selectorEngine } = require("query-selector-shadow-dom/plugins/playwright");
2+
const playwright = require('playwright')
3+
4+
const main = async () => {
5+
await playwright.selectors.register(selectorEngine, { name: 'shadow' })
6+
7+
const browser = await playwright.chromium.launch({ headless: false})
8+
const context = await browser.newContext({ viewport: null })
9+
const page = await context.newPage()
10+
11+
await page.goto('chrome://downloads')
12+
13+
await page.waitForSelector('shadow=#no-downloads span', {timeout: 3000})
14+
await new Promise(resolve => setTimeout(resolve, 3000))
15+
16+
await page.close()
17+
await context.close()
18+
await browser.close()
19+
}
20+
21+
main()

package-lock.json

Lines changed: 65 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "query-selector-shadow-dom",
3-
"version": "0.3.4",
3+
"version": "0.4.0",
44
"description": "use querySelector syntax to search for nodes inside of (nested) shadow roots",
55
"main": "src/querySelectorDeep.js",
66
"scripts": {
@@ -15,7 +15,8 @@
1515
},
1616
"files": [
1717
"/src/",
18-
"dist/querySelectorShadowDom.js"
18+
"dist/querySelectorShadowDom.js",
19+
"/plugins/"
1920
],
2021
"author": "George Griffiths <GeorgeGriff>",
2122
"keywords": [
@@ -39,6 +40,7 @@
3940
"karma-rollup-preprocessor": "^6.0.0",
4041
"karma-spec-reporter": "0.0.32",
4142
"npm-watch": "^0.4.0",
43+
"playwright": "^0.10.0",
4244
"puppeteer": "^2.0.0",
4345
"rollup": "^0.67.0",
4446
"rollup-plugin-babel": "^4.0.3",

plugins/playwright/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
// load the library in UMD format which self executes and adds window.querySelectorShadowDom
6+
const querySelectorShadowDomUMD = fs.readFileSync(path.resolve(__dirname, "../../dist/querySelectorShadowDom.js"))
7+
8+
// a string because playwright does a .toString on a selector engine and we need to
9+
// make sure that query-selector-shadow-dom is injected and loaded into the function closure
10+
const engineString =`
11+
options = options || {};
12+
const name = options.name || 'shadow';
13+
${querySelectorShadowDomUMD}
14+
return {
15+
name: name,
16+
create(root, target) {
17+
return undefined;
18+
},
19+
query(root, selector) {
20+
return querySelectorShadowDom.querySelectorDeep(selector, root);
21+
},
22+
queryAll(root, selector) {
23+
return querySelectorShadowDom.querySelectorAllDeep(selector, root);
24+
}
25+
}
26+
`
27+
const selectorEngine = new Function("options", engineString)
28+
29+
module.exports = { selectorEngine };

0 commit comments

Comments
 (0)