Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experimental] Puppeteer Custom Engine support #36

Merged
merged 4 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions examples/puppeteer/clicking-elements.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs');
const { QueryHandler } = require("../../plugins/puppeteer");
(async () => {
try {
await puppeteer.__experimental_registerCustomQueryHandler('shadow', QueryHandler);
const browser = await puppeteer.launch({
headless: false
headless: false,
devtools: true
})
const page = await browser.newPage()
await page.goto('http://127.0.0.1:5500/test/')

// makes the library available in evaluate functions which run within the browser context
await page.addScriptTag({
path: path.join(__dirname, 'node_modules/query-selector-shadow-dom/dist/querySelectorShadowDom.js')
});


// ensure btn exists and return it
const btn = (await page.waitForFunction(() => {
const btn = querySelectorShadowDom.querySelectorDeep(".btn-in-shadow-dom");
return btn;
})).asElement();
await page.waitForSelector("shadow/.btn-in-shadow-dom");
const btn = await page.$("shadow/.btn-in-shadow-dom");
await btn.click();
// check btn was clicked (this page expected btn to change text of output)
const outputSpan = await page.evaluateHandle(() => querySelectorShadowDom.querySelectorDeep(".output"));
const outputSpan = await page.$("shadow/.output");
const text = await page.evaluate((output) => output.innerText, outputSpan);
// prints the text from the output
console.log(text);
Expand Down
23 changes: 23 additions & 0 deletions examples/puppeteer/custom-engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { QueryHandler } = require("../../plugins/puppeteer");
const puppeteer = require('puppeteer')

const main = async () => {
await puppeteer.__experimental_registerCustomQueryHandler('shadow', QueryHandler);

const browser = await puppeteer.chromium.launch({ headless: false})
const context = await browser.newContext({ viewport: null })
const page = await context.newPage()

await page.goto('chrome://downloads')

const element = await page.waitForSelector('shadow/div', {timeout: 3000})
const span = await element.$$("shadow/div > .illustration + span")
console.log(span)
await new Promise(resolve => setTimeout(resolve, 3000))

await page.close()
await context.close()
await browser.close()
}

main()
23 changes: 23 additions & 0 deletions examples/puppeteer/multiple-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const puppeteer = require('puppeteer');
const { QueryHandler } = require("../../plugins/puppeteer");
(async () => {
try {
await puppeteer.__experimental_registerCustomQueryHandler('shadow', QueryHandler);
const browser = await puppeteer.launch({
headless: false
})
const page = await browser.newPage()
await page.goto('http://127.0.0.1:5500/test/')
await page.waitForSelector("shadow/.btn-in-shadow-dom")
const elements = await page.$$("*");
const elementsShadow = await page.$$("shadow/*");
console.log("All Elements on Page Excluding Shadow Dom", elements.length);
console.log("All Elements on Page Including Shadow Dom", elementsShadow.length);

await browser.close()

} catch (e) {
console.error(e);
}

})()
17 changes: 4 additions & 13 deletions examples/puppeteer/typing-to-elements.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs');
const { QueryHandler } = require("../../plugins/puppeteer");
(async () => {
try {
await puppeteer.__experimental_registerCustomQueryHandler('shadow', QueryHandler);
const browser = await puppeteer.launch({
headless: false
})
const page = await browser.newPage()
await page.goto('http://127.0.0.1:5500/test/')
// makes the library available in evaluate functions which run within the browser context
await page.addScriptTag({
path: path.join(__dirname, 'node_modules/query-selector-shadow-dom/dist/querySelectorShadowDom.js')
});

const inputElement = await page.waitForSelector("shadow/#type-to-input");

// returns an https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-elementhandle
const inputElement = (await page.waitForFunction(() => {
return querySelectorShadowDom.querySelectorDeep("#type-to-input");
})).asElement();

// https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#elementhandletypetext-options
await inputElement.type("Typed text to input");

const value = await page.evaluate(inputElement => inputElement.value, inputElement);
console.log(value);
console.log("Value", value);

await browser.close()

Expand Down
Loading