UNPKG

@qavajs/steps-playwright

Version:

qavajs steps to interact with playwright

156 lines 5.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Playwright = void 0; const driverProvider_1 = require("./driverProvider"); const test_1 = require("@playwright/test"); class Playwright { drivers = {}; driver; browser; context; page; expect = test_1.expect; driverProvider; constructor(driverProvider) { this.driverProvider = driverProvider; } async launchDriver(key, driverConfig) { const driverInstance = (key === 'default' && this.drivers.default) ? this.drivers.default : await this.driverProvider(driverConfig); this.drivers[key] = driverInstance; this.setDriver(driverInstance); if (driverConfig.isElectron) { this.setContext(driverInstance.context()); this.setPage(await driverInstance.firstWindow()); } else { const firstContext = driverInstance.contexts()[0]; const context = driverConfig.reuseSession && firstContext ? firstContext : await driverInstance.newContext(driverConfig.capabilities); this.setContext(context); const firstPage = this.context?.pages()[0]; const page = driverConfig.reuseSession && firstPage ? firstPage : await this.context.newPage(); this.setPage(page); } this.context.name = 'default'; } async launchContext(key, contextConfig) { if (!this.driver) throw new Error('No active drivers launched'); const newContext = await this.driver.newContext(contextConfig); newContext.name = key; const newPage = await newContext.newPage(); this.setContext(newContext); this.setPage(newPage); } async switchDriver(key) { const currentDriver = this.drivers[key]; if (!currentDriver) throw new Error(`Driver '${key}' was not found`); this.setDriver(currentDriver); this.setContext(this.getDefaultContext()); this.setPage(await this.getFirstPage()); } async switchContext(key) { const currentContext = this.findContext(key); this.setContext(currentContext); this.setPage(await this.getFirstPage()); } async closeContext(key) { const currentContext = this.findContext(key); await currentContext.close(); if (this.context === currentContext) { this.setContext(this.getDefaultContext()); this.setPage(await this.getFirstPage()); } } findContext(key) { const currentContext = this.driver?.contexts().find((ctx) => ctx.name === key); if (!currentContext) throw new Error(`Context '${key}' was not found`); return currentContext; } async closeDriver(driverKey) { const currentDriver = this.drivers[driverKey]; if (!currentDriver) throw new Error(`Driver '${driverKey}' was not found`); await currentDriver.close(); delete this.drivers[driverKey]; if (this.driver === currentDriver) { this.setDriver(this.drivers['default']); this.setContext(this.getDefaultContext()); this.setPage(await this.getFirstPage()); } } /** * return to default state (1 browser, no contexts) */ async teardown({ reuseSession, restartBrowser } = { reuseSession: false, restartBrowser: false }) { this.setDriver(this.drivers['default']); if (reuseSession) return; for (const driverKey in this.drivers) { const driverInstance = this.drivers[driverKey]; if (driverInstance.firstWindow) { await this.electronTeardown(driverInstance, driverKey); } else { await this.browserTeardown(driverInstance, driverKey, restartBrowser); } } } async browserTeardown(driverInstance, driverKey, restartBrowser) { if (driverKey !== 'default' || restartBrowser) { await driverInstance.close(); delete this.drivers[driverKey]; } else { const contexts = driverInstance.contexts(); for (const ctx of contexts) { await ctx.close(); } } } async electronTeardown(driverInstance, driverKey) { await driverInstance.evaluate((main) => { main.app.exit(0); }); delete this.drivers[driverKey]; } async close() { for (const driverKey in this.drivers) { const driverInstance = this.drivers[driverKey]; if (driverInstance.firstWindow) { await this.electronTeardown(driverInstance, driverKey); } else { await driverInstance.close(); } delete this.drivers[driverKey]; } } ; getDefaultContext() { const currentDriver = this.driver; return currentDriver.context ? currentDriver.context() : currentDriver.contexts().at(0); } async getFirstPage() { const currentDriver = this.driver; const currentContext = this.context; return currentDriver.firstWindow ? await currentDriver.firstWindow() : currentContext.pages().at(0); } setDriver(driver) { this.driver = this.browser = driver; } setContext(context) { this.context = context; } setPage(page) { this.page = page; } } exports.Playwright = Playwright; exports.default = new Playwright(driverProvider_1.driverProvider); //# sourceMappingURL=playwright.js.map