Adam Raine | b1ac423 | 2022-07-21 12:13:29 -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 { |
| 9 | $textContent, |
| 10 | getBrowserAndPages, |
| 11 | setDevToolsSettings, |
| 12 | waitFor, |
| 13 | waitForElementWithTextContent, |
| 14 | } from '../../shared/helper.js'; |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 15 | import {describe, it} from '../../shared/mocha-extensions.js'; |
| 16 | import { |
| 17 | clickStartButton, |
| 18 | getAuditsBreakdown, |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 19 | getServiceWorkerCount, |
Adam Raine | 242a862 | 2022-10-12 09:56:47 -0700 | [diff] [blame] | 20 | interceptNextFileSave, |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 21 | navigateToLighthouseTab, |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 22 | registerServiceWorker, |
Adam Raine | 242a862 | 2022-10-12 09:56:47 -0700 | [diff] [blame] | 23 | renderHtmlInIframe, |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 24 | selectCategories, |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 25 | selectDevice, |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 26 | setLegacyNavigation, |
| 27 | setThrottlingMethod, |
Adam Raine | da3fa69 | 2022-09-06 17:47:28 -0700 | [diff] [blame] | 28 | setToolbarCheckboxWithText, |
Danil Somsikov | de4ef17 | 2022-11-30 18:29:55 +0100 | [diff] [blame] | 29 | unregisterAllServiceWorkers, |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 30 | waitForResult, |
| 31 | } from '../helpers/lighthouse-helpers.js'; |
| 32 | |
| 33 | // This test will fail (by default) in headful mode, as the target page never gets painted. |
| 34 | // To resolve this when debugging, just make sure the target page is visible during the lighthouse run. |
| 35 | |
Adam Raine | cd6d449 | 2022-07-27 12:01:36 -0700 | [diff] [blame] | 36 | describe('Navigation', async function() { |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 37 | // The tests in this suite are particularly slow |
| 38 | this.timeout(60_000); |
| 39 | |
Adam Raine | 62373ba | 2022-10-31 16:34:25 -0700 | [diff] [blame] | 40 | beforeEach(() => { |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 41 | // https://github.com/GoogleChrome/lighthouse/issues/14572 |
| 42 | expectError(/Request CacheStorage\.requestCacheNames failed/); |
| 43 | |
Adam Raine | 62373ba | 2022-10-31 16:34:25 -0700 | [diff] [blame] | 44 | // https://bugs.chromium.org/p/chromium/issues/detail?id=1357791 |
| 45 | expectError(/Protocol Error: the message with wrong session id/); |
| 46 | expectError(/Protocol Error: the message with wrong session id/); |
| 47 | expectError(/Protocol Error: the message with wrong session id/); |
| 48 | expectError(/Protocol Error: the message with wrong session id/); |
| 49 | expectError(/Protocol Error: the message with wrong session id/); |
| 50 | }); |
| 51 | |
Danil Somsikov | de4ef17 | 2022-11-30 18:29:55 +0100 | [diff] [blame] | 52 | afterEach(async () => { |
| 53 | await unregisterAllServiceWorkers(); |
| 54 | }); |
| 55 | |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 56 | const modes = ['legacy', 'FR']; |
| 57 | |
| 58 | for (const mode of modes) { |
| 59 | describe(`in ${mode} mode`, () => { |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 60 | it('successfully returns a Lighthouse report', async () => { |
| 61 | await navigateToLighthouseTab('lighthouse/hello.html'); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 62 | await registerServiceWorker(); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 63 | |
| 64 | await setLegacyNavigation(mode === 'legacy'); |
Adam Raine | 7179cf5 | 2022-09-29 13:09:37 -0700 | [diff] [blame] | 65 | await selectCategories([ |
| 66 | 'performance', |
| 67 | 'accessibility', |
| 68 | 'best-practices', |
| 69 | 'seo', |
| 70 | 'pwa', |
| 71 | 'lighthouse-plugin-publisher-ads', |
| 72 | ]); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 73 | |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 74 | let numNavigations = 0; |
| 75 | const {target} = await getBrowserAndPages(); |
| 76 | target.on('framenavigated', () => ++numNavigations); |
| 77 | |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 78 | await clickStartButton(); |
| 79 | |
| 80 | const {lhr, artifacts, reportEl} = await waitForResult(); |
| 81 | |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 82 | if (mode === 'legacy') { |
| 83 | // 1 initial about:blank jump |
| 84 | // 1 about:blank jump + 1 navigation for the default pass |
| 85 | // 1 about:blank jump + 1 navigation for the offline pass |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 86 | // 2 navigations to go to chrome://terms and back testing bfcache |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 87 | // 1 navigation after auditing to reset state |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 88 | assert.strictEqual(numNavigations, 8); |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 89 | } else { |
| 90 | // 1 initial about:blank jump |
| 91 | // 1 about:blank jump + 1 navigation for the default pass |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 92 | // 2 navigations to go to chrome://terms and back testing bfcache |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 93 | // 1 navigation after auditing to reset state |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 94 | assert.strictEqual(numNavigations, 6); |
Adam Raine | f25ab50 | 2022-10-17 12:54:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 97 | assert.strictEqual(lhr.lighthouseVersion, '10.0.0'); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 98 | assert.match(lhr.finalUrl, /^https:\/\/localhost:[0-9]+\/test\/e2e\/resources\/lighthouse\/hello.html/); |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 99 | |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 100 | assert.strictEqual(lhr.configSettings.throttlingMethod, 'simulate'); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 101 | assert.strictEqual(lhr.configSettings.disableStorageReset, false); |
| 102 | assert.strictEqual(lhr.configSettings.formFactor, 'mobile'); |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 103 | assert.strictEqual(lhr.configSettings.throttling.rttMs, 150); |
| 104 | assert.strictEqual(lhr.configSettings.screenEmulation.disabled, true); |
| 105 | assert.include(lhr.configSettings.emulatedUserAgent, 'Mobile'); |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 106 | assert.include(lhr.environment.networkUserAgent, 'Mobile'); |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 107 | |
| 108 | assert.deepStrictEqual(artifacts.ViewportDimensions, { |
Adam Raine | 1427476 | 2023-02-02 13:07:07 -0800 | [diff] [blame] | 109 | innerHeight: 823, |
| 110 | innerWidth: 412, |
| 111 | outerHeight: 823, |
| 112 | outerWidth: 412, |
| 113 | devicePixelRatio: 1.75, |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 114 | }); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 115 | |
| 116 | const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr); |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 117 | assert.strictEqual(auditResults.length, 173); |
| 118 | assert.deepStrictEqual(erroredAudits, []); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 119 | assert.deepStrictEqual(failedAudits.map(audit => audit.id), [ |
| 120 | 'service-worker', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 121 | 'installable-manifest', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 122 | 'splash-screen', |
| 123 | 'themed-omnibox', |
| 124 | 'maskable-icon', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 125 | 'document-title', |
| 126 | 'html-has-lang', |
| 127 | 'meta-description', |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 128 | 'bf-cache', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 129 | ]); |
| 130 | |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 131 | const viewTraceButton = await $textContent('View Original Trace', reportEl); |
| 132 | if (!viewTraceButton) { |
| 133 | throw new Error('Could not find view trace button'); |
| 134 | } |
Adam Raine | 2fcf01b | 2022-10-04 15:17:38 -0700 | [diff] [blame] | 135 | |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 136 | // Test view trace button behavior |
Adam Raine | 2fcf01b | 2022-10-04 15:17:38 -0700 | [diff] [blame] | 137 | await viewTraceButton.click(); |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 138 | let selectedTab = await waitFor('.tabbed-pane-header-tab.selected[aria-label="Performance"]'); |
| 139 | let selectedTabText = await selectedTab.evaluate(selectedTabEl => { |
Adam Raine | 2fcf01b | 2022-10-04 15:17:38 -0700 | [diff] [blame] | 140 | return selectedTabEl.textContent; |
| 141 | }); |
| 142 | assert.strictEqual(selectedTabText, 'Performance'); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 143 | |
Adam Raine | 242a862 | 2022-10-12 09:56:47 -0700 | [diff] [blame] | 144 | await navigateToLighthouseTab(); |
| 145 | |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 146 | // Test element link behavior |
| 147 | const lcpElementAudit = await waitForElementWithTextContent('Largest Contentful Paint element', reportEl); |
| 148 | await lcpElementAudit.click(); |
| 149 | const lcpElementLink = await waitForElementWithTextContent('button'); |
| 150 | await lcpElementLink.click(); |
| 151 | |
| 152 | selectedTab = await waitFor('.tabbed-pane-header-tab.selected[aria-label="Elements"]'); |
| 153 | selectedTabText = await selectedTab.evaluate(selectedTabEl => { |
| 154 | return selectedTabEl.textContent; |
| 155 | }); |
| 156 | assert.strictEqual(selectedTabText, 'Elements'); |
| 157 | |
Adam Raine | 242a862 | 2022-10-12 09:56:47 -0700 | [diff] [blame] | 158 | const waitForJson = await interceptNextFileSave(); |
| 159 | |
| 160 | // For some reason the CDP click command doesn't work here even if the tools menu is open. |
Connor Clark | 76abfb3 | 2022-11-03 15:50:41 -0700 | [diff] [blame] | 161 | await reportEl.$eval( |
| 162 | 'a[data-action="save-json"]:not(.hidden)', saveJsonEl => (saveJsonEl as HTMLElement).click()); |
Adam Raine | 242a862 | 2022-10-12 09:56:47 -0700 | [diff] [blame] | 163 | |
| 164 | const jsonContent = await waitForJson(); |
| 165 | assert.strictEqual(jsonContent, JSON.stringify(lhr, null, 2)); |
| 166 | |
| 167 | const waitForHtml = await interceptNextFileSave(); |
| 168 | |
| 169 | // For some reason the CDP click command doesn't work here even if the tools menu is open. |
Connor Clark | 76abfb3 | 2022-11-03 15:50:41 -0700 | [diff] [blame] | 170 | await reportEl.$eval( |
| 171 | 'a[data-action="save-html"]:not(.hidden)', saveHtmlEl => (saveHtmlEl as HTMLElement).click()); |
Adam Raine | 242a862 | 2022-10-12 09:56:47 -0700 | [diff] [blame] | 172 | |
| 173 | const htmlContent = await waitForHtml(); |
| 174 | const iframeHandle = await renderHtmlInIframe(htmlContent); |
| 175 | const iframeAuditDivs = await iframeHandle.$$('.lh-audit'); |
| 176 | const frontendAuditDivs = await reportEl.$$('.lh-audit'); |
| 177 | assert.strictEqual(frontendAuditDivs.length, iframeAuditDivs.length); |
| 178 | |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 179 | // Ensure service worker was cleared. |
| 180 | assert.strictEqual(await getServiceWorkerCount(), 0); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 181 | }); |
| 182 | |
| 183 | it('successfully returns a Lighthouse report with DevTools throttling', async () => { |
| 184 | await navigateToLighthouseTab('lighthouse/hello.html'); |
| 185 | |
| 186 | await setThrottlingMethod('devtools'); |
| 187 | await setLegacyNavigation(mode === 'legacy'); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 188 | |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 189 | await clickStartButton(); |
| 190 | |
| 191 | const {lhr, reportEl} = await waitForResult(); |
| 192 | |
| 193 | assert.strictEqual(lhr.configSettings.throttlingMethod, 'devtools'); |
| 194 | |
Adam Raine | cd6d449 | 2022-07-27 12:01:36 -0700 | [diff] [blame] | 195 | // [crbug.com/1347220] DevTools throttling can force resources to load slow enough for these audits to fail sometimes. |
| 196 | const flakyAudits = [ |
| 197 | 'server-response-time', |
| 198 | 'render-blocking-resources', |
| 199 | ]; |
| 200 | |
| 201 | const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr, flakyAudits); |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 202 | assert.strictEqual(auditResults.length, 150); |
| 203 | assert.deepStrictEqual(erroredAudits, []); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 204 | assert.deepStrictEqual(failedAudits.map(audit => audit.id), [ |
| 205 | 'service-worker', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 206 | 'installable-manifest', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 207 | 'splash-screen', |
| 208 | 'themed-omnibox', |
| 209 | 'maskable-icon', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 210 | 'document-title', |
| 211 | 'html-has-lang', |
| 212 | 'meta-description', |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 213 | 'bf-cache', |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 214 | ]); |
| 215 | |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 216 | const viewTraceButton = await $textContent('View Trace', reportEl); |
| 217 | assert.ok(viewTraceButton); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 218 | }); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 219 | |
| 220 | it('successfully returns a Lighthouse report when settings changed', async () => { |
Adam Raine | d34ecbc | 2022-10-10 11:33:44 -0700 | [diff] [blame] | 221 | await setDevToolsSettings({language: 'es'}); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 222 | await navigateToLighthouseTab('lighthouse/hello.html'); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 223 | await registerServiceWorker(); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 224 | |
Adam Raine | d34ecbc | 2022-10-10 11:33:44 -0700 | [diff] [blame] | 225 | await setToolbarCheckboxWithText(mode === 'legacy', 'Navegación antigua'); |
| 226 | await setToolbarCheckboxWithText(false, 'Borrar almacenamiento'); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 227 | await selectCategories(['performance', 'best-practices']); |
Adam Raine | bf8654c | 2022-09-13 13:24:07 -0700 | [diff] [blame] | 228 | await selectDevice('desktop'); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 229 | |
| 230 | await clickStartButton(); |
| 231 | |
Adam Raine | da3fa69 | 2022-09-06 17:47:28 -0700 | [diff] [blame] | 232 | const {reportEl, lhr, artifacts} = await waitForResult(); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 233 | |
| 234 | const {innerWidth, innerHeight, devicePixelRatio} = artifacts.ViewportDimensions; |
| 235 | // TODO: Figure out why outerHeight can be different depending on OS |
| 236 | assert.strictEqual(innerHeight, 720); |
| 237 | assert.strictEqual(innerWidth, 1280); |
| 238 | assert.strictEqual(devicePixelRatio, 1); |
| 239 | |
| 240 | const {erroredAudits} = getAuditsBreakdown(lhr); |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 241 | assert.deepStrictEqual(erroredAudits, []); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 242 | |
| 243 | assert.deepStrictEqual(Object.keys(lhr.categories), ['performance', 'best-practices']); |
| 244 | assert.strictEqual(lhr.configSettings.disableStorageReset, true); |
| 245 | assert.strictEqual(lhr.configSettings.formFactor, 'desktop'); |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 246 | assert.strictEqual(lhr.configSettings.throttling.rttMs, 40); |
| 247 | assert.strictEqual(lhr.configSettings.screenEmulation.disabled, true); |
| 248 | assert.notInclude(lhr.configSettings.emulatedUserAgent, 'Mobile'); |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 249 | assert.notInclude(lhr.environment.networkUserAgent, 'Mobile'); |
Adam Raine | 4515db6 | 2022-09-28 16:37:20 -0700 | [diff] [blame] | 250 | |
Adam Raine | de6c4e5 | 2023-02-09 14:10:42 -0800 | [diff] [blame^] | 251 | const viewTraceButton = await $textContent('Ver rastro original', reportEl); |
Adam Raine | 2a9afbc | 2022-11-30 10:28:58 -0800 | [diff] [blame] | 252 | assert.ok(viewTraceButton); |
Adam Raine | da3fa69 | 2022-09-06 17:47:28 -0700 | [diff] [blame] | 253 | |
Adam Raine | d34ecbc | 2022-10-10 11:33:44 -0700 | [diff] [blame] | 254 | const footerIssueText = await reportEl.$eval('.lh-footer__version_issue', footerIssueEl => { |
| 255 | return footerIssueEl.textContent; |
| 256 | }); |
| 257 | assert.strictEqual(lhr.i18n.rendererFormattedStrings.footerIssue, 'Notificar un problema'); |
| 258 | assert.strictEqual(footerIssueText, 'Notificar un problema'); |
Adam Raine | 3983f18 | 2022-10-11 09:11:06 -0700 | [diff] [blame] | 259 | |
| 260 | // Ensure service worker is not cleared because we disable the storage reset. |
| 261 | assert.strictEqual(await getServiceWorkerCount(), 1); |
Adam Raine | a708e01 | 2022-08-10 09:58:23 -0700 | [diff] [blame] | 262 | }); |
Adam Raine | b1ac423 | 2022-07-21 12:13:29 -0700 | [diff] [blame] | 263 | }); |
| 264 | } |
| 265 | }); |