This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (46 loc) · 1.63 KB
/
index.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
const puppeteer = require('puppeteer');
const lighthouse = require('lighthouse');
// origin page that we want to test
const PAGE_URL = 'https://mazipan.space/examples/only-for-login';
// page for doing authentication
const PAGE_LOGIN_URL = 'https://mazipan.space/examples/login';
const doingAuthentication = async (browser) => {
// -- START doing authentication
const page = await browser.newPage();
// Go to the login page
await page.goto(PAGE_LOGIN_URL);
// waiting the email field is available
await page.waitForSelector('#email');
const emailInput = await page.$('#email');
await emailInput.type('me@mazipan.space');
const passwordInput = await page.$('#password');
await passwordInput.type('password123');
const submitBtn = await page.$('button[type="submit"]');
await submitBtn.click();
// waiting for redirection after login
await page.waitForNavigation();
await page.close();
// -- FINISH doing authentication
};
(async () => {
const browser = await puppeteer.launch({
headless: false, // switch to false if you want to see in actions
defaultViewport: null,
});
await doingAuthentication(browser);
// running lighthouse to open the page behind auth
const { report, lhr } = await lighthouse(PAGE_URL, {
port: new URL(browser.wsEndpoint()).port,
output: 'html',
disableStorageReset: true,
});
fs.writeFileSync('lhreport.html', report);
fs.writeFileSync('lhreport.json', JSON.stringify(lhr, null, 2));
console.log(
`Lighthouse scores: ${Object.values(lhr.categories)
.map((c) => c.score)
.join(', ')}`
);
await browser.close();
})();