Skip to content

Commit 74bf436

Browse files
authoredFeb 25, 2022
core(fr): use frame url in gather context (#13699)
1 parent 9addbda commit 74bf436

File tree

11 files changed

+75
-8
lines changed

11 files changed

+75
-8
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
</head>
8+
<body>
9+
<h1>Redirect to myself</h1>
10+
<script>
11+
const done = new URLSearchParams(window.location.search).has('done');
12+
if (!done) {
13+
const url = new URL(window.location.href);
14+
url.searchParams.set('done', '');
15+
window.location.replace(url);
16+
} else {
17+
const url = new URL(window.location.href);
18+
url.searchParams.delete('done');
19+
window.history.pushState({}, '', url);
20+
}
21+
</script>
22+
</body>
23+
</html>

‎lighthouse-cli/test/smokehouse/core-tests.js

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import redirectsHistoryPushState from './test-definitions/redirects-history-push
5353
import redirectsMultipleServer from './test-definitions/redirects-multiple-server.js';
5454
import redirectsSingleClient from './test-definitions/redirects-single-client.js';
5555
import redirectsSingleServer from './test-definitions/redirects-single-server.js';
56+
import redirectsSelf from './test-definitions/redirects-self.js';
5657
import screenshot from './test-definitions/screenshot.js';
5758
import seoFailing from './test-definitions/seo-failing.js';
5859
import seoPassing from './test-definitions/seo-passing.js';
@@ -111,6 +112,7 @@ const smokeTests = [
111112
redirectsMultipleServer,
112113
redirectsSingleClient,
113114
redirectsSingleServer,
115+
redirectsSelf,
114116
screenshot,
115117
seoFailing,
116118
seoPassing,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license Copyright 2022 The Lighthouse Authors. All Rights Reserved.
3+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
5+
*/
6+
'use strict';
7+
8+
/**
9+
* @type {Smokehouse.ExpectedRunnerResult}
10+
*/
11+
const expectations = {
12+
artifacts: {
13+
MainDocumentContent: /Redirect to myself/,
14+
},
15+
lhr: {
16+
requestedUrl: 'http://localhost:10200/redirects-self.html',
17+
finalUrl: 'http://localhost:10200/redirects-self.html?done=',
18+
audits: {
19+
},
20+
runWarnings: [
21+
'The page may not be loading as expected because your test URL (http://localhost:10200/redirects-self.html) was redirected to http://localhost:10200/redirects-self.html?done=. Try testing the second URL directly.',
22+
],
23+
},
24+
};
25+
26+
export default {
27+
id: 'redirects-self',
28+
expectations,
29+
};
30+

‎lighthouse-core/fraggle-rock/gather/navigation-runner.js

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ async function _computeNavigationResult(
210210
async function _navigation(navigationContext) {
211211
const artifactState = getEmptyArtifactState();
212212
const phaseState = {
213+
url: await navigationContext.driver.url(),
213214
gatherMode: /** @type {const} */ ('navigation'),
214215
driver: navigationContext.driver,
215216
computedCache: navigationContext.computedCache,
@@ -223,6 +224,7 @@ async function _navigation(navigationContext) {
223224
await collectPhaseArtifacts({phase: 'startInstrumentation', ...phaseState});
224225
await collectPhaseArtifacts({phase: 'startSensitiveInstrumentation', ...phaseState});
225226
const navigateResult = await _navigate(navigationContext);
227+
phaseState.url = navigateResult.finalUrl;
226228
await collectPhaseArtifacts({phase: 'stopSensitiveInstrumentation', ...phaseState});
227229
await collectPhaseArtifacts({phase: 'stopInstrumentation', ...phaseState});
228230
await _cleanupNavigation(navigationContext);

‎lighthouse-core/fraggle-rock/gather/runner-helpers.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @property {LH.Gatherer.GatherMode} gatherMode
1616
* @property {Map<string, LH.ArbitraryEqualityMap>} computedCache
1717
* @property {LH.Config.Settings} settings
18+
* @property {string} url
1819
*/
1920

2021
/** @typedef {Record<string, Promise<any>>} IntermediateArtifacts */
@@ -74,6 +75,7 @@ async function collectPhaseArtifacts(options) {
7475
gatherMode,
7576
computedCache,
7677
settings,
78+
url,
7779
} = options;
7880
const priorPhase = phaseToPriorPhase[phase];
7981
const priorPhaseArtifacts = (priorPhase && artifactState[priorPhase]) || {};
@@ -90,7 +92,7 @@ async function collectPhaseArtifacts(options) {
9092
: /** @type {Dependencies} */ ({});
9193

9294
return gatherer[phase]({
93-
url: await driver.url(),
95+
url,
9496
gatherMode,
9597
driver,
9698
baseArtifacts,

‎lighthouse-core/fraggle-rock/gather/snapshot-runner.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function snapshot(options) {
2727

2828
/** @type {Map<string, LH.ArbitraryEqualityMap>} */
2929
const computedCache = new Map();
30-
const url = await options.page.url();
30+
const url = await driver.url();
3131

3232
const runnerOptions = {config, computedCache};
3333
const artifacts = await Runner.gather(
@@ -39,6 +39,7 @@ async function snapshot(options) {
3939
const artifactDefinitions = config.artifacts || [];
4040
const artifactState = getEmptyArtifactState();
4141
await collectPhaseArtifacts({
42+
url,
4243
phase: 'getArtifact',
4344
gatherMode: 'snapshot',
4445
driver,

‎lighthouse-core/fraggle-rock/gather/timespan-runner.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ async function startTimespan(options) {
3232
/** @type {Map<string, LH.ArbitraryEqualityMap>} */
3333
const computedCache = new Map();
3434
const artifactDefinitions = config.artifacts || [];
35-
const requestedUrl = await options.page.url();
35+
const requestedUrl = await driver.url();
3636
const baseArtifacts = await getBaseArtifacts(config, driver, {gatherMode: 'timespan'});
3737
const artifactState = getEmptyArtifactState();
3838
/** @type {Omit<import('./runner-helpers.js').CollectPhaseArtifactOptions, 'phase'>} */
3939
const phaseOptions = {
40+
url: requestedUrl,
4041
driver,
4142
artifactDefinitions,
4243
artifactState,
@@ -52,7 +53,9 @@ async function startTimespan(options) {
5253

5354
return {
5455
async endTimespan() {
55-
const finalUrl = await options.page.url();
56+
const finalUrl = await driver.url();
57+
phaseOptions.url = finalUrl;
58+
5659
const runnerOptions = {config, computedCache};
5760
const artifacts = await Runner.gather(
5861
async () => {

‎lighthouse-core/test/fraggle-rock/gather/mock-driver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function createMockDriver() {
129129
_page: page,
130130
_executionContext: context,
131131
_session: session,
132-
url: () => page.url(),
132+
url: jest.fn(() => page.url()),
133133
defaultSession: session,
134134
connect: jest.fn(),
135135
disconnect: jest.fn(),

‎lighthouse-core/test/fraggle-rock/gather/runner-helpers-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ describe('collectPhaseArtifacts', () => {
132132
it(`should run the ${phase} phase of gatherers in ${gatherMode} mode`, async () => {
133133
const {artifactDefinitions, gatherers} = createGathererSet();
134134
await helpers.collectPhaseArtifacts({
135+
url: 'https://example.com',
135136
driver,
136137
artifactDefinitions,
137138
artifactState,
@@ -156,6 +157,7 @@ describe('collectPhaseArtifacts', () => {
156157
it('should gather the artifacts', async () => {
157158
const {artifactDefinitions} = createGathererSet();
158159
await helpers.collectPhaseArtifacts({
160+
url: 'https://example.com',
159161
driver,
160162
artifactDefinitions,
161163
artifactState,
@@ -188,6 +190,7 @@ describe('collectPhaseArtifacts', () => {
188190
];
189191

190192
await helpers.collectPhaseArtifacts({
193+
url: 'https://example.com',
191194
driver,
192195
artifactDefinitions,
193196
artifactState,
@@ -221,6 +224,7 @@ describe('collectPhaseArtifacts', () => {
221224

222225
const {artifactDefinitions, gatherers} = createGathererSet();
223226
await helpers.collectPhaseArtifacts({
227+
url: 'https://example.com',
224228
driver,
225229
artifactDefinitions,
226230
artifactState,

‎lighthouse-core/test/fraggle-rock/gather/snapshot-runner-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Snapshot Runner', () => {
7171
});
7272

7373
it('should collect base artifacts', async () => {
74-
mockPage.url.mockResolvedValue('https://lighthouse.example.com/');
74+
mockDriver.url.mockResolvedValue('https://lighthouse.example.com/');
7575

7676
await snapshot({page, config});
7777
const artifacts = await mockRunner.gather.mock.calls[0][0]();

‎lighthouse-core/test/fraggle-rock/gather/timespan-runner-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ describe('Timespan Runner', () => {
9090
});
9191

9292
it('should collect base artifacts', async () => {
93-
mockPage.url.mockResolvedValue('https://start.example.com/');
93+
mockDriver.url.mockResolvedValue('https://start.example.com/');
9494

9595
const timespan = await startTimespan({page, config});
9696

97-
mockPage.url.mockResolvedValue('https://end.example.com/');
97+
mockDriver.url.mockResolvedValue('https://end.example.com/');
9898

9999
await timespan.endTimespan();
100100
const artifacts = await mockRunner.gather.mock.calls[0][0]();

0 commit comments

Comments
 (0)
Please sign in to comment.