@web/test-runner-playwright
Version:
Playwright browser launcher for Web Test Runner
75 lines • 3.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlaywrightLauncherPage = void 0;
const test_runner_coverage_v8_1 = require("@web/test-runner-coverage-v8");
class PlaywrightLauncherPage {
config;
testFiles;
product;
playwrightContext;
playwrightPage;
nativeInstrumentationEnabledOnPage = false;
constructor(config, product, testFiles, playwrightContext, playwrightPage) {
this.config = config;
this.product = product;
this.testFiles = testFiles;
this.playwrightContext = playwrightContext;
this.playwrightPage = playwrightPage;
}
async runSession(url, coverage) {
if (coverage &&
this.product === 'chromium' &&
this.config.coverageConfig?.nativeInstrumentation !== false) {
if (this.nativeInstrumentationEnabledOnPage) {
await this.playwrightPage.coverage.stopJSCoverage();
}
this.nativeInstrumentationEnabledOnPage = true;
await this.playwrightPage.coverage.startJSCoverage();
}
await this.playwrightPage.setViewportSize({ height: 600, width: 800 });
await this.playwrightPage.goto(url);
}
async stopSession() {
const testCoverage = this.nativeInstrumentationEnabledOnPage
? await this.collectTestCoverage(this.config, this.testFiles)
: undefined;
// navigate to an empty page to kill any running code on the page, stopping timers and
// breaking a potential endless reload loop
await this.playwrightPage.goto('about:blank');
await this.playwrightContext.close();
return { testCoverage };
}
async collectTestCoverage(config, testFiles) {
const userAgentPromise = this.playwrightPage
.evaluate(() => window.navigator.userAgent)
.catch(() => undefined);
try {
const coverageFromBrowser = await this.playwrightPage.evaluate(() => window.__coverage__);
if (coverageFromBrowser) {
// coverage was generated by JS, return that
return coverageFromBrowser;
}
}
catch {
// evaluate throws when the a 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.');
}
// get native coverage from playwright
let coverage;
if (this.product === 'chromium') {
coverage = await this.playwrightPage?.coverage?.stopJSCoverage();
}
else {
coverage = [];
}
this.nativeInstrumentationEnabledOnPage = false;
const userAgent = await userAgentPromise;
return (0, test_runner_coverage_v8_1.v8ToIstanbul)(config, testFiles, coverage, userAgent);
}
}
exports.PlaywrightLauncherPage = PlaywrightLauncherPage;
//# sourceMappingURL=PlaywrightLauncherPage.js.map