UNPKG

@best/runner-headless

Version:

Best Runner (Headless)

114 lines 3.88 kB
"use strict"; /* * Copyright (c) 2019, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const os_1 = __importDefault(require("os")); const puppeteer_1 = __importDefault(require("puppeteer")); const trace_1 = require("./trace"); const BROWSER_ARGS = [ '--no-sandbox', `--js-flags=--expose-gc`, '--disable-infobars', '--disable-background-networking', '--disable-extensions', '--disable-translate', '--no-first-run', '--ignore-certificate-errors', '--enable-precise-memory-info', ]; const PUPPETEER_OPTIONS = { args: BROWSER_ARGS }; function tempDir() { const TEMP_DIR_PREFIX = 'runner-headless-temp'; return fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), TEMP_DIR_PREFIX)); } class HeadlessBrowser { pageUrl; projectConfig; tracePath; tracingEnabled; browser; page; pageError; constructor(url, projectConfig) { this.pageUrl = url; this.projectConfig = projectConfig; this.tracePath = path_1.default.resolve(tempDir(), 'trace.json'); this.tracingEnabled = projectConfig.metrics.includes('paint') || projectConfig.metrics.includes('layout'); } async initialize() { const runnerConfig = this.projectConfig.benchmarkRunnerConfig || {}; const { launchOptions = {} } = runnerConfig; this.browser = await puppeteer_1.default.launch({ ...PUPPETEER_OPTIONS, ...launchOptions }); this.page = await this.browser.newPage(); this.page.on('pageerror', (error) => (this.pageError = error)); await this.page.goto(this.pageUrl); this.checkForErrors(); } checkForErrors() { const pageError = this.pageError; if (pageError) { pageError.message = 'Benchmark parse error.\n' + pageError.message; throw pageError; } } async processTrace(result) { if (!this.page) return result; if (this.tracingEnabled) { const traces = await (0, trace_1.parseTrace)(this.tracePath); (0, trace_1.mergeTracedMetrics)(result, traces); } return result; } async close() { if (this.tracingEnabled) await (0, trace_1.removeTrace)(this.tracePath); if (this.browser) { try { await this.browser.close(); } catch (err) { console.log('[pupeteer] - close error', err); } } } reloadPage() { if (this.page) { return this.page.reload(); } } async evaluate(fn, payload) { let result; if (this.page) { if (this.tracingEnabled) await this.page.tracing.start({ path: this.tracePath }); result = await this.page.evaluate(fn, payload); if (this.tracingEnabled) await this.page.tracing.stop(); this.checkForErrors(); result = await this.processTrace(result); } return result; } version() { return this.browser ? this.browser.version() : Promise.resolve('unknown'); } static async getSpecs() { // TODO: Create pupeteer test so we fail when upgrading return [ { name: 'chrome.headless', version: '140' }, { name: 'chrome', version: '140' }, ]; } } exports.default = HeadlessBrowser; //# sourceMappingURL=headless.js.map