Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(timespan-runner): warn if a navigation is detected #15407

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {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 @@

await prepareTargetForTimespanMode(driver, resolvedConfig.settings);

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

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

Check warning on line 64 in core/gather/timespan-runner.js

View check run for this annotation

Codecov / codecov/patch

core/gather/timespan-runner.js#L58-L64

Added lines #L58 - L64 were not covered by tests
const disableAsyncStacks = await enableAsyncStacks(driver.defaultSession);

await collectPhaseArtifacts({phase: 'startInstrumentation', ...phaseOptions});
Expand All @@ -67,6 +84,11 @@
// 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));
}

Check warning on line 91 in core/gather/timespan-runner.js

View check run for this annotation

Codecov / codecov/patch

core/gather/timespan-runner.js#L87-L91

Added lines #L87 - L91 were not covered by tests
await collectPhaseArtifacts({phase: 'getArtifact', ...phaseOptions});
await driver.disconnect();

Expand All @@ -82,4 +104,5 @@

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('########');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 😬 😬 😬 😬 😬 😬 😬

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.

Loading