UNPKG

@web/test-runner-playwright

Version:
125 lines 4.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlaywrightLauncher = void 0; const playwright_1 = __importDefault(require("playwright")); const PlaywrightLauncherPage_js_1 = require("./PlaywrightLauncherPage.js"); function capitalize(str) { return `${str[0].toUpperCase()}${str.substring(1)}`; } class PlaywrightLauncher { name; type = 'playwright'; concurrency; product; launchOptions; createBrowserContextFn; createPageFn; config; testFiles; browser; debugBrowser; activePages = new Map(); activeDebugPages = new Map(); testCoveragePerSession = new Map(); __launchBrowserPromise; __experimentalWindowFocus__; constructor(product, launchOptions, createBrowserContextFn, createPageFn, __experimentalWindowFocus__, concurrency) { this.product = product; this.launchOptions = launchOptions; this.createBrowserContextFn = createBrowserContextFn; this.createPageFn = createPageFn; this.concurrency = concurrency; this.name = capitalize(product); this.__experimentalWindowFocus__ = !!__experimentalWindowFocus__; } async initialize(config, testFiles) { this.config = config; this.testFiles = testFiles; } async stop() { if (this.browser?.isConnected()) { await this.browser.close(); } if (this.debugBrowser?.isConnected()) { await this.debugBrowser.close(); } } async startSession(sessionId, url) { const browser = await this.getOrStartBrowser(); const page = await this.createNewPage(browser); this.activePages.set(sessionId, page); this.testCoveragePerSession.delete(sessionId); await page.runSession(url, !!this.config?.coverage); } isActive(sessionId) { return this.activePages.has(sessionId); } getBrowserUrl(sessionId) { return this.getPage(sessionId).url(); } async startDebugSession(sessionId, url) { if (!this.debugBrowser) { this.debugBrowser = await playwright_1.default[this.product].launch({ ...this.launchOptions, // devtools is only supported on chromium devtools: this.product === 'chromium', headless: false, }); } const page = await this.createNewPage(this.debugBrowser); this.activeDebugPages.set(sessionId, page); page.playwrightPage.on('close', () => { this.activeDebugPages.delete(sessionId); }); await page.runSession(url, false); } async createNewPage(browser) { const playwrightContext = await this.createBrowserContextFn({ config: this.config, browser }); const playwrightPage = await this.createPageFn({ config: this.config, browser, context: playwrightContext, }); return new PlaywrightLauncherPage_js_1.PlaywrightLauncherPage(this.config, this.product, this.testFiles, playwrightContext, playwrightPage); } async stopSession(sessionId) { const page = this.activePages.get(sessionId); if (!page) { throw new Error(`No page for session ${sessionId}`); } if (page.playwrightPage.isClosed()) { throw new Error(`Session ${sessionId} is already stopped`); } const result = await page.stopSession(); this.activePages.delete(sessionId); return result; } async getOrStartBrowser() { if (this.__launchBrowserPromise) { return this.__launchBrowserPromise; } if (!this.browser || !this.browser?.isConnected()) { this.__launchBrowserPromise = (async () => { const browser = await playwright_1.default[this.product].launch(this.launchOptions); return browser; })(); const browser = await this.__launchBrowserPromise; this.browser = browser; this.__launchBrowserPromise = undefined; } return this.browser; } getPage(sessionId) { const page = this.activePages.get(sessionId)?.playwrightPage ?? this.activeDebugPages.get(sessionId)?.playwrightPage; if (!page) { throw new Error(`Could not find a page for session ${sessionId}`); } return page; } } exports.PlaywrightLauncher = PlaywrightLauncher; //# sourceMappingURL=PlaywrightLauncher.js.map