blob: d7af644c41bd43cefea969e9f818adffe2c8da85 [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';
Adam Raine2a9afbc2022-11-30 10:28:58 -08008import {$textContent, getBrowserAndPages} from '../../shared/helper.js';
Adam Rainebf8654c2022-09-13 13:24:07 -07009import {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,
Danil Somsikovde4ef172022-11-30 18:29:55 +010017 unregisterAllServiceWorkers,
Adam Rainebf8654c2022-09-13 13:24:07 -070018 waitForResult,
19} from '../helpers/lighthouse-helpers.js';
20
21// This test will fail (by default) in headful mode, as the target page never gets painted.
22// To resolve this when debugging, just make sure the target page is visible during the lighthouse run.
23
24describe('Snapshot', async function() {
25 // The tests in this suite are particularly slow
26 this.timeout(60_000);
27
28 beforeEach(() => {
29 // https://bugs.chromium.org/p/chromium/issues/detail?id=1357791
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 expectError(/Protocol Error: the message with wrong session id/);
35 });
36
Danil Somsikovde4ef172022-11-30 18:29:55 +010037 afterEach(async () => {
38 await unregisterAllServiceWorkers();
39 });
40
Adam Rainebf8654c2022-09-13 13:24:07 -070041 it('successfully returns a Lighthouse report for the page state', async () => {
42 await navigateToLighthouseTab('lighthouse/hello.html');
Adam Raine3983f182022-10-11 09:11:06 -070043 await registerServiceWorker();
Adam Rainebf8654c2022-09-13 13:24:07 -070044
45 const {target} = await getBrowserAndPages();
46 await target.evaluate(() => {
47 const makeTextFieldBtn = document.querySelector('button');
48 if (!makeTextFieldBtn) {
49 throw new Error('Button not found');
50 }
51 makeTextFieldBtn.click();
52 makeTextFieldBtn.click();
53 makeTextFieldBtn.click();
54 });
55
Adam Rainef25ab502022-10-17 12:54:06 -070056 let numNavigations = 0;
57 target.on('framenavigated', () => ++numNavigations);
58
Adam Rainebf8654c2022-09-13 13:24:07 -070059 await selectMode('snapshot');
60 await clickStartButton();
61
62 const {lhr, artifacts, reportEl} = await waitForResult();
63
Adam Rainef25ab502022-10-17 12:54:06 -070064 assert.strictEqual(numNavigations, 0);
65
Adam Rainebf8654c2022-09-13 13:24:07 -070066 assert.strictEqual(lhr.gatherMode, 'snapshot');
67
Adam Raine4515db62022-09-28 16:37:20 -070068 assert.deepStrictEqual(artifacts.ViewportDimensions, {
69 innerHeight: 640,
70 innerWidth: 360,
71 outerHeight: 640,
72 outerWidth: 360,
73 devicePixelRatio: 3,
74 });
Adam Rainebf8654c2022-09-13 13:24:07 -070075
76 const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr);
77 assert.strictEqual(auditResults.length, 73);
78 assert.strictEqual(erroredAudits.length, 0);
79 assert.deepStrictEqual(failedAudits.map(audit => audit.id), [
Adam Rainebf8654c2022-09-13 13:24:07 -070080 'document-title',
81 'html-has-lang',
82 'label',
83 'meta-description',
Adam Rainebf8654c2022-09-13 13:24:07 -070084 'tap-targets',
85 ]);
86
87 // These a11y violations are not present on initial page load.
88 assert.strictEqual(lhr.audits['label'].details.items.length, 3);
89
90 // No trace was collected in snapshot mode.
Adam Raine2a9afbc2022-11-30 10:28:58 -080091 const viewTrace = await $textContent('View Trace', reportEl);
Adam Rainebf8654c2022-09-13 13:24:07 -070092 assert.strictEqual(viewTrace, null);
Adam Raine2a9afbc2022-11-30 10:28:58 -080093 const viewOriginalTrace = await $textContent('View Original Trace', reportEl);
94 assert.strictEqual(viewOriginalTrace, null);
Adam Raine3983f182022-10-11 09:11:06 -070095
96 // Ensure service worker is not cleared in snapshot mode.
97 assert.strictEqual(await getServiceWorkerCount(), 1);
Adam Rainebf8654c2022-09-13 13:24:07 -070098 });
99});