Skip to content

Instantly share code, notes, and snippets.

@robin-drexler
Created July 3, 2018 20:36
Show Gist options
  • Save robin-drexler/9338da0db7cac1f6d46ab215d69bb7f0 to your computer and use it in GitHub Desktop.
Save robin-drexler/9338da0db7cac1f6d46ab215d69bb7f0 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const queries = require('dom-testing-library/dist/queries');
const fs = require('fs');
async function createHelpers(page) {
await page.evaluateOnNewDocument(
fs.readFileSync(
'./node_modules/dom-testing-library/dist/dom-testing-library.umd.js',
'utf8'
)
);
const puppeteerQueries = Object.entries(queries).reduce(
(memo, [name, fn]) => {
memo[name] = async (...args) => {
return await page.evaluateHandle(
(innerName, ...innerArgs) => {
return window.DomTestingLibrary.queries[innerName](
document,
...innerArgs
);
},
name,
...args
);
};
return memo;
},
{}
);
return puppeteerQueries;
}
(async () => {
const browser = await puppeteer.launch({ headless: false, devtools: true });
const page = await browser.newPage();
const { getByText, getByPlaceholderText } = await createHelpers(page);
await page.goto('https://www.twitter.com');
const signupButton = await getByText('Sign Up');
await signupButton.click();
await page.waitForNavigation({ waitUntil: 'networkidle0' });
const nameField = await getByPlaceholderText('Name');
await nameField.focus();
await nameField.type('hello world');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment