Implementation:Microsoft Playwright Client Electron
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, Desktop Application Testing |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for automating Electron desktop applications provided by the Playwright library.
Description
This module exports two classes: Electron and ElectronApplication.
Electron extends ChannelOwner and serves as the launcher for Electron applications. Its launch() method accepts ElectronOptions (including environment variables, HTTP headers, HAR recording, color scheme, and timeout), prepares browser context parameters, and returns an ElectronApplication instance. It integrates with the Selectors system for custom selector support.
ElectronApplication extends ChannelOwner and represents a running Electron app. It manages the set of open windows (pages), provides firstWindow() to wait for the initial window, gives access to the underlying BrowserContext, and supports evaluating code in the Electron main process via evaluate() and evaluateHandle(). It emits events for close, console messages, and new windows. The browserWindow() method retrieves the Electron BrowserWindow handle for a given page. It also supports waitForEvent() with timeout and predicate support, and implements Symbol.asyncDispose.
Usage
Use the Electron class to launch and automate Electron desktop applications in tests. Access it via playwright._electron. After launching, interact with the application through the ElectronApplication instance's windows, context, and evaluation methods.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File:
packages/playwright-core/src/client/electron.ts
Signature
export class Electron extends ChannelOwner<channels.ElectronChannel> implements api.Electron {
_playwright!: Playwright;
static from(electron: channels.ElectronChannel): Electron;
async launch(options?: ElectronOptions): Promise<ElectronApplication>;
}
export class ElectronApplication extends ChannelOwner<channels.ElectronApplicationChannel> implements api.ElectronApplication {
readonly _context: BrowserContext;
static from(electronApplication: channels.ElectronApplicationChannel): ElectronApplication;
process(): childProcess.ChildProcess;
windows(): Page[];
async firstWindow(options?: { timeout?: number }): Promise<Page>;
context(): BrowserContext;
async close(): Promise<void>;
async waitForEvent(event: string, optionsOrPredicate?: WaitForEventOptions): Promise<any>;
async browserWindow(page: Page): Promise<JSHandle<BrowserWindow>>;
async evaluate<R, Arg>(pageFunction: PageFunctionOn<ElectronAppType, Arg, R>, arg: Arg): Promise<R>;
async evaluateHandle<R, Arg>(pageFunction: PageFunctionOn<ElectronAppType, Arg, R>, arg: Arg): Promise<SmartHandle<R>>;
}
Import
import { Electron, ElectronApplication } from 'playwright-core/src/client/electron';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| options (launch) | ElectronOptions |
No | Launch options including env, extraHTTPHeaders, recordHar, colorScheme, acceptDownloads, timeout, tracesDir |
| page (browserWindow) | Page |
Yes | The page to get the BrowserWindow handle for |
| pageFunction (evaluate) | string | Yes | The function or expression to evaluate in the main process |
| arg (evaluate) | any |
Yes | The argument to pass to the evaluation function |
Outputs
| Name | Type | Description |
|---|---|---|
| launch() | Promise<ElectronApplication> |
A running Electron application instance |
| windows() | Page[] |
Array of currently open Electron windows as Page objects |
| firstWindow() | Promise<Page> |
The first (or next) window opened by the application |
| context() | BrowserContext |
The underlying browser context |
| browserWindow() | Promise<JSHandle<BrowserWindow>> |
Handle to the Electron BrowserWindow for a page |
| evaluate() | Promise<R> |
Result of evaluating a function in the Electron main process |
Usage Examples
const electronApp = await playwright._electron.launch({
args: ['./main.js'],
env: { NODE_ENV: 'test' },
});
// Wait for the first window
const window = await electronApp.firstWindow();
console.log('Window title:', await window.title());
// Access browser window handle
const bw = await electronApp.browserWindow(window);
const bounds = await bw.evaluate((win) => win.getBounds());
console.log('Window bounds:', bounds);
// Evaluate in the main process
const appName = await electronApp.evaluate(async ({ app }) => app.getName());
console.log('App name:', appName);
// Close the application
await electronApp.close();