blob: 873a1ae84d0697c001ce9dc8d0e2c8f0cb274135 [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
51 await selectMode('snapshot');
52 await clickStartButton();
53
54 const {lhr, artifacts, reportEl} = await waitForResult();
55
56 assert.strictEqual(lhr.gatherMode, 'snapshot');
57
Adam Raine4515db62022-09-28 16:37:20 -070058 assert.deepStrictEqual(artifacts.ViewportDimensions, {
59 innerHeight: 640,
60 innerWidth: 360,
61 outerHeight: 640,
62 outerWidth: 360,
63 devicePixelRatio: 3,
64 });
Adam Rainebf8654c2022-09-13 13:24:07 -070065
66 const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr);
67 assert.strictEqual(auditResults.length, 73);
68 assert.strictEqual(erroredAudits.length, 0);
69 assert.deepStrictEqual(failedAudits.map(audit => audit.id), [
Adam Rainebf8654c2022-09-13 13:24:07 -070070 'document-title',
71 'html-has-lang',
72 'label',
73 'meta-description',
Adam Rainebf8654c2022-09-13 13:24:07 -070074 'tap-targets',
75 ]);
76
77 // These a11y violations are not present on initial page load.
78 assert.strictEqual(lhr.audits['label'].details.items.length, 3);
79
80 // No trace was collected in snapshot mode.
81 const viewTrace = await reportEl.$('.lh-button--trace');
82 assert.strictEqual(viewTrace, null);
Adam Raine3983f182022-10-11 09:11:06 -070083
84 // Ensure service worker is not cleared in snapshot mode.
85 assert.strictEqual(await getServiceWorkerCount(), 1);
Adam Rainebf8654c2022-09-13 13:24:07 -070086 });
87});