Skip to content

Instantly share code, notes, and snippets.

@urban235
Last active January 25, 2019 16:51
Show Gist options
  • Save urban235/bb55ab6a8ada8bcc968c67cb03d5dc85 to your computer and use it in GitHub Desktop.
Save urban235/bb55ab6a8ada8bcc968c67cb03d5dc85 to your computer and use it in GitHub Desktop.
Puppeteer cheat sheet
// Puppeteer cheat sheet
// gonna try and do for puppeteer what capybara got: https://gist.github.com/zhengjia/428105
// to delete an input value
function DeleteInput(page, inputSelector, word){
// input must be focused
page.type(inputSelector, '')
for (let i = 0; i < word.length; ++i) {
page.keyboard.down('Backspace');
}
}
// is element visible by selector
const isVisible = await page.evaluate(() => {
const e = document.querySelector('#join');
if (!e){
return false;
}
const style = window.getComputedStyle(e);
return style && style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0';
});
console.log(isVisible)
// to make sure an element is visible, USE: waitForElemToBeVisible(page, selector);
async function isVisible(page, selector){
const isVisible = await page.evaluate(
(selector) => {
const e = document.querySelector(selector);
if (!e){
return false;
}
const style = window.getComputedStyle(e);
lala = style && style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0';
return style && style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0';
}
, selector);
return isVisible
}
async function waitForElemToBeVisible(page, selector){
console.log('1. starting fun');
let isItVisible = false;
while(!isItVisible) {
isItVisible = await isVisible(page, selector);
await new Promise(r => setTimeout(r, 2000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment