Skip to content

Commit

Permalink
core(fr): add page to context (#14359)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Sep 6, 2022
1 parent fd304ed commit 7ecd66b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
9 changes: 6 additions & 3 deletions core/fraggle-rock/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import NetworkRecords from '../../computed/network-records.js';
/**
* @typedef NavigationContext
* @property {Driver} driver
* @property {LH.Puppeteer.Page} page
* @property {LH.Config.FRConfig} config
* @property {LH.Config.NavigationDefn} navigation
* @property {LH.NavigationRequestor} requestor
Expand Down Expand Up @@ -218,6 +219,7 @@ async function _navigation(navigationContext) {
url: initialUrl,
gatherMode: /** @type {const} */ ('navigation'),
driver: navigationContext.driver,
page: navigationContext.page,
computedCache: navigationContext.computedCache,
artifactDefinitions: navigationContext.navigation.artifacts,
artifactState,
Expand Down Expand Up @@ -250,10 +252,10 @@ async function _navigation(navigationContext) {
}

/**
* @param {{driver: Driver, config: LH.Config.FRConfig, requestor: LH.NavigationRequestor; baseArtifacts: LH.FRBaseArtifacts, computedCache: NavigationContext['computedCache']}} args
* @param {{driver: Driver, page: LH.Puppeteer.Page, config: LH.Config.FRConfig, requestor: LH.NavigationRequestor; baseArtifacts: LH.FRBaseArtifacts, computedCache: NavigationContext['computedCache']}} args
* @return {Promise<{artifacts: Partial<LH.FRArtifacts & LH.FRBaseArtifacts>}>}
*/
async function _navigations({driver, config, requestor, baseArtifacts, computedCache}) {
async function _navigations({driver, page, config, requestor, baseArtifacts, computedCache}) {
if (!config.navigations) throw new Error('No navigations configured');

/** @type {Partial<LH.FRArtifacts & LH.FRBaseArtifacts>} */
Expand All @@ -264,6 +266,7 @@ async function _navigations({driver, config, requestor, baseArtifacts, computedC
for (const navigation of config.navigations) {
const navigationContext = {
driver,
page,
navigation,
requestor,
config,
Expand Down Expand Up @@ -333,7 +336,7 @@ async function navigationGather(requestor, options) {
requestor: normalizedRequestor,
};
const {baseArtifacts} = await _setup(context);
const {artifacts} = await _navigations({...context, baseArtifacts, computedCache});
const {artifacts} = await _navigations({...context, page, baseArtifacts, computedCache});
await _cleanup(context);

return finalizeArtifacts(baseArtifacts, artifacts);
Expand Down
3 changes: 3 additions & 0 deletions core/fraggle-rock/gather/runner-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @typedef CollectPhaseArtifactOptions
* @property {import('./driver.js').Driver} driver
* @property {LH.Puppeteer.Page} page
* @property {Array<LH.Config.AnyArtifactDefn>} artifactDefinitions
* @property {ArtifactState} artifactState
* @property {LH.FRBaseArtifacts} baseArtifacts
Expand Down Expand Up @@ -66,6 +67,7 @@ const phaseToPriorPhase = {
async function collectPhaseArtifacts(options) {
const {
driver,
page,
artifactDefinitions,
artifactState,
baseArtifacts,
Expand All @@ -91,6 +93,7 @@ async function collectPhaseArtifacts(options) {
return gatherer[phase]({
gatherMode,
driver,
page,
baseArtifacts,
dependencies,
computedCache,
Expand Down
5 changes: 3 additions & 2 deletions core/fraggle-rock/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import {getBaseArtifacts, finalizeArtifacts} from './base-artifacts.js';
* @return {Promise<LH.Gatherer.FRGatherResult>}
*/
async function snapshotGather(options) {
const {flags = {}} = options;
const {page, flags = {}} = options;
log.setLevel(flags.logLevel || 'error');

const {config} = await initializeConfig('snapshot', options.config, flags);
const driver = new Driver(options.page);
const driver = new Driver(page);
await driver.connect();

/** @type {Map<string, LH.ArbitraryEqualityMap>} */
Expand All @@ -43,6 +43,7 @@ async function snapshotGather(options) {
phase: 'getArtifact',
gatherMode: 'snapshot',
driver,
page,
baseArtifacts,
artifactDefinitions,
artifactState,
Expand Down
5 changes: 3 additions & 2 deletions core/fraggle-rock/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import {getBaseArtifacts, finalizeArtifacts} from './base-artifacts.js';
* @return {Promise<{endTimespanGather(): Promise<LH.Gatherer.FRGatherResult>}>}
*/
async function startTimespanGather(options) {
const {flags = {}} = options;
const {page, flags = {}} = options;
log.setLevel(flags.logLevel || 'error');

const {config} = await initializeConfig('timespan', options.config, flags);
const driver = new Driver(options.page);
const driver = new Driver(page);
await driver.connect();

/** @type {Map<string, LH.ArbitraryEqualityMap>} */
Expand All @@ -34,6 +34,7 @@ async function startTimespanGather(options) {
/** @type {Omit<import('./runner-helpers.js').CollectPhaseArtifactOptions, 'phase'>} */
const phaseOptions = {
driver,
page,
artifactDefinitions,
artifactState,
baseArtifacts,
Expand Down
12 changes: 11 additions & 1 deletion core/test/fraggle-rock/gather/navigation-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ describe('NavigationRunner', () => {
let mockDriver;
/** @type {import('../../../fraggle-rock/gather/driver.js').Driver} */
let driver;
/** @type {LH.Puppeteer.Page} */
let page;
/** @type {LH.Config.FRConfig} */
let config;
/** @type {LH.Config.NavigationDefn} */
Expand Down Expand Up @@ -113,6 +115,7 @@ describe('NavigationRunner', () => {
mockDriver = createMockDriver();
mockDriver.url.mockReturnValue('about:blank');
driver = mockDriver.asDriver();
page = mockDriver._page.asPage();

mocks.reset();
});
Expand Down Expand Up @@ -186,7 +189,7 @@ describe('NavigationRunner', () => {

describe('_navigations', () => {
const run = () =>
runner._navigations({driver, config, requestor, computedCache, baseArtifacts});
runner._navigations({driver, page, config, requestor, computedCache, baseArtifacts});

it('should throw if no navigations available', async () => {
config = {...config, navigations: null};
Expand Down Expand Up @@ -312,6 +315,7 @@ describe('NavigationRunner', () => {
/** @param {LH.Config.NavigationDefn} navigation */
const run = navigation => runner._navigation({
driver,
page,
config,
navigation,
requestor,
Expand All @@ -334,6 +338,7 @@ describe('NavigationRunner', () => {

const {artifacts} = await runner._navigation({
driver,
page,
config,
navigation,
requestor: requestedUrl,
Expand All @@ -351,6 +356,7 @@ describe('NavigationRunner', () => {
it('skips about:blank if using a callback requestor', async () => {
const {artifacts} = await runner._navigation({
driver,
page,
config,
navigation,
requestor: () => {},
Expand Down Expand Up @@ -491,6 +497,7 @@ describe('NavigationRunner', () => {
navigation.blankPage = 'data:text/html;...';
await runner._setupNavigation({
driver,
page,
navigation,
requestor: requestedUrl,
config,
Expand All @@ -507,6 +514,7 @@ describe('NavigationRunner', () => {
it('should prepare target for navigation', async () => {
await runner._setupNavigation({
driver,
page,
navigation,
requestor: requestedUrl,
config,
Expand All @@ -521,6 +529,7 @@ describe('NavigationRunner', () => {
mocks.prepareMock.prepareTargetForIndividualNavigation.mockResolvedValue({warnings});
const result = await runner._setupNavigation({
driver,
page,
navigation,
requestor: requestedUrl,
config,
Expand All @@ -535,6 +544,7 @@ describe('NavigationRunner', () => {
const run = () =>
runner._navigate({
driver,
page,
navigation,
requestor,
config,
Expand Down
7 changes: 7 additions & 0 deletions core/test/fraggle-rock/gather/runner-helpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ describe('collectPhaseArtifacts', () => {

/** @type {ReturnType<ReturnType<typeof createMockDriver>['asDriver']>} */
let driver;
/** @type {LH.Puppeteer.Page} */
let page;
/** @type {ReturnType<typeof createMockDriver>} */
let mockDriver;
/** @type {LH.FRBaseArtifacts} */
Expand All @@ -112,6 +114,7 @@ describe('collectPhaseArtifacts', () => {

beforeEach(() => {
mockDriver = createMockDriver();
page = mockDriver._page.asPage();
driver = mockDriver.asDriver();
artifactState = {
startInstrumentation: {},
Expand All @@ -130,6 +133,7 @@ describe('collectPhaseArtifacts', () => {
const {artifactDefinitions, gatherers} = createGathererSet();
await helpers.collectPhaseArtifacts({
driver,
page,
artifactDefinitions,
artifactState,
phase,
Expand All @@ -154,6 +158,7 @@ describe('collectPhaseArtifacts', () => {
const {artifactDefinitions} = createGathererSet();
await helpers.collectPhaseArtifacts({
driver,
page,
artifactDefinitions,
artifactState,
gatherMode: 'navigation',
Expand Down Expand Up @@ -186,6 +191,7 @@ describe('collectPhaseArtifacts', () => {

await helpers.collectPhaseArtifacts({
driver,
page,
artifactDefinitions,
artifactState,
gatherMode: 'navigation',
Expand Down Expand Up @@ -219,6 +225,7 @@ describe('collectPhaseArtifacts', () => {
const {artifactDefinitions, gatherers} = createGathererSet();
await helpers.collectPhaseArtifacts({
driver,
page,
artifactDefinitions,
artifactState,
gatherMode: 'navigation',
Expand Down
2 changes: 2 additions & 0 deletions types/gatherer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ declare module Gatherer {
gatherMode: GatherMode;
/** The connection to the page being analyzed. */
driver: FRTransitionalDriver;
/** The Puppeteer page handle. Will be undefined in legacy navigation mode. */
page?: LH.Puppeteer.Page;
/** The set of base artifacts that are always collected. */
baseArtifacts: FRBaseArtifacts;
/** The cached results of computed artifacts. */
Expand Down

0 comments on commit 7ecd66b

Please sign in to comment.