blob: 61943105f01b95bed5735aa73df9a69c6df94a88 [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(() => {
Adam Rainede6c4e52023-02-09 14:10:42 -080029 // https://github.com/GoogleChrome/lighthouse/issues/14572
30 expectError(/Request CacheStorage\.requestCacheNames failed/);
31
Adam Rainebf8654c2022-09-13 13:24:07 -070032 // https://bugs.chromium.org/p/chromium/issues/detail?id=1357791
33 expectError(/Protocol Error: the message with wrong session id/);
34 expectError(/Protocol Error: the message with wrong session id/);
35 expectError(/Protocol Error: the message with wrong session id/);
36 expectError(/Protocol Error: the message with wrong session id/);
37 expectError(/Protocol Error: the message with wrong session id/);
38 });
39
Danil Somsikovde4ef172022-11-30 18:29:55 +010040 afterEach(async () => {
41 await unregisterAllServiceWorkers();
42 });
43
Adam Rainebf8654c2022-09-13 13:24:07 -070044 it('successfully returns a Lighthouse report for the page state', async () => {
45 await navigateToLighthouseTab('lighthouse/hello.html');
Adam Raine3983f182022-10-11 09:11:06 -070046 await registerServiceWorker();
Adam Rainebf8654c2022-09-13 13:24:07 -070047
48 const {target} = await getBrowserAndPages();
49 await target.evaluate(() => {
50 const makeTextFieldBtn = document.querySelector('button');
51 if (!makeTextFieldBtn) {
52 throw new Error('Button not found');
53 }
54 makeTextFieldBtn.click();
55 makeTextFieldBtn.click();
56 makeTextFieldBtn.click();
57 });
58
Adam Rainef25ab502022-10-17 12:54:06 -070059 let numNavigations = 0;
60 target.on('framenavigated', () => ++numNavigations);
61
Adam Rainebf8654c2022-09-13 13:24:07 -070062 await selectMode('snapshot');
63 await clickStartButton();
64
65 const {lhr, artifacts, reportEl} = await waitForResult();
66
Adam Rainef25ab502022-10-17 12:54:06 -070067 assert.strictEqual(numNavigations, 0);
68
Adam Rainebf8654c2022-09-13 13:24:07 -070069 assert.strictEqual(lhr.gatherMode, 'snapshot');
70
Adam Raine4515db62022-09-28 16:37:20 -070071 assert.deepStrictEqual(artifacts.ViewportDimensions, {
Adam Raine14274762023-02-02 13:07:07 -080072 innerHeight: 823,
73 innerWidth: 412,
74 outerHeight: 823,
75 outerWidth: 412,
76 devicePixelRatio: 1.75,
Adam Raine4515db62022-09-28 16:37:20 -070077 });
Adam Rainebf8654c2022-09-13 13:24:07 -070078
79 const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr);
Adam Rainede6c4e52023-02-09 14:10:42 -080080 assert.strictEqual(auditResults.length, 71);
81 assert.deepStrictEqual(erroredAudits, []);
Adam Rainebf8654c2022-09-13 13:24:07 -070082 assert.deepStrictEqual(failedAudits.map(audit => audit.id), [
Adam Rainebf8654c2022-09-13 13:24:07 -070083 'document-title',
84 'html-has-lang',
85 'label',
86 'meta-description',
Adam Rainebf8654c2022-09-13 13:24:07 -070087 'tap-targets',
88 ]);
89
90 // These a11y violations are not present on initial page load.
91 assert.strictEqual(lhr.audits['label'].details.items.length, 3);
92
93 // No trace was collected in snapshot mode.
Adam Raine2a9afbc2022-11-30 10:28:58 -080094 const viewTrace = await $textContent('View Trace', reportEl);
Adam Rainebf8654c2022-09-13 13:24:07 -070095 assert.strictEqual(viewTrace, null);
Adam Raine2a9afbc2022-11-30 10:28:58 -080096 const viewOriginalTrace = await $textContent('View Original Trace', reportEl);
97 assert.strictEqual(viewOriginalTrace, null);
Adam Raine3983f182022-10-11 09:11:06 -070098
99 // Ensure service worker is not cleared in snapshot mode.
100 assert.strictEqual(await getServiceWorkerCount(), 1);
Adam Rainebf8654c2022-09-13 13:24:07 -0700101 });
102});