UNPKG

@serenity-js/playwright

Version:

Adapter that integrates @serenity-js/web with Playwright, enabling Serenity/JS reporting and using the Screenplay Pattern to write component and end-to-end test scenarios

70 lines 2.48 kB
import * as playwright from 'playwright-core'; import { PlaywrightBrowsingSessionWithElectron } from './PlaywrightBrowsingSessionWithElectron.js'; /** * Self-launching implementation of [`PlaywrightBrowsingSession`](https://serenity-js.org/api/playwright/class/PlaywrightBrowsingSession/) * for Electron applications. * * This class launches the Electron application on first use and closes it when discarded. * Use this for test runners like Mocha or Jasmine that don't manage Electron lifecycle. * * ## Example * * ```typescript * import { actorCalled } from '@serenity-js/core'; * import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright'; * * const actor = actorCalled('Tester').whoCan( * BrowseTheWebWithPlaywright.launchingElectronApp({ * args: ['path/to/main.js'], * cwd: 'path/to/app', * }) * ); * * // The app is automatically closed when the actor is dismissed * ``` * * @group Models */ export class SelfLaunchingPlaywrightBrowsingSessionWithElectron extends PlaywrightBrowsingSessionWithElectron { launchOptions; constructor(launchOptions, extraBrowserContextOptions, selectors) { // setting electronApp to undefined since it's lazily initialised super(undefined, extraBrowserContextOptions, selectors); this.launchOptions = launchOptions; } /** * Launches the Electron application using the configured launch options. * * This method is idempotent - calling it multiple times will only launch * the application once. */ async initialise() { if (this.electronApp) { return; } this.electronApp = await playwright._electron.launch(this.launchOptions); this.currentBrowserPage = await this.registerCurrentPage(); } /** * Returns `true` if the Electron application has been launched. */ isInitialised() { return this.electronApp !== undefined; } /** * Closes the Electron application that was launched by this session. * Called when the ability is discarded. */ async closeElectronApp() { if (this.electronApp) { await this.electronApp.close(); this.electronApp = undefined; } } async discard() { await this.closeElectronApp(); this.pages.clear(); this.currentBrowserPage = undefined; } } //# sourceMappingURL=SelfLaunchingPlaywrightBrowsingSessionWithElectron.js.map