-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(fr): add navigation runner (#11975)
- Loading branch information
1 parent
394a6c3
commit 55cfbef
Showing
10 changed files
with
601 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
lighthouse-core/fraggle-rock/gather/navigation-runner.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/** | ||
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved. | ||
* 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 | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
const Driver = require('./driver.js'); | ||
const Runner = require('../../runner.js'); | ||
const {defaultNavigationConfig} = require('../../config/constants.js'); | ||
const {initializeConfig} = require('../config/config.js'); | ||
const {getBaseArtifacts} = require('./base-artifacts.js'); | ||
|
||
/** | ||
* @typedef NavigationContext | ||
* @property {Driver} driver | ||
* @property {LH.Config.NavigationDefn} navigation | ||
* @property {string} requestedUrl | ||
*/ | ||
|
||
/** @typedef {Record<string, Promise<any>>} IntermediateArtifacts */ | ||
|
||
/** | ||
* @param {{driver: Driver, config: LH.Config.FRConfig, requestedUrl: string}} args | ||
*/ | ||
async function _setup({driver, config, requestedUrl}) { | ||
await driver.connect(); | ||
// TODO(FR-COMPAT): use frameNavigated-based navigation | ||
await driver._page.goto(defaultNavigationConfig.blankPage); | ||
|
||
// TODO(FR-COMPAT): setupDriver | ||
|
||
const baseArtifacts = getBaseArtifacts(config); | ||
baseArtifacts.URL.requestedUrl = requestedUrl; | ||
|
||
return {baseArtifacts}; | ||
} | ||
|
||
/** | ||
* @param {NavigationContext} navigationContext | ||
*/ | ||
async function _setupNavigation({driver, navigation}) { | ||
// TODO(FR-COMPAT): use frameNavigated-based navigation | ||
await driver._page.goto(navigation.blankPage); | ||
|
||
// TODO(FR-COMPAT): setup network conditions (throttling & cache state) | ||
} | ||
|
||
/** | ||
* @param {NavigationContext} navigationContext | ||
* @param {IntermediateArtifacts} artifacts | ||
*/ | ||
async function _beforeTimespanPhase(navigationContext, artifacts) { | ||
for (const artifactDefn of navigationContext.navigation.artifacts) { | ||
const gatherer = artifactDefn.gatherer.instance; | ||
if (!gatherer.meta.supportedModes.includes('timespan')) continue; | ||
|
||
const artifactPromise = Promise.resolve().then(() => | ||
gatherer.beforeTimespan({driver: navigationContext.driver, gatherMode: 'navigation'}) | ||
); | ||
artifacts[artifactDefn.id] = artifactPromise; | ||
await artifactPromise.catch(() => {}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {NavigationContext} navigationContext | ||
*/ | ||
async function _navigate(navigationContext) { | ||
const {driver, requestedUrl} = navigationContext; | ||
// TODO(FR-COMPAT): use waitForCondition-based navigation | ||
await driver._page.goto(requestedUrl, {waitUntil: ['load', 'networkidle2']}); | ||
|
||
// TODO(FR-COMPAT): disable all throttling settings | ||
// TODO(FR-COMPAT): capture page load errors | ||
} | ||
|
||
/** | ||
* @param {NavigationContext} navigationContext | ||
* @param {IntermediateArtifacts} artifacts | ||
*/ | ||
async function _afterTimespanPhase(navigationContext, artifacts) { | ||
for (const artifactDefn of navigationContext.navigation.artifacts) { | ||
const gatherer = artifactDefn.gatherer.instance; | ||
if (!gatherer.meta.supportedModes.includes('timespan')) continue; | ||
|
||
const artifactPromise = (artifacts[artifactDefn.id] || Promise.resolve()).then(() => | ||
gatherer.afterTimespan({driver: navigationContext.driver, gatherMode: 'navigation'}) | ||
); | ||
artifacts[artifactDefn.id] = artifactPromise; | ||
await artifactPromise.catch(() => {}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {NavigationContext} navigationContext | ||
* @param {IntermediateArtifacts} artifacts | ||
*/ | ||
async function _snapshotPhase(navigationContext, artifacts) { | ||
for (const artifactDefn of navigationContext.navigation.artifacts) { | ||
const gatherer = artifactDefn.gatherer.instance; | ||
if (!gatherer.meta.supportedModes.includes('snapshot')) continue; | ||
|
||
const artifactPromise = Promise.resolve().then(() => | ||
gatherer.snapshot({driver: navigationContext.driver, gatherMode: 'navigation'}) | ||
); | ||
artifacts[artifactDefn.id] = artifactPromise; | ||
await artifactPromise.catch(() => {}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {IntermediateArtifacts} timespanArtifacts | ||
* @param {IntermediateArtifacts} snapshotArtifacts | ||
* @return {Promise<Partial<LH.GathererArtifacts>>} | ||
*/ | ||
async function _mergeArtifacts(timespanArtifacts, snapshotArtifacts) { | ||
/** @type {IntermediateArtifacts} */ | ||
const artifacts = {}; | ||
for (const [id, promise] of Object.entries({...timespanArtifacts, ...snapshotArtifacts})) { | ||
artifacts[id] = await promise.catch(err => err); | ||
} | ||
|
||
return artifacts; | ||
} | ||
|
||
/** | ||
* @param {NavigationContext} navigationContext | ||
*/ | ||
async function _navigation(navigationContext) { | ||
/** @type {IntermediateArtifacts} */ | ||
const timespanArtifacts = {}; | ||
/** @type {IntermediateArtifacts} */ | ||
const snapshotArtifacts = {}; | ||
|
||
await _setupNavigation(navigationContext); | ||
await _beforeTimespanPhase(navigationContext, timespanArtifacts); | ||
await _navigate(navigationContext); | ||
await _afterTimespanPhase(navigationContext, timespanArtifacts); | ||
await _snapshotPhase(navigationContext, snapshotArtifacts); | ||
|
||
const artifacts = await _mergeArtifacts(timespanArtifacts, snapshotArtifacts); | ||
return {artifacts}; | ||
} | ||
|
||
/** | ||
* @param {{driver: Driver, config: LH.Config.FRConfig, requestedUrl: string}} args | ||
*/ | ||
async function _navigations({driver, config, requestedUrl}) { | ||
if (!config.navigations) throw new Error('No navigations configured'); | ||
|
||
/** @type {Partial<LH.GathererArtifacts>} */ | ||
const artifacts = {}; | ||
|
||
for (const navigation of config.navigations) { | ||
const navigationContext = { | ||
driver, | ||
navigation, | ||
requestedUrl, | ||
}; | ||
|
||
const navigationResult = await _navigation(navigationContext); | ||
Object.assign(artifacts, navigationResult.artifacts); | ||
} | ||
|
||
return {artifacts}; | ||
} | ||
|
||
/** | ||
* @param {{driver: Driver}} args | ||
*/ | ||
async function _cleanup({driver}) { // eslint-disable-line no-unused-vars | ||
// TODO(FR-COMPAT): clear storage if necessary | ||
} | ||
|
||
/** | ||
* @param {{url: string, page: import('puppeteer').Page, config?: LH.Config.Json}} options | ||
* @return {Promise<LH.RunnerResult|undefined>} | ||
*/ | ||
async function navigation(options) { | ||
const {url: requestedUrl, page} = options; | ||
const {config} = initializeConfig(options.config, {gatherMode: 'navigation'}); | ||
|
||
return Runner.run( | ||
async () => { | ||
const driver = new Driver(page); | ||
const {baseArtifacts} = await _setup({driver, config, requestedUrl}); | ||
const {artifacts} = await _navigations({driver, config, requestedUrl}); | ||
await _cleanup({driver}); | ||
|
||
return /** @type {LH.Artifacts} */ ({...baseArtifacts, ...artifacts}); // Cast to drop Partial<> | ||
}, | ||
{ | ||
url: requestedUrl, | ||
config, | ||
} | ||
); | ||
} | ||
|
||
module.exports = { | ||
navigation, | ||
_setup, | ||
_setupNavigation, | ||
_beforeTimespanPhase, | ||
_afterTimespanPhase, | ||
_snapshotPhase, | ||
_navigate, | ||
_navigation, | ||
_navigations, | ||
_cleanup, | ||
}; |
32 changes: 32 additions & 0 deletions
32
lighthouse-core/test/fixtures/fraggle-rock/navigation-basic/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!doctype html> | ||
<!-- | ||
* Copyright 2021 The Lighthouse Authors. All Rights Reserved. | ||
* 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 | ||
* 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. | ||
--> | ||
<html lang="en"> | ||
<head> | ||
<title></title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0"> | ||
</head> | ||
<body> | ||
Hello, Fraggle Rock! | ||
|
||
This page has some basic violations when the page has loaded. | ||
|
||
<!-- FAIL(label) --> | ||
<section> | ||
<form> | ||
<input | ||
id="label" | ||
type="text"> | ||
</form> | ||
</section> | ||
|
||
<script> | ||
// FAIL(errors-in-console) | ||
console.error('Accessibility violations added!'); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved. | ||
* 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 | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
/* eslint-env jest */ | ||
|
||
const { | ||
createMockOnFn, | ||
createMockOnceFn, | ||
createMockSendCommandFn, | ||
} = require('../../gather/mock-commands.js'); | ||
|
||
/** | ||
* @fileoverview Mock fraggle rock driver for testing. | ||
*/ | ||
|
||
/** @typedef {import('../../../fraggle-rock/gather/driver.js')} Driver */ | ||
/** @typedef {import('../../../gather/driver/execution-context.js')} ExecutionContext */ | ||
|
||
function createMockSession() { | ||
return { | ||
sendCommand: createMockSendCommandFn({useSessionId: false}), | ||
once: createMockOnceFn(), | ||
on: createMockOnFn(), | ||
off: jest.fn(), | ||
}; | ||
} | ||
|
||
function createMockPage() { | ||
return { | ||
goto: jest.fn(), | ||
target: () => ({createCDPSession: () => createMockSession()}), | ||
|
||
/** @return {import('puppeteer').Page} */ | ||
asPage() { | ||
// @ts-expect-error - We'll rely on the tests passing to know this matches. | ||
return this; | ||
}, | ||
}; | ||
} | ||
|
||
function createMockExecutionContext() { | ||
const context = /** @type {ExecutionContext} */ ({}); | ||
return {...context, evaluate: jest.fn(), evaluateAsync: jest.fn()}; | ||
} | ||
|
||
function createMockDriver() { | ||
const session = createMockSession(); | ||
const context = createMockExecutionContext(); | ||
|
||
return { | ||
_page: createMockPage(), | ||
_executionContext: context, | ||
_session: session, | ||
defaultSession: session, | ||
connect: jest.fn(), | ||
evaluate: context.evaluate, | ||
evaluateAsync: context.evaluateAsync, | ||
|
||
/** @return {Driver} */ | ||
asDriver() { | ||
// @ts-expect-error - We'll rely on the tests passing to know this matches. | ||
return this; | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
createMockDriver, | ||
createMockPage, | ||
createMockSession, | ||
}; |
Oops, something went wrong.