mirror of https://github.com/01-edu/public.git
add comments to puppeteer script helper
This commit is contained in:
parent
0492e13f83
commit
86d27d3a56
|
@ -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…
Reference in New Issue