Skip to content

docs(recipes): update custom-gatherer-puppeteer to use FR #13940

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

Merged
merged 17 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions docs/recipes/custom-gatherer-puppeteer/custom-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
*/

export default {
passes: [{
passName: 'defaultPass',
gatherers: [
'custom-gatherer',
],
}],
artifacts: [
{id: 'CustomGatherer', gatherer: 'custom-gatherer'},
],

navigations: [
{
id: 'default',
artifacts: [
'CustomGatherer',
],
},
],

audits: [
'custom-audit',
Expand Down
25 changes: 7 additions & 18 deletions docs/recipes/custom-gatherer-puppeteer/custom-gatherer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@
/* global document */

import {Gatherer} from 'lighthouse';
import puppeteer from 'puppeteer';

async function connect(driver) {
const browser = await puppeteer.connect({
browserWSEndpoint: await driver.wsEndpoint(),
defaultViewport: null,
});
const {targetInfo} = await driver.sendCommand('Target.getTargetInfo');
const puppeteerTarget = (await browser.targets())
.find(target => target._targetId === targetInfo.targetId);
const page = await puppeteerTarget.page();
return {browser, page, executionContext: driver.executionContext};
}

class CustomGatherer extends Gatherer {
async afterPass(options) {
const {driver} = options;
const {page, executionContext} = await connect(driver);
meta = {
supportedModes: ['navigation', 'timespan', 'snapshot'],
};

async getArtifact(context) {
const {driver, page} = context;
const {executionContext} = driver;

// Inject an input field for our debugging pleasure.
function makeInput() {
Expand All @@ -41,8 +32,6 @@ class CustomGatherer extends Gatherer {
const value = await executionContext.evaluateAsync(`document.querySelector('input').value`);
if (value !== '123') throw new Error('huh?');

// No need to close the browser or page. Puppeteer doesn't own either of them.

return {value};
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/recipes/custom-gatherer-puppeteer/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# Make sure we're in this `docs/recipes/customer-gatherer-puppeteer` directory
cd "$(dirname "$0")"

node node_modules/.bin/lighthouse --legacy-navigation --config-path=custom-config.js https://www.example.com --output=json |
node node_modules/.bin/lighthouse --config-path=custom-config.js https://www.example.com --output=json |
jq '.audits["custom-audit"].score' |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to see the recipe tests paying off :)

grep -q 1