|
|
@ -40,31 +40,48 @@ const browser = await puppeteer.launch({ |
|
|
|
devtools: true, // to be commented or removed when project is submitted |
|
|
|
devtools: true, // to be commented or removed when project is submitted |
|
|
|
}) |
|
|
|
}) |
|
|
|
const [page] = await browser.pages() |
|
|
|
const [page] = await browser.pages() |
|
|
|
await page.goto('https://www.google.com/') |
|
|
|
// actions... |
|
|
|
// other actions... |
|
|
|
|
|
|
|
await browser.close() // you can comment this during you tests |
|
|
|
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** |
|
|
|
**Puppeteer helper - script model** |
|
|
|
|
|
|
|
|
|
|
|
```javascript |
|
|
|
```javascript |
|
|
|
if ((await page.$('button#myId')) !== null) { |
|
|
|
// navigate to a specific url |
|
|
|
await page.click('button#myId') |
|
|
|
await page.goto('https://www.google.com/') |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// wait an element in the page |
|
|
|
await page.waitForSelector('input#myId') |
|
|
|
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([ |
|
|
|
await Promise.all([ |
|
|
|
page.waitForNavigation(), |
|
|
|
page.waitForNavigation(), |
|
|
|
page.click('div#myId'), |
|
|
|
page.click('div#myId'), |
|
|
|
]) |
|
|
|
]) |
|
|
|
await page.waitForSelector('div#myId') |
|
|
|
|
|
|
|
|
|
|
|
// get the textContent of an element |
|
|
|
const myText = await page.$eval( |
|
|
|
const myText = await page.$eval( |
|
|
|
'div#myId', |
|
|
|
'div#myId', |
|
|
|
(s) => s.textContent, |
|
|
|
(elem) => elem.textContent, |
|
|
|
) |
|
|
|
) |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// type something in an input |
|
|
|
|
|
|
|
await page.type(`input#myId`, 'some text') |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
### Optional |
|
|
|
### Optional |
|
|
|
|
|
|
|
|
|
|
|