Forked from urban235/gist:bb55ab6a8ada8bcc968c67cb03d5dc85
Created
January 25, 2019 16:51
-
-
Save Steakeye/e3e3ff11cf9a4065b352c3c967e5a7ca to your computer and use it in GitHub Desktop.
Puppeteer cheat sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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