summaryrefslogtreecommitdiff
path: root/src/handlers/launchUserSession.js
blob: 44f21938a32ae7f5b5d4226bfc969bad83d533fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const puppeteer = require('puppeteer');
const { clickElementBySelector } = require('./utils.js');

const { EDUFPMI_URL } = process.env;

const launchUserSession = (user, headless = true) => puppeteer.launch({
  headless,
  args: ['--no-sandbox']
}).then(async browser => {
    const { username, password } = user;
    const page = await browser.newPage();

    // Login
    await page.goto(EDUFPMI_URL, { waitUntil: 'domcontentloaded' });
    await page.type('input#username', username);
    await page.type('input#password', password);
    await Promise.all([
      clickElementBySelector(page, 'button#loginbtn'),
      page.waitForNavigation()
    ]);
    console.log(`Logged in as ${username}`);

    return browser;
  });

module.exports = launchUserSession;