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

feat: add Lighthouse runner extension #281

Merged
merged 17 commits into from
Sep 14, 2022
2 changes: 1 addition & 1 deletion __snapshots__/lighthouse-e2e.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
exports[
'Lighthouse stringify Puppeteer script generates a valid desktop flow report 1'
'Lighthouse user flow via stringify generates a valid desktop flow report 1'
] = `
const fs = require('fs');
const puppeteer = require('puppeteer'); // v13.0.0 or later
Expand Down
71 changes: 39 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@
"typescript": "4.6.4"
},
"peerDependencies": {
"puppeteer": ">=16.2.0"
"puppeteer": ">=16.2.0",
"lighthouse": ">=10.0.0"
},
"peerDependenciesMeta": {
"puppeteer": {
"optional": true
},
"lighthouse": {
"optional": true
}
},
"dependencies": {
Expand Down
80 changes: 80 additions & 0 deletions src/lighthouse/LighthouseRunnerExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
Copyright 2022 Google LLC

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

https://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.
*/

import { PuppeteerRunnerExtension } from '../PuppeteerRunnerExtension.js';
import type { Step, UserFlow } from '../Schema.js';
import { isMobileFlow, isNavigationStep } from './helpers.js';

// @ts-expect-error Lighthouse doesn't expose types.
import desktopConfig from 'lighthouse/core/config/desktop-config.js';
// @ts-expect-error Lighthouse doesn't expose types.
import { startFlow } from 'lighthouse/core/fraggle-rock/api.js';
import FlowResult from 'lighthouse/types/lhr/flow';

export class LighthouseRunnerExtension extends PuppeteerRunnerExtension {
#isTimespanRunning = false;
#isNavigationRunning = false;
#lhFlow?: any;

async createFlowResult(): Promise<FlowResult> {
if (!this.#lhFlow) {
throw new Error('Cannot get flow result before running the flow');
}
return this.#lhFlow.createFlowResult();
}

override async beforeAllSteps(flow: UserFlow) {
await super.beforeAllSteps?.(flow);

this.#lhFlow = await startFlow(this.page, {
config: isMobileFlow(flow) ? undefined : desktopConfig,
flags: { screenEmulation: { disabled: true } },
name: flow.title,
});
}

override async beforeEachStep(step: Step, flow: UserFlow) {
await super.beforeEachStep?.(step, flow);
if (step.type === 'setViewport') return;

if (isNavigationStep(step)) {
if (this.#isTimespanRunning) {
await this.#lhFlow.endTimespan();
this.#isTimespanRunning = false;
}
await this.#lhFlow.startNavigation();
this.#isNavigationRunning = true;
} else if (!this.#isTimespanRunning) {
await this.#lhFlow.startTimespan();
this.#isTimespanRunning = true;
}
}

override async afterEachStep(step: Step, flow: UserFlow) {
if (this.#isNavigationRunning) {
await this.#lhFlow.endNavigation();
this.#isNavigationRunning = false;
}
await super.afterEachStep?.(step, flow);
}

override async afterAllSteps(flow: UserFlow) {
if (this.#isTimespanRunning) {
await this.#lhFlow.endTimespan();
}
await super.afterAllSteps?.(flow);
}
}
15 changes: 2 additions & 13 deletions src/lighthouse/LighthouseStringifyExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,14 @@ import { PuppeteerStringifyExtension } from '../PuppeteerStringifyExtension.js';
import type { LineWriter } from '../LineWriter.js';
import type { Step, UserFlow } from '../Schema.js';

function isNavigationStep(step: Step): boolean {
return Boolean(
step.type === 'navigate' ||
step.assertedEvents?.some((event) => event.type === 'navigation')
);
}
import { isNavigationStep, isMobileFlow } from './helpers.js';

export class LighthouseStringifyExtension extends PuppeteerStringifyExtension {
#isProcessingTimespan = false;

override async beforeAllSteps(out: LineWriter, flow: UserFlow) {
out.appendLine(`const fs = require('fs');`);

let isMobile = true;
for (const step of flow.steps) {
if (step.type !== 'setViewport') continue;
isMobile = step.isMobile;
}

await super.beforeAllSteps(out, flow);

const flags = {
Expand All @@ -46,7 +35,7 @@ export class LighthouseStringifyExtension extends PuppeteerStringifyExtension {
},
};
out.appendLine(`const flags = ${JSON.stringify(flags)}`);
if (isMobile) {
if (isMobileFlow(flow)) {
out.appendLine(`const config = undefined;`);
} else {
// eslint-disable-next-line max-len
Expand Down
33 changes: 33 additions & 0 deletions src/lighthouse/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
Copyright 2022 Google LLC

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

https://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.
*/

import type { Step, UserFlow } from '../Schema.js';

export function isNavigationStep(step: Step): boolean {
return Boolean(
step.type === 'navigate' ||
step.assertedEvents?.some((event) => event.type === 'navigation')
);
}

export function isMobileFlow(flow: UserFlow): boolean {
for (const step of flow.steps) {
if (step.type === 'setViewport') {
return step.isMobile;
}
}
return false;
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export { PuppeteerRunnerExtension } from './PuppeteerRunnerExtension.js';
export { PuppeteerRunnerOwningBrowserExtension } from './PuppeteerRunnerExtension.js';
export { PuppeteerStringifyExtension } from './PuppeteerStringifyExtension.js';
export { LighthouseStringifyExtension } from './lighthouse/LighthouseStringifyExtension.js';
export { LighthouseRunnerExtension } from './lighthouse/LighthouseRunnerExtension.js';
Loading