Skip to content

Commit 510320c

Browse files
committed
docs: update puppeteer auth example for 10.0 (#14195)
1 parent f7c51c3 commit 510320c

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

docs/recipes/auth/README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,25 @@ What does this do? Read on....
3535

3636
## Process
3737

38-
Puppeteer - a browser automation tool - can be used to programatically setup a session.
38+
Puppeteer - a browser automation tool - can be used to programmatically setup a session.
3939

4040
1. Launch a new browser.
4141
1. Navigate to the login page.
4242
1. Fill and submit the login form.
4343
1. Run Lighthouse using the same browser.
4444

45-
First, launch Chrome:
45+
First, launch Chrome and create a new page:
4646
```js
47-
// This port will be used by Lighthouse later. The specific port is arbitrary.
48-
const PORT = 8041;
4947
const browser = await puppeteer.launch({
50-
args: [`--remote-debugging-port=${PORT}`],
5148
// Optional, if you want to see the tests in action.
5249
headless: false,
5350
slowMo: 50,
5451
});
52+
const page = await browser.newPage();
5553
```
5654

5755
Navigate to the login form:
5856
```js
59-
const page = await browser.newPage();
6057
await page.goto('http://localhost:10632');
6158
```
6259

@@ -89,18 +86,13 @@ await Promise.all([
8986

9087
At this point, the session that Puppeteer is managing is now logged in.
9188

92-
Close the page used to log in:
93-
```js
94-
await page.close();
95-
// The page has been closed, but the browser still has the relevant session.
96-
```
97-
98-
Now run Lighthouse, using the same port as before:
89+
Now run Lighthouse, using the same page as before:
9990
```js
10091
// The local server is running on port 10632.
10192
const url = 'http://localhost:10632/dashboard';
102-
// Direct Lighthouse to use the same port.
103-
const result = await lighthouse(url, {port: PORT, disableStorageReset: true});
93+
// Direct Lighthouse to use the same page.
94+
// Disable storage reset so login session is preserved.
95+
const result = await lighthouse(url, {disableStorageReset: true}, undefined, page);
10496
const lhr = result.lhr;
10597

10698
// Direct Puppeteer to close the browser - we're done with it.

docs/recipes/auth/example-lh-auth.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ import puppeteer from 'puppeteer';
1414
import lighthouse from 'lighthouse';
1515
import esMain from 'es-main';
1616

17-
// This port will be used by Lighthouse later. The specific port is arbitrary.
18-
const PORT = 8041;
19-
2017
/**
21-
* @param {import('puppeteer').Browser} browser
18+
* @param {puppeteer.Page} page
2219
* @param {string} origin
2320
*/
24-
async function login(browser, origin) {
25-
const page = await browser.newPage();
21+
async function login(page, origin) {
2622
await page.goto(origin);
2723
await page.waitForSelector('input[type="email"]', {visible: true});
2824

@@ -35,36 +31,35 @@ async function login(browser, origin) {
3531
page.$eval('.login-form', form => form.submit()),
3632
page.waitForNavigation(),
3733
]);
38-
39-
await page.close();
4034
}
4135

4236
/**
43-
* @param {puppeteer.Browser} browser
37+
* @param {puppeteer.Page} page
4438
* @param {string} origin
4539
*/
46-
async function logout(browser, origin) {
47-
const page = await browser.newPage();
40+
async function logout(page, origin) {
4841
await page.goto(`${origin}/logout`);
49-
await page.close();
5042
}
5143

5244
async function main() {
5345
// Direct Puppeteer to open Chrome with a specific debugging port.
5446
const browser = await puppeteer.launch({
55-
args: [`--remote-debugging-port=${PORT}`],
5647
// Optional, if you want to see the tests in action.
5748
headless: false,
5849
slowMo: 50,
5950
});
51+
const page = await browser.newPage();
6052

6153
// Setup the browser session to be logged into our site.
62-
await login(browser, 'http://localhost:10632');
54+
await login(page, 'http://localhost:10632');
6355

6456
// The local server is running on port 10632.
6557
const url = 'http://localhost:10632/dashboard';
66-
// Direct Lighthouse to use the same port.
67-
const result = await lighthouse(url, {port: PORT, disableStorageReset: true});
58+
59+
// Direct Lighthouse to use the same Puppeteer page.
60+
// Disable storage reset so login session is preserved.
61+
const result = await lighthouse(url, {disableStorageReset: true}, undefined, page);
62+
6863
// Direct Puppeteer to close the browser as we're done with it.
6964
await browser.close();
7065

@@ -73,7 +68,7 @@ async function main() {
7368
}
7469

7570
if (esMain(import.meta)) {
76-
main();
71+
await main();
7772
}
7873

7974
export {

docs/recipes/integration-test/example-lh-auth.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ describe('my site', () => {
9898
});
9999

100100
afterEach(async () => {
101+
await logout(page, ORIGIN);
101102
await page.close();
102-
await logout(browser, ORIGIN);
103103
});
104104

105105
describe('/ logged out', () => {
@@ -120,14 +120,14 @@ describe('my site', () => {
120120

121121
describe('/ logged in', () => {
122122
it('lighthouse', async () => {
123-
await login(browser, ORIGIN);
123+
await login(page, ORIGIN);
124124
await page.goto(ORIGIN);
125125
const lhr = await runLighthouse(page.url());
126126
expect(lhr).toHaveLighthouseScoreGreaterThanOrEqual('seo', 0.9);
127127
});
128128

129129
it('login form should not exist', async () => {
130-
await login(browser, ORIGIN);
130+
await login(page, ORIGIN);
131131
await page.goto(ORIGIN);
132132
const emailInput = await page.$('input[type="email"]');
133133
const passwordInput = await page.$('input[type="password"]');
@@ -145,14 +145,14 @@ describe('my site', () => {
145145

146146
describe('/dashboard logged in', () => {
147147
it('lighthouse', async () => {
148-
await login(browser, ORIGIN);
148+
await login(page, ORIGIN);
149149
await page.goto(`${ORIGIN}/dashboard`);
150150
const lhr = await runLighthouse(page.url());
151151
expect(lhr).toHaveLighthouseScoreGreaterThanOrEqual('seo', 0.9);
152152
});
153153

154154
it('has secrets', async () => {
155-
await login(browser, ORIGIN);
155+
await login(page, ORIGIN);
156156
await page.goto(`${ORIGIN}/dashboard`);
157157
expect(await page.content()).toContain('secrets');
158158
});

0 commit comments

Comments
 (0)