Skip to content

Commit

Permalink
core(fr): convert additional base artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Jun 1, 2021
1 parent c3698d3 commit 412612c
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 45 deletions.
9 changes: 9 additions & 0 deletions lighthouse-core/fraggle-rock/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const artifacts = {
Accessibility: '',
AnchorElements: '',
AppCacheManifest: '',
BenchmarkIndex: '',
CacheContents: '',
ConsoleMessages: '',
CSSUsage: '',
Expand All @@ -25,6 +26,8 @@ const artifacts = {
FormElements: '',
FullPageScreenshot: '',
GlobalListeners: '',
HostFormFactor: '',
HostUserAgent: '',
IFrameElements: '',
ImageElements: '',
InstallabilityErrors: '',
Expand Down Expand Up @@ -57,6 +60,9 @@ for (const key of Object.keys(artifacts)) {
const defaultConfig = {
artifacts: [
// Artifacts which can be depended on come first.
{id: artifacts.HostUserAgent, gatherer: 'host-user-agent'},
{id: artifacts.HostFormFactor, gatherer: 'host-form-factor'},
{id: artifacts.BenchmarkIndex, gatherer: 'benchmark-index'},
{id: artifacts.DevtoolsLog, gatherer: 'devtools-log'},
{id: artifacts.Trace, gatherer: 'trace'},

Expand Down Expand Up @@ -109,6 +115,9 @@ const defaultConfig = {
cpuQuietThresholdMs: 1000,
artifacts: [
// Artifacts which can be depended on come first.
artifacts.HostUserAgent,
artifacts.HostFormFactor,
artifacts.BenchmarkIndex,
artifacts.DevtoolsLog,
artifacts.Trace,

Expand Down
20 changes: 4 additions & 16 deletions lighthouse-core/fraggle-rock/gather/base-artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,11 @@
*/
'use strict';

const {getBrowserVersion, getBenchmarkIndex} = require('../../gather/driver/environment.js');

/**
* @param {LH.Config.FRConfig} config
* @param {LH.Gatherer.FRTransitionalDriver} driver
* @return {Promise<LH.BaseArtifacts>}
*/
async function getBaseArtifacts(config, driver) {
const HostUserAgent = (await getBrowserVersion(driver.defaultSession)).userAgent;

// Whether Lighthouse was run on a mobile device (i.e. not on a desktop machine).
const HostFormFactor =
HostUserAgent.includes('Android') || HostUserAgent.includes('Mobile') ? 'mobile' : 'desktop';

const BenchmarkIndex = await getBenchmarkIndex(driver.executionContext);

async function getBaseArtifacts(config) {
/** @type {Array<string | LH.IcuMessage>} */
const LighthouseRunWarnings = [];

Expand All @@ -33,14 +22,13 @@ async function getBaseArtifacts(config, driver) {
Timing: [],
LighthouseRunWarnings,
settings: config.settings,
// Environment artifacts that can always be computed.
HostFormFactor,
HostUserAgent,
BenchmarkIndex,
// Contextual artifacts whose collection changes based on gather mode.
URL: {requestedUrl: '', finalUrl: ''},
PageLoadError: null, // TODO(FR-COMPAT): support PageLoadError
// Artifacts that have been replaced by regular gatherers in Fraggle Rock.
HostFormFactor: 'mobile',
HostUserAgent: '',
BenchmarkIndex: 0,
Stacks: [],
NetworkUserAgent: '',
WebAppManifest: null, // replaced by standard gatherer
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/fraggle-rock/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function _setup({driver, config, requestedUrl}) {
await driver.connect();
await gotoURL(driver, defaultNavigationConfig.blankPage, {waitUntil: ['navigated']});

const baseArtifacts = await getBaseArtifacts(config, driver);
const baseArtifacts = await getBaseArtifacts(config);
baseArtifacts.URL.requestedUrl = requestedUrl;

await prepare.prepareTargetForNavigationMode(driver, config.settings);
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/fraggle-rock/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function snapshot(options) {

return Runner.run(
async () => {
const baseArtifacts = await getBaseArtifacts(config, driver);
const baseArtifacts = await getBaseArtifacts(config);
baseArtifacts.URL.requestedUrl = url;
baseArtifacts.URL.finalUrl = url;

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/fraggle-rock/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function startTimespan(options) {
const finalUrl = await options.page.url();
return Runner.run(
async () => {
const baseArtifacts = await getBaseArtifacts(config, driver);
const baseArtifacts = await getBaseArtifacts(config);
baseArtifacts.URL.requestedUrl = requestedUrl;
baseArtifacts.URL.finalUrl = finalUrl;

Expand Down
29 changes: 29 additions & 0 deletions lighthouse-core/gather/gatherers/benchmark-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @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 FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
const {getBenchmarkIndex} = require('../../gather/driver/environment.js');

class BenchmarkIndex extends FRGatherer {
static symbol = Symbol('BenchmarkIndex');

/** @type {LH.Gatherer.GathererMeta} */
meta = {
symbol: BenchmarkIndex.symbol,
supportedModes: ['snapshot', 'timespan', 'navigation'],
};

/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @return {Promise<LH.Artifacts['BenchmarkIndex']>}
*/
async getArtifact(context) {
return getBenchmarkIndex(context.driver.executionContext);
}
}

module.exports = BenchmarkIndex;
31 changes: 31 additions & 0 deletions lighthouse-core/gather/gatherers/host-form-factor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @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 FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
const HostUserAgent = require('./host-user-agent.js');

class HostFormFactor extends FRGatherer {
static symbol = Symbol('HostFormFactor');

/** @type {LH.Gatherer.GathererMeta<'HostUserAgent'>} */
meta = {
symbol: HostFormFactor.symbol,
supportedModes: ['snapshot', 'timespan', 'navigation'],
dependencies: {HostUserAgent: HostUserAgent.symbol},
};

/**
* @param {LH.Gatherer.FRTransitionalContext<'HostUserAgent'>} context
* @return {Promise<LH.Artifacts['HostFormFactor']>}
*/
async getArtifact(context) {
const userAgent = context.dependencies.HostUserAgent;
return userAgent.includes('Android') || userAgent.includes('Mobile') ? 'mobile' : 'desktop';
}
}

module.exports = HostFormFactor;
29 changes: 29 additions & 0 deletions lighthouse-core/gather/gatherers/host-user-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @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 FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
const {getBrowserVersion} = require('../driver/environment.js');

class HostUserAgent extends FRGatherer {
static symbol = Symbol('HostUserAgent');

/** @type {LH.Gatherer.GathererMeta} */
meta = {
symbol: HostUserAgent.symbol,
supportedModes: ['snapshot', 'timespan', 'navigation'],
};

/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @return {Promise<LH.Artifacts['HostUserAgent']>}
*/
getArtifact(context) {
return getBrowserVersion(context.driver.defaultSession).then(v => v.userAgent);
}
}

module.exports = HostUserAgent;
4 changes: 2 additions & 2 deletions lighthouse-core/test/fraggle-rock/api-test-pptr.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('Fraggle Rock API', () => {

const {auditResults, erroredAudits, failedAudits} = getAuditsBreakdown(lhr);
// TODO(FR-COMPAT): This assertion can be removed when full compatibility is reached.
expect(auditResults.length).toMatchInlineSnapshot(`28`);
expect(auditResults.length).toMatchInlineSnapshot(`29`);

expect(erroredAudits).toHaveLength(0);
expect(failedAudits.map(audit => audit.id)).toContain('errors-in-console');
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('Fraggle Rock API', () => {
const {lhr} = result;
const {auditResults, failedAudits, erroredAudits} = getAuditsBreakdown(lhr);
// TODO(FR-COMPAT): This assertion can be removed when full compatibility is reached.
expect(auditResults.length).toMatchInlineSnapshot(`114`);
expect(auditResults.length).toMatchInlineSnapshot(`116`);
expect(erroredAudits).toHaveLength(0);

const failedAuditIds = failedAudits.map(audit => audit.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,13 @@ describe('NavigationRunner', () => {

it('should collect base artifacts', async () => {
const {baseArtifacts} = await runner._setup({driver, config, requestedUrl});
expect(baseArtifacts).toMatchObject({HostUserAgent: 'Chrome', URL: {requestedUrl}});
expect(baseArtifacts).toMatchObject({URL: {requestedUrl}});
});

it('should prepare the target for navigation', async () => {
await runner._setup({driver, config, requestedUrl});
expect(mocks.prepareMock.prepareTargetForNavigationMode).toHaveBeenCalledTimes(1);
});

it('should prepare the target for navigation *after* base artifact collection', async () => {
mockDriver._session.sendCommand.mockReset();
mockDriver._session.sendCommand.mockRejectedValue(new Error('Not available'));
const setupPromise = runner._setup({driver, config, requestedUrl});
await expect(setupPromise).rejects.toThrowError(/Not available/);
expect(mocks.prepareMock.prepareTargetForNavigationMode).not.toHaveBeenCalled();
});
});

describe('_navigations', () => {
Expand Down
14 changes: 7 additions & 7 deletions types/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ declare global {
fetchTime: string;
/** A set of warnings about unexpected things encountered while loading and testing the page. */
LighthouseRunWarnings: Array<string | IcuMessage>;
/** Device which Chrome is running on. */
HostFormFactor: 'desktop'|'mobile';
/** The user agent string of the version of Chrome used. */
HostUserAgent: string;
/** The benchmark index that indicates rough device class. */
BenchmarkIndex: number;
/** An object containing information about the testing configuration used by Lighthouse. */
settings: Config.Settings;
/** The timing instrumentation of the gather portion of a run. */
Expand All @@ -72,6 +66,12 @@ declare global {
* The set of base artifacts that were replaced by standard gatherers in Fraggle Rock.
*/
export interface LegacyBaseArtifacts {
/** Device which Chrome is running on. */
HostFormFactor: 'desktop'|'mobile';
/** The user agent string of the version of Chrome used. */
HostUserAgent: string;
/** The benchmark index that indicates rough device class. */
BenchmarkIndex: number;
/** The user agent string that Lighthouse used to load the page. Set to the empty string if unknown. */
NetworkUserAgent: string;
/** Information on detected tech stacks (e.g. JS libraries) used by the page. */
Expand Down Expand Up @@ -114,7 +114,7 @@ declare global {
* Artifacts provided by the default gatherers. Augment this interface when adding additional
* gatherers. Changes to these artifacts are not considered a breaking Lighthouse change.
*/
export interface GathererArtifacts extends PublicGathererArtifacts {
export interface GathererArtifacts extends PublicGathererArtifacts,LegacyBaseArtifacts {
/** The results of running the aXe accessibility tests on the page. */
Accessibility: Artifacts.Accessibility;
/** Array of all anchors on the page. */
Expand Down
9 changes: 1 addition & 8 deletions types/gatherer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ declare global {
trace?: Trace;
}

export type PhaseArtifact = |
LH.GathererArtifacts[keyof LH.GathererArtifacts] |
LH.Artifacts['devtoolsLogs'] |
LH.Artifacts['traces'] |
LH.Artifacts['WebAppManifest'] |
LH.Artifacts['InstallabilityErrors'] |
LH.Artifacts['Stacks'];
export type PhaseResultNonPromise = void|PhaseArtifact
export type PhaseResultNonPromise = void | LH.GathererArtifacts[keyof LH.GathererArtifacts];
export type PhaseResult = PhaseResultNonPromise | Promise<PhaseResultNonPromise>

export type GatherMode = 'snapshot'|'timespan'|'navigation';
Expand Down

0 comments on commit 412612c

Please sign in to comment.