blob: b18823ea7e9625e9625da57ac53d8683ee1649bf [file] [log] [blame]
Adam Rainebf8654c2022-09-13 13:24:07 -07001// Copyright 2022 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {assert} from 'chai';
6
7import {expectError} from '../../conductor/events.js';
8import {getBrowserAndPages} from '../../shared/helper.js';
9import {describe, it} from '../../shared/mocha-extensions.js';
10import {
11 clickStartButton,
12 getAuditsBreakdown,
Adam Raine3983f182022-10-11 09:11:06 -070013 getServiceWorkerCount,
Adam Rainebf8654c2022-09-13 13:24:07 -070014 navigateToLighthouseTab,
Adam Raine3983f182022-10-11 09:11:06 -070015 registerServiceWorker,
Adam Rainebf8654c2022-09-13 13:24:07 -070016 selectMode,
17 waitForResult,
18} from '../helpers/lighthouse-helpers.js';
19
20// This test will fail (by default) in headful mode, as the target page never gets painted.
21// To resolve this when debugging, just make sure the target page is visible during the lighthouse run.
22
23describe('Snapshot', async function() {
24 // The tests in this suite are particularly slow
25 this.timeout(60_000);
26
27 beforeEach(() => {
28 // https://bugs.chromium.org/p/chromium/issues/detail?id=1357791
29 expectError(/Protocol Error: the message with wrong session id/);
30 expectError(/Protocol Error: the message with wrong session id/);
31 expectError(/Protocol Error: the message with wrong session id/);
32 expectError(/Protocol Error: the message with wrong session id/);
33 expectError(/Protocol Error: the message with wrong session id/);
34 });
35
36 it('successfully returns a Lighthouse report for the page state', async () => {
37 await navigateToLighthouseTab('lighthouse/hello.html');
Adam Raine3983f182022-10-11 09:11:06 -070038 await registerServiceWorker();
Adam Rainebf8654c2022-09-13 13:24:07 -070039
40 const {target} = await getBrowserAndPages();
41 await target.evaluate(() => {
42 const makeTextFieldBtn = document.querySelector('button');
43 if (!makeTextFieldBtn) {
44 throw new Error('Button not found');
45 }
46 makeTextFieldBtn.click();
47 makeTextFieldBtn.click();
48 makeTextFieldBtn.click();
49 });
50
Adam Rainef25ab502022-10-17 12:54:06 -070051 let numNavigations = 0;
52 target.on('framenavigated', () => ++numNavigations);
53
Adam Rainebf8654c2022-09-13 13:24:07 -070054 await selectMode('snapshot');
55 await clickStartButton();
56
57 const {lhr, artifacts, reportEl} = await waitForResult();
58
Adam Rainef25ab502022-10-17 12:54:06 -070059 assert.strictEqual(numNavigations, 0);
60
Adam Rainebf8654c2022-09-13 13:24:07 -070061 assert.strictEqual(lhr.gatherMode, 'snapshot');
62
Adam Raine4515db62022-09-28 16:37:20 -070063 assert.deepStrictEqual(artifacts.ViewportDimensions, {
64 innerHeight: 640,
65 innerWidth: 360,
66 outerHeight: 640,
67 outerWidth: 360,
68 devicePixelRatio: 3,
69 });
Adam Rainebf8654c2022-09-13 13:24:07 -070070
71 const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr);
72 assert.strictEqual(auditResults.length, 73);
73 assert.strictEqual(erroredAudits.length, 0);
74 assert.deepStrictEqual(failedAudits.map(audit => audit.id), [
Adam Rainebf8654c2022-09-13 13:24:07 -070075 'document-title',
76 'html-has-lang',
77 'label',
78 'meta-description',
Adam Rainebf8654c2022-09-13 13:24:07 -070079 'tap-targets',
80 ]);
81
82 // These a11y violations are not present on initial page load.
83 assert.strictEqual(lhr.audits['label'].details.items.length, 3);
84
85 // No trace was collected in snapshot mode.
86 const viewTrace = await reportEl.$('.lh-button--trace');
87 assert.strictEqual(viewTrace, null);
Adam Raine3983f182022-10-11 09:11:06 -070088
89 // Ensure service worker is not cleared in snapshot mode.
90 assert.strictEqual(await getServiceWorkerCount(), 1);
Adam Rainebf8654c2022-09-13 13:24:07 -070091 });
92});