[go: nahoru, domu]

Skip to content

Commit

Permalink
core(timespan-runner): warn if a navigation is detected (#15407)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Aug 28, 2023
1 parent 18a9e07 commit 29b80e5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
23 changes: 23 additions & 0 deletions core/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import {getEmptyArtifactState, collectPhaseArtifacts, awaitArtifacts} from './ru
import {enableAsyncStacks, prepareTargetForTimespanMode} from './driver/prepare.js';
import {initializeConfig} from '../config/config.js';
import {getBaseArtifacts, finalizeArtifacts} from './base-artifacts.js';
import * as i18n from '../lib/i18n/i18n.js';

/* eslint-disable max-len */
const UIStrings = {
/** A warning that indicates page navigations should be audited using navigation mode, as opposed to timespan mode. "navigation mode" refers to a Lighthouse mode that analyzes a page navigation. "timespan mode" refers to a Lighthouse mode that analyzes user interactions over an arbitrary period of time. */
warningNavigationDetected: 'A page navigation was detected during the run. Using timespan mode to audit page navigations is not recommended. Use navigation mode to audit page navigations for better third-party attribution and main thread detection.',
};
/* eslint-enable max-len */

const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);

/**
* @param {LH.Puppeteer.Page} page
Expand Down Expand Up @@ -45,6 +55,13 @@ async function startTimespanGather(page, options = {}) {

await prepareTargetForTimespanMode(driver, resolvedConfig.settings);

let pageNavigationDetected = false;
function onFrameNavigated() {
pageNavigationDetected = true;
}

driver.defaultSession.on('Page.frameNavigated', onFrameNavigated);

const disableAsyncStacks = await enableAsyncStacks(driver.defaultSession);

await collectPhaseArtifacts({phase: 'startInstrumentation', ...phaseOptions});
Expand All @@ -67,6 +84,11 @@ async function startTimespanGather(page, options = {}) {
// We should disable our `Page.frameNavigated` handlers before that.
await disableAsyncStacks();

driver.defaultSession.off('Page.frameNavigated', onFrameNavigated);
if (pageNavigationDetected) {
baseArtifacts.LighthouseRunWarnings.push(str_(UIStrings.warningNavigationDetected));
}

await collectPhaseArtifacts({phase: 'getArtifact', ...phaseOptions});
await driver.disconnect();

Expand All @@ -82,4 +104,5 @@ async function startTimespanGather(page, options = {}) {

export {
startTimespanGather,
UIStrings,
};
17 changes: 17 additions & 0 deletions core/test/gather/timespan-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
mockDriverModule,
mockRunnerModule,
} from './mock-driver.js';
import {flushAllTimersAndMicrotasks, timers} from '../test-utils.js';

const mockSubmodules = await mockDriverSubmodules();
const mockRunner = await mockRunnerModule();
Expand All @@ -40,6 +41,9 @@ describe('Timespan Runner', () => {
/** @type {LH.Config} */
let config;

before(() => timers.useFakeTimers());
after(() => timers.dispose());

beforeEach(() => {
mockSubmodules.reset();
mockPage = createMockPage();
Expand Down Expand Up @@ -180,4 +184,17 @@ describe('Timespan Runner', () => {
},
});
});

it('should warn if a navigation was detected', async () => {
mockDriver._session.on.mockEvent('Page.frameNavigated', {});

const timespan = await startTimespanGather(page, {config});
await flushAllTimersAndMicrotasks();
await timespan.endTimespanGather();

const artifacts = await mockRunner.gather.mock.calls[0][0]();
expect(artifacts.LighthouseRunWarnings).toHaveLength(1);
expect(artifacts.LighthouseRunWarnings[0])
.toBeDisplayString(/A page navigation was detected during the run. Using timespan mode/);
});
});
4 changes: 4 additions & 0 deletions core/test/scenarios/api-test-pptr.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ describe('Individual modes API', function() {
finalDisplayedUrl: `${state.serverBaseUrl}/onclick.html#done`,
});

expect(lhr.runWarnings).toHaveLength(1);
expect(lhr.runWarnings[0])
.toMatch(/A page navigation was detected during the run. Using timespan mode/);

const bestPractices = lhr.categories['best-practices'];
expect(bestPractices.score).toBeLessThan(1);

Expand Down
1 change: 0 additions & 1 deletion core/test/scenarios/pptr-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ function createTestState() {

beforeEach(async () => {
trace = undefined;
console.log('########');
this.page = await this.browser.newPage();
});

Expand Down
3 changes: 3 additions & 0 deletions shared/localization/locales/en-US.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions shared/localization/locales/en-XL.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 29b80e5

Please sign in to comment.