diff --git a/lighthouse-core/scripts/pptr-run-devtools.js b/lighthouse-core/scripts/pptr-run-devtools.js new file mode 100644 index 000000000000..578f17870467 --- /dev/null +++ b/lighthouse-core/scripts/pptr-run-devtools.js @@ -0,0 +1,191 @@ +/** + * @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'; + +/** + * USAGE: + * Make sure CHROME_PATH is set to a modern version of Chrome. + * This script won't work on older versions that use the "Audits" panel. + * + * URL list file: yarn run-devtools < path/to/urls.txt + * Single URL: yarn run-devtools "https://example.com" + */ + +const puppeteer = require('puppeteer'); +const fs = require('fs'); +const readline = require('readline'); +const yargs = require('yargs/yargs'); + +/** @typedef {{result?: {value?: string, objectId?: number}, exceptionDetails?: object}} RuntimeEvaluateResponse */ + +const argv = yargs(process.argv.slice(2)) + .usage('$0 [url]') + .help('help').alias('help', 'h') + .option('_', {type: 'string'}) + .option('output-dir', { + type: 'string', + default: 'latest-run/devtools-lhrs', + alias: 'o', + }) + .option('custom-devtools-frontend', { + type: 'string', + alias: 'd', + }) + .argv; + +/** + * https://source.chromium.org/chromium/chromium/src/+/master:third_party/devtools-frontend/src/front_end/test_runner/TestRunner.js;l=170;drc=f59e6de269f4f50bca824f8ca678d5906c7d3dc8 + * @param {Record} receiver + * @param {string} methodName + * @param {function} override + */ +function addSniffer(receiver, methodName, override) { + const original = receiver[methodName]; + if (typeof original !== 'function') { + throw new Error('Cannot find method to override: ' + methodName); + } + + /** + * @param {...any} args + */ + receiver[methodName] = function(...args) { + let result; + try { + result = original.apply(this, args); + } finally { + receiver[methodName] = original; + } + // In case of exception the override won't be called. + try { + Array.prototype.push.call(args, result); + override.apply(this, args); + } catch (e) { + throw new Error('Exception in overriden method \'' + methodName + '\': ' + e); + } + return result; + }; +} + +const sniffLhr = ` +new Promise(resolve => { + (${addSniffer.toString()})( + UI.panels.lighthouse.__proto__, + '_buildReportUI', + (lhr, artifacts) => resolve(lhr) + ); +}); +`; + +const startLighthouse = ` +(async () => { + const ViewManager = UI.ViewManager.ViewManager || UI.ViewManager; + await ViewManager.instance().showView('lighthouse'); + const button = UI.panels.lighthouse.contentElement.querySelector('button'); + if (button.disabled) throw new Error('Start button disabled'); + button.click(); +})() +`; + +/** + * @param {import('puppeteer').Browser} browser + * @param {string} url + * @return {Promise} + */ +async function testPage(browser, url) { + const page = await browser.newPage(); + + const targets = await browser.targets(); + const inspectorTarget = targets.filter(t => t.url().includes('devtools'))[1]; + if (!inspectorTarget) throw new Error('No inspector found.'); + + const session = await inspectorTarget.createCDPSession(); + await session.send('Runtime.enable'); + + // Navigate to page and wait for initial HTML to be parsed before trying to start LH. + await new Promise((resolve, reject) => { + page.target().createCDPSession() + .then(session => { + session.send('Page.enable').then(() => { + session.once('Page.domContentEventFired', resolve); + page.goto(url); + }); + }) + .catch(reject); + }); + + /** @type {RuntimeEvaluateResponse|undefined} */ + let startLHResponse; + while (!startLHResponse || startLHResponse.exceptionDetails) { + startLHResponse = await session.send('Runtime.evaluate', { + expression: startLighthouse, + awaitPromise: true, + }).catch(err => ({exceptionDetails: err})); + } + + /** @type {RuntimeEvaluateResponse} */ + const remoteLhrResponse = await session.send('Runtime.evaluate', { + expression: sniffLhr, + awaitPromise: true, + returnByValue: true, + }).catch(err => err); + + if (!remoteLhrResponse.result || !remoteLhrResponse.result.value) { + throw new Error('Problem sniffing LHR.'); + } + + await page.close(); + + return JSON.stringify(remoteLhrResponse.result.value); +} + +/** + * @return {Promise} + */ +async function readUrlList() { + if (argv._[0]) return [argv._[0]]; + + /** @type {string[]} */ + const urlList = []; + const rl = readline.createInterface(process.stdin, process.stdout); + + rl.on('line', async line => { + if (line.startsWith('#')) return; + urlList.push(line); + }); + + return new Promise(resolve => rl.on('close', () => resolve(urlList))); +} + +async function run() { + const outputDir = argv['output-dir']; + + // Create output directory. + if (fs.existsSync(outputDir)) { + if (fs.readdirSync(outputDir).length) { + // eslint-disable-next-line no-console + console.warn('WARNING: Output directory is not empty.'); + } + } else { + fs.mkdirSync(outputDir); + } + + const customDevtools = argv['custom-devtools-frontend']; + + const browser = await puppeteer.launch({ + executablePath: process.env.CHROME_PATH, + args: customDevtools ? [`--custom-devtools-frontend=${customDevtools}`] : [], + devtools: true, + }); + + const urlList = await readUrlList(); + for (let i = 0; i < urlList.length; ++i) { + const lhr = await testPage(browser, urlList[i]); + fs.writeFileSync(`${argv.o}/lhr-${i}.json`, lhr); + } + + await browser.close(); +} +run(); diff --git a/lighthouse-core/test/chromium-web-tests/test-page-devtools.sh b/lighthouse-core/test/chromium-web-tests/test-page-devtools.sh deleted file mode 100644 index 3619ff44c257..000000000000 --- a/lighthouse-core/test/chromium-web-tests/test-page-devtools.sh +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env bash - -## -# @license Copyright 2020 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. -## - -# USAGE: -# -# URL list file: yarn run-devtools < lighthouse-core/scripts/gcp-collection/urls.txt -# yarn run-devtools lighthouse-core/scripts/gcp-collection/urls.txt -# Single URL: echo "https://example.com" | yarn run-devtools -# - -set -euo pipefail - -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -export LH_ROOT="$( cd "$SCRIPT_DIR/../../.." && pwd )" - -# Setup dependencies. -TEST_DIR="$LH_ROOT/.tmp/chromium-web-tests" -export DEPOT_TOOLS_PATH="$TEST_DIR/depot-tools" -export DEVTOOLS_PATH=${DEVTOOLS_PATH:-"$TEST_DIR/devtools/devtools-frontend"} -export BLINK_TOOLS_PATH="$TEST_DIR/blink_tools" -export PATH=$DEPOT_TOOLS_PATH:$PATH - -bash "$SCRIPT_DIR/download-depot-tools.sh" -bash "$SCRIPT_DIR/download-devtools.sh" -bash "$SCRIPT_DIR/download-blink-tools.sh" -bash "$SCRIPT_DIR/download-content-shell.sh" - -bash "$SCRIPT_DIR/roll-devtools.sh" - -WEB_TEST_DIR="$DEVTOOLS_PATH/test/webtests/http/tests/devtools/lighthouse-run" - -# Remove any tests leftover from previous run. -if [ -d "$WEB_TEST_DIR" ]; then - rm -rf "$WEB_TEST_DIR" -fi -mkdir -p "$WEB_TEST_DIR" - -echo "Creating test files for URLs" -COUNTER=0 -while read url; do - [[ -z $url || ${url:0:1} == "#" ]] && continue - echo " - (async function() { - await TestRunner.navigatePromise('$url'); - - await TestRunner.loadModule('lighthouse_test_runner'); - await TestRunner.showPanel('lighthouse'); - - LighthouseTestRunner.getRunButton().click(); - const {lhr} = await LighthouseTestRunner.waitForResults(); - TestRunner.addResult(JSON.stringify(lhr)); - - TestRunner.completeTest(); - })(); - " > "$WEB_TEST_DIR/lighthouse-run-$COUNTER.js" - COUNTER=$[$COUNTER +1] -done <"${1:-/dev/stdin}" - -set +e -bash "$SCRIPT_DIR/web-test-server.sh" \ - --no-show-results \ - --no-retry-failures \ - --time-out-ms=60000 \ - --additional-driver-flag=--disable-blink-features=TrustTokens,TrustTokensAlwaysAllowIssuance \ - http/tests/devtools/lighthouse-run -set -e - -OUTPUT_DIR="$LH_ROOT/latest-run/devtools-lhrs" -if [ -d "$OUTPUT_DIR" ]; then - rm -rf "$OUTPUT_DIR" -fi -mkdir -p "$OUTPUT_DIR" - -# Copy results to latest-run folder. -# Sometimes there will be extra output before the line with LHR. To get around this, only copy the last line with content. -COUNTER=0 -for file in "$LH_ROOT/.tmp/layout-test-results/http/tests/devtools/lighthouse-run"/lighthouse-run-*-actual.txt; do - grep "lighthouseVersion" -m 1 "$file" > "$OUTPUT_DIR/devtools-lhr-$COUNTER.json" - COUNTER=$[$COUNTER +1] -done - -echo "Open the LHRs at $OUTPUT_DIR" diff --git a/package.json b/package.json index 2cd61dfb7fbb..cc674538fe44 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "update:test-devtools": "bash lighthouse-core/test/chromium-web-tests/test-locally.sh --reset-results", "test-devtools": "bash lighthouse-core/test/chromium-web-tests/test-locally.sh", "open-devtools": "bash lighthouse-core/scripts/open-devtools.sh", - "run-devtools": "bash lighthouse-core/test/chromium-web-tests/test-page-devtools.sh", + "run-devtools": "node lighthouse-core/scripts/pptr-run-devtools.js", "diff:sample-json": "yarn i18n:checks && bash lighthouse-core/scripts/assert-golden-lhr-unchanged.sh", "computeBenchmarkIndex": "./lighthouse-core/scripts/benchmark.js", "minify-latest-run": "./lighthouse-core/scripts/lantern/minify-trace.js ./latest-run/defaultPass.trace.json ./latest-run/defaultPass.trace.min.json && ./lighthouse-core/scripts/lantern/minify-devtoolslog.js ./latest-run/defaultPass.devtoolslog.json ./latest-run/defaultPass.devtoolslog.min.json",