Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | import {assert} from 'chai'; |
| 6 | |
| 7 | import {expectError} from '../../conductor/events.js'; |
| 8 | import {getBrowserAndPages} from '../../shared/helper.js'; |
| 9 | import {describe, it} from '../../shared/mocha-extensions.js'; |
| 10 | import { |
| 11 | clickStartButton, |
| 12 | getAuditsBreakdown, |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 13 | getServiceWorkerCount, |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 14 | navigateToLighthouseTab, |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 15 | registerServiceWorker, |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 16 | selectMode, |
Danil Somsikov | de4ef17 | 2022-11-30 18:29:55 +0100 | [diff] [blame] | 17 | unregisterAllServiceWorkers, |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 18 | 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 | |
| 24 | describe('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 Somsikov | de4ef17 | 2022-11-30 18:29:55 +0100 | [diff] [blame] | 37 | afterEach(async () => { |
| 38 | await unregisterAllServiceWorkers(); |
| 39 | }); |
| 40 | |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 41 | it('successfully returns a Lighthouse report for the page state', async () => { |
| 42 | await navigateToLighthouseTab('lighthouse/hello.html'); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 43 | await registerServiceWorker(); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 44 | |
| 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 Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 56 | let numNavigations = 0; |
| 57 | target.on('framenavigated', () => ++numNavigations); |
| 58 | |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 59 | await selectMode('snapshot'); |
| 60 | await clickStartButton(); |
| 61 | |
| 62 | const {lhr, artifacts, reportEl} = await waitForResult(); |
| 63 | |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 64 | assert.strictEqual(numNavigations, 0); |
| 65 | |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 66 | assert.strictEqual(lhr.gatherMode, 'snapshot'); |
| 67 | |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 68 | assert.deepStrictEqual(artifacts.ViewportDimensions, { |
| 69 | innerHeight: 640, |
| 70 | innerWidth: 360, |
| 71 | outerHeight: 640, |
| 72 | outerWidth: 360, |
| 73 | devicePixelRatio: 3, |
| 74 | }); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 75 | |
| 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 Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 80 | 'document-title', |
| 81 | 'html-has-lang', |
| 82 | 'label', |
| 83 | 'meta-description', |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 84 | '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. |
| 91 | const viewTrace = await reportEl.$('.lh-button--trace'); |
| 92 | assert.strictEqual(viewTrace, null); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 93 | |
| 94 | // Ensure service worker is not cleared in snapshot mode. |
| 95 | assert.strictEqual(await getServiceWorkerCount(), 1); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 96 | }); |
| 97 | }); |