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'; |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 8 | import {$textContent, getBrowserAndPages} from '../../shared/helper.js'; |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 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(() => { |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 29 | // https://github.com/GoogleChrome/lighthouse/issues/14572 |
| 30 | expectError(/Request CacheStorage\.requestCacheNames failed/); |
| 31 | |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 32 | // 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 Somsikov | de4ef17 | 2022-11-30 18:29:55 +0100 | [diff] [blame] | 40 | afterEach(async () => { |
| 41 | await unregisterAllServiceWorkers(); |
| 42 | }); |
| 43 | |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 44 | it('successfully returns a Lighthouse report for the page state', async () => { |
| 45 | await navigateToLighthouseTab('lighthouse/hello.html'); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 46 | await registerServiceWorker(); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 47 | |
| 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 Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 59 | let numNavigations = 0; |
| 60 | target.on('framenavigated', () => ++numNavigations); |
| 61 | |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 62 | await selectMode('snapshot'); |
| 63 | await clickStartButton(); |
| 64 | |
| 65 | const {lhr, artifacts, reportEl} = await waitForResult(); |
| 66 | |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 67 | assert.strictEqual(numNavigations, 0); |
| 68 | |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 69 | assert.strictEqual(lhr.gatherMode, 'snapshot'); |
| 70 | |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 71 | assert.deepStrictEqual(artifacts.ViewportDimensions, { |
Adam Raine | 1427476 | 2023-02-02 13:07:07 -0800 | [diff] [blame] | 72 | innerHeight: 823, |
| 73 | innerWidth: 412, |
| 74 | outerHeight: 823, |
| 75 | outerWidth: 412, |
| 76 | devicePixelRatio: 1.75, |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 77 | }); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 78 | |
| 79 | const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr); |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 80 | assert.strictEqual(auditResults.length, 71); |
| 81 | assert.deepStrictEqual(erroredAudits, []); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 82 | assert.deepStrictEqual(failedAudits.map(audit => audit.id), [ |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 83 | 'document-title', |
| 84 | 'html-has-lang', |
| 85 | 'label', |
| 86 | 'meta-description', |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 87 | '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 Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 94 | const viewTrace = await $textContent('View Trace', reportEl); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 95 | assert.strictEqual(viewTrace, null); |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 96 | const viewOriginalTrace = await $textContent('View Original Trace', reportEl); |
| 97 | assert.strictEqual(viewOriginalTrace, null); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 98 | |
| 99 | // Ensure service worker is not cleared in snapshot mode. |
| 100 | assert.strictEqual(await getServiceWorkerCount(), 1); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 101 | }); |
| 102 | }); |