Skip to content

Instantly share code, notes, and snippets.

@noherczeg
Created March 8, 2019 01:24
Show Gist options
  • Save noherczeg/26c22377994f1983e73fc5b76a979123 to your computer and use it in GitHub Desktop.
Save noherczeg/26c22377994f1983e73fc5b76a979123 to your computer and use it in GitHub Desktop.
How to operate on multiple levels of shadow DOM elements
import puppeteer from 'puppeteer';
function queryShadow(...args: any[]): string {
const prop = args.pop();
const middle = args.map(a => `.querySelector('${a}')`).join('.shadowRoot');
return `document${middle}.${prop}`;
}
describe('my-component', () => {
it('renders', async () => {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto('http://localhost:3333', {waitUntil: 'networkidle2'});
// const inputValue = await page.evaluate(() => {
// const input: HTMLInputElement = document.querySelector('body > my-component').shadowRoot.querySelector('div > div > other-comp').shadowRoot.querySelector('div > input[type="text"]');
// return input.value;
// });
const inputValue = await page.evaluate(queryShadow('my-component', 'other-comp', 'input', 'value'));
// const inputValue = await page.evaluate(queryShadow('my-component', 'other-comp', 'input', 'value=\'BaaaaaBB\''));
// await page.screenshot({ path: 'yas.png' });
expect(inputValue).toEqual('AAA');
await browser.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment