Browse Source

add comments to puppeteer script helper

content-update
Louise Foussat 3 years ago committed by Clément
parent
commit
86d27d3a56
  1. 37
      subjects/ephemeris/README.md

37
subjects/ephemeris/README.md

@ -40,31 +40,48 @@ const browser = await puppeteer.launch({
devtools: true, // to be commented or removed when project is submitted
})
const [page] = await browser.pages()
await page.goto('https://www.google.com/')
// other actions...
// actions...
await browser.close() // you can comment this during you tests
process.exit(0) // terminate the process as succeeded (you can comment this during you tests)
```
**Puppeteer helper - script model**
```javascript
if ((await page.$('button#myId')) !== null) {
await page.click('button#myId')
}
// navigate to a specific url
await page.goto('https://www.google.com/')
// wait an element in the page
await page.waitForSelector('input#myId')
await page.type(`input#myId`, 'myText')
await page.waitForSelector('div#myId')
// find an element in the page:
const button = await page.$('button#myId')
// check the element was found
if (button) {
// use the element
await button.click()
} else {
// do something else: for example treat the exception as an error
console.error('Button not found !')
// and terminate the process as failed (code 1)
process.exit(1)
}
// navigate in a website
await Promise.all([
page.waitForNavigation(),
page.click('div#myId'),
])
await page.waitForSelector('div#myId')
// get the textContent of an element
const myText = await page.$eval(
'div#myId',
(s) => s.textContent,
(elem) => elem.textContent,
)
```
// type something in an input
await page.type(`input#myId`, 'some text')
```
### Optional

Loading…
Cancel
Save