Simple template of puppeteer in Cloud Function
I hope it helps you.
Packages:
- Puppeteer 8.0.0
- Node version 12
| const puppeteer = require('puppeteer'); | |
| const urlLogin = 'https://myurl.com/'; | |
| exports.auth = async (req, res) => { | |
| const {email, pass} = req.body; | |
| console.log('Init puppeteer...') | |
| const browser = await puppeteer.launch({ headless: false, slowMo: 10 }); //Change headless true to deploy in Cloud Function | |
| try { | |
| const page = await browser.newPage(); | |
| console.log('Browser to login'); | |
| await page.goto(urlLogin); | |
| console.log('Insert data auth'); | |
| await page.type('input[name=identificacion]', email); | |
| await page.type('input[name=password]', pass); | |
| await page.click('.btn-iniciar-sesion'); | |
| console.log('Waiting for navigation...'); | |
| await page.waitForSelector('.avatar-miniatura'); | |
| console.log('Load service sucessfully!!!'); | |
| res.send({ response: 'success', data: null}); | |
| } catch (e) { | |
| console.log(e); | |
| res.send({response: 'error', data: 'puppeteer'}); | |
| } finally { | |
| await browser.close(); | |
| } | |
| } |