-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathexample-lh-auth.js
76 lines (63 loc) · 1.97 KB
/
example-lh-auth.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Example script for running Lighthouse on an authenticated page.
* See docs/recipes/auth/README.md for more.
*/
import puppeteer from 'puppeteer';
import lighthouse from 'lighthouse';
import esMain from 'es-main';
/**
* @param {puppeteer.Page} page
* @param {string} origin
*/
async function login(page, origin) {
await page.goto(origin);
await page.waitForSelector('input[type="email"]', {visible: true});
// Fill in and submit login form.
const emailInput = await page.$('input[type="email"]');
await emailInput.type('[email protected]');
const passwordInput = await page.$('input[type="password"]');
await passwordInput.type('password');
await Promise.all([
page.$eval('.login-form', form => form.submit()),
page.waitForNavigation(),
]);
}
/**
* @param {puppeteer.Page} page
* @param {string} origin
*/
async function logout(page, origin) {
await page.goto(`${origin}/logout`);
}
async function main() {
// Direct Puppeteer to open Chrome with a specific debugging port.
const browser = await puppeteer.launch({
// Set DEBUG environment variable if you want to see the tests in action.
headless: process.env.DEBUG ? false : 'new',
slowMo: 50,
});
const page = await browser.newPage();
// Setup the browser session to be logged into our site.
await login(page, 'http://localhost:10632');
// The local server is running on port 10632.
const url = 'http://localhost:10632/dashboard';
// Direct Lighthouse to use the same Puppeteer page.
// Disable storage reset so login session is preserved.
const result = await lighthouse(url, {disableStorageReset: true}, undefined, page);
// Direct Puppeteer to close the browser as we're done with it.
await browser.close();
// Output the result.
console.log(JSON.stringify(result.lhr, null, 2));
}
if (esMain(import.meta)) {
await main();
}
export {
login,
logout,
};