import { Page } from 'puppeteer-core'; import { TestRunnerCoreConfig } from '@web/test-runner-core'; import { V8Coverage, v8ToIstanbul } from '@web/test-runner-coverage-v8'; import { SessionResult } from '@web/test-runner-core'; export class ChromeLauncherPage { private config: TestRunnerCoreConfig; private testFiles: string[]; private product: string; public puppeteerPage: Page; private nativeInstrumentationEnabledOnPage = false; constructor( config: TestRunnerCoreConfig, testFiles: string[], product: string, puppeteerPage: Page, ) { this.config = config; this.testFiles = testFiles; this.product = product; this.puppeteerPage = puppeteerPage; } async runSession(url: string, coverage: boolean) { if ( coverage && this.config.coverageConfig?.nativeInstrumentation !== false && this.product === 'chromium' ) { if (this.nativeInstrumentationEnabledOnPage) { await this.puppeteerPage.coverage.stopJSCoverage(); } this.nativeInstrumentationEnabledOnPage = true; await this.puppeteerPage.coverage.startJSCoverage(); } await this.puppeteerPage.setViewport({ height: 600, width: 800 }); await this.puppeteerPage.goto(url); } async stopSession(): Promise { const testCoverage = await this.collectTestCoverage(this.config, this.testFiles); // navigate to an empty page to kill any running code on the page, stopping timers and // breaking a potential endless reload loop await this.puppeteerPage.goto('about:blank'); return { testCoverage }; } private async collectTestCoverage(config: TestRunnerCoreConfig, testFiles: string[]) { const userAgentPromise = this.puppeteerPage .browser() .userAgent() .catch(() => undefined); try { const coverageFromBrowser = await this.puppeteerPage.evaluate( () => (window as any).__coverage__, ); if (coverageFromBrowser) { // coverage was generated by JS, return that return coverageFromBrowser; } } catch (error) { // evaluate throws when the test navigates in the browser } if (config.coverageConfig?.nativeInstrumentation === false) { throw new Error( 'Coverage is enabled with nativeInstrumentation disabled. ' + 'Expected coverage provided in the browser as a global __coverage__ variable.' + 'Use a plugin like babel-plugin-istanbul to generate the coverage, or enable native instrumentation.', ); } if (!this.nativeInstrumentationEnabledOnPage) { return undefined; } // get native coverage from puppeteer // TODO: this is using a private puppeteer API to grab v8 code coverage, this can be removed // when https://github.com/puppeteer/puppeteer/issues/2136 is resolved const response = (await (this.puppeteerPage as any)._client.send( 'Profiler.takePreciseCoverage', )) as { result: V8Coverage[]; }; const v8Coverage = response.result // remove puppeteer specific scripts .filter(r => r.url && r.url !== '__puppeteer_evaluation_script__'); const userAgent = await userAgentPromise; await this.puppeteerPage.coverage?.stopJSCoverage(); this.nativeInstrumentationEnabledOnPage = false; return v8ToIstanbul(config, testFiles, v8Coverage, userAgent); } }