Electron Automation
Relevant source files
The following files were used as context for generating this wiki page:
- packages/playwright-core/src/server/electron/electron.ts
- packages/playwright-core/src/client/electron.ts
- tests/electron/electron-app.spec.ts
- tests/electron/electronTest.ts
- tests/installation/playwright-electron-should-work.spec.ts
- tests/electron/playwright.config.ts
- packages/playwright/types/test.d.ts
Playwright provides robust capabilities for automating and testing Electron applications. This is achieved by launching the Electron application process and establishing communication channels to control both the main process (Node.js environment) and the renderer processes (Chromium environment). This allows for comprehensive end-to-end testing of Electron applications, covering everything from main process logic to user interface interactions within the application windows.
The automation is managed through the _electron object available in @playwright/test, which serves as the entry point for launching and interacting with an Electron application. The core of this feature lies in a dual-connection architecture, where Playwright communicates with the Electron main process via the Node.js inspector protocol and with the renderer windows via the Chrome DevTools Protocol.
Architecture
The Electron automation architecture in Playwright is built upon a client-server model that manages two distinct communication protocols to gain full control over an Electron application.
Core Components
The main classes involved in Electron automation are:
Electron(Server-side): Responsible for launching the Electron application process. It sets up the necessary environment and command-line arguments to enable debugging and automation.ElectronApplication(Server-side): Represents a running instance of an Electron application. It encapsulates the connections to both the main and renderer processes and provides an API to interact with them.CRBrowser/CRBrowserContext(Server-side): Chromium-specific browser and context classes are used to manage the renderer processes (windows) of the Electron application, treating them like web pages.CRConnection(Server-side): A connection class used for both the Node.js main process and the Chromium renderer processes.
Architectural Flow
The following diagram illustrates the high-level architecture of Playwright's Electron automation.
This diagram shows how a test script initiates Electron automation, leading to the Playwright server launching and connecting to the Electron application's main and renderer processes.
Sources: packages/playwright-core/src/server/electron/electron.ts
Class Relationships
The key classes interact to provide a unified automation experience.
This diagram shows the relationship between the main classes involved in Electron automation. Playwright holds an Electron instance, which launches an ElectronApplication that manages connections and contexts.
Sources: packages/playwright-core/src/server/electron/electron.ts:182, packages/playwright-core/src/server/electron/electron.ts:62
Launch Process
Launching an Electron application is an orchestrated process that involves spawning the application with specific debug flags and establishing two separate WebSocket connections for automation.
Launch Sequence
The sequence diagram below details the steps involved when playwright._electron.launch() is called.
This diagram illustrates the sequence of events from calling _electron.launch in a test to receiving the ElectronApplication object, ready for automation.
Sources: packages/playwright-core/src/server/electron/electron.ts:187-288
Launch Arguments and Environment
When launching, Playwright injects specific command-line arguments to enable automation:
--inspect=0: Starts the Node.js inspector on a random port, allowing automation of the main process.--remote-debugging-port=0: Starts the Chrome DevTools Protocol server on a random port, allowing automation of renderer windows.-r require.resolve('./loader'): Injects a Playwright loader script into the application for non-packaged apps.
Sources: packages/playwright-core/src/server/electron/electron.ts:189-217
Launch Options
Several options can be passed to _electron.launch() to configure the application environment. These are often used in test fixtures.
| Option | Type | Description | Source File |
|---|---|---|---|
args | string[] | Additional command-line arguments to pass to the Electron application. | tests/electron/electron-app.spec.ts:150 |
env | object | Specifies environment variables for the new process. | tests/electron/electronTest.ts:56 |
bypassCSP | boolean | Toggles bypassing of the page's Content-Security-Policy. | tests/electron/electron-app.spec.ts:213 |
recordVideo | object | Records a video of the application's windows. | tests/electron/electron-app.spec.ts:261 |
recordHar | object | Records a HAR file of the application's network activity. | tests/electron/electron-app.spec.ts:271 |
acceptDownloads | boolean | Whether to automatically download all attachments. | tests/electron/electron-app.spec.ts:303 |
Interacting with the Application
The ElectronApplication object is the primary interface for controlling the running application.
Key Methods and Properties
| Method/Property | Description | Source File |
|---|---|---|
context() | Returns the browser context that contains all windows. | packages/playwright-core/src/server/electron/electron.ts:121 |
firstWindow() | Waits for the first application window to be opened and returns a Page object for it. | tests/electron/electron-app.spec.ts:183 |
windows() | Returns an array of all currently opened Page objects. | tests/electron/electron-app.spec.ts:155 |
evaluate(pageFunction, arg) | Executes a function in the main process of the Electron application. | tests/electron/electron-app.spec.ts:144 |
browserWindow(page) | Returns a JSHandle for the BrowserWindow instance corresponding to the given Page. | packages/playwright-core/src/server/electron/electron.ts:125 |
close() | Closes the application by calling app.quit(). | packages/playwright-core/src/server/electron/electron.ts:129 |
process() | Returns the child_process instance of the application. | packages/playwright-core/src/server/electron/electron.ts:117 |
Accessing Electron APIs
You can access Electron's main process APIs, such as app or clipboard, by using electronApp.evaluate(). The electron module is automatically available within the evaluation context.
// tests/electron/electron-app.spec.ts:144-147
test('should script application', async ({ electronApp }) => {
const appPath = await electronApp.evaluate(async ({ app }) => app.getAppPath());
expect(appPath).toBe(path.resolve(__dirname));
});
// tests/electron/electron-app.spec.ts:193-197
test('should have a clipboard instance', async ({ electronApp }) => {
const clipboardContentToWrite = 'Hello from Playwright';
await electronApp.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), clipboardContentToWrite);
const clipboardContentRead = await electronApp.evaluate(async ({ clipboard }) => clipboard.readText());
expect(clipboardContentRead).toEqual(clipboardContentToWrite);
});
Sources: tests/electron/electron-app.spec.ts:144-147, 193-197
Event Handling
The ElectronApplication object emits events that allow tests to react to application-level changes.
close: Emitted when the application closes. This can be triggered byelectronApp.close(),context.close(), or the application quitting itself.
Sources: tests/electron/electron-app.spec.ts:25-78console: Emitted when the main process logs a message to the console. The event provides aConsoleMessageobject.
Sources: packages/playwright-core/src/server/electron/electron.ts:106, tests/electron/electron-app.spec.ts:80-94
Testing with Playwright Test
Playwright Test provides dedicated fixtures for streamlining Electron testing.
Test Fixtures
The electronTest object extends the base test runner with Electron-specific fixtures.
| Fixture | Description |
|---|---|
launchElectronApp | A function to launch an Electron app with a specific script file, arguments, and options. It handles creating and cleaning up a temporary user data directory. |
electronApp | An ElectronApplication instance launched with a default test script (electron-app.js). |
newWindow | A function that creates a new BrowserWindow in the running electronApp and returns its Page object. |
page | A Page object for a newly created window, provided by the newWindow fixture. |
context | The BrowserContext of the electronApp. |
Sources: tests/electron/electronTest.ts:28-106
Example Test
The following example demonstrates a typical test case using the electronApp fixture to interact with an Electron application.
// tests/electron/electron-app.spec.ts:183-188
test('should wait for first window', async ({ electronApp }) => {
await electronApp.evaluate(({ BrowserWindow }) => {
const window = new BrowserWindow({ width: 800, height: 600 });
void window.loadURL('data:text/html,<title>Hello World!</title>');
});
const window = await electronApp.firstWindow();
expect(await window.title()).toBe('Hello World!');
});
Sources: tests/electron/electron-app.spec.ts:183-188
Configuration
An Electron test project can be defined in the playwright.config.ts file. This allows for specific configurations for Electron tests, separate from browser-based tests.
// tests/electron/playwright.config.ts:43-50
config.projects.push({
name: 'electron-api',
use: {
browserName: 'chromium',
headless: false,
},
testDir: path.join(testDir, 'electron'),
metadata,
});
Sources: tests/electron/playwright.config.ts:43-50
Summary
Playwright's Electron automation provides a powerful and comprehensive solution for testing Electron applications. By leveraging a dual-protocol architecture, it offers deep control over both the main and renderer processes. With dedicated APIs, test fixtures, and clear eventing, developers can write reliable and thorough end-to-end tests that cover all aspects of their Electron application's functionality. The integration with the Playwright Test runner ensures a seamless and efficient testing experience.