docs/src/electron-api/class-electron.md
Playwright has experimental support for Electron automation. You can access electron namespace via:
const { _electron } = require('playwright');
An example of the Electron automation script would be:
const { _electron: electron } = require('playwright');
(async () => {
// Launch Electron app.
const electronApp = await electron.launch({ args: ['main.js'] });
// Evaluation expression in the Electron context.
const appPath = await electronApp.evaluate(async ({ app }) => {
// This runs in the main Electron process, parameter here is always
// the result of the require('electron') in the main app script.
return app.getAppPath();
});
console.log(appPath);
// Get the first window that the app opens, wait if necessary.
const window = await electronApp.firstWindow();
// Print the title.
console.log(await window.title());
// Capture a screenshot.
await window.screenshot({ path: 'intro.png' });
// Direct Electron console to Node terminal.
window.on('console', console.log);
// Click button.
await window.click('text=Click me');
// Exit app.
await electronApp.close();
})();
Supported Electron versions are:
Known issues:
If you are not able to launch Electron and it will end up in timeouts during launch, try the following:
nodeCliInspect (FuseV1Options.EnableNodeCliInspectArguments) fuse is not set to false.Mocking native dialogs:
Playwright does not intercept the native Electron dialog API
(dialog.showOpenDialog, dialog.showSaveDialog, dialog.showMessageBox, etc.) because those calls happen in the
Electron main process and go straight to OS APIs. Use [method: ElectronApplication.evaluate] to replace the
relevant methods in the main process so tests run deterministically without any OS-level UI:
// Stub the open dialog to always return a fixed path.
await electronApp.evaluate(({ dialog }, filePaths) => {
dialog.showOpenDialog = () => Promise.resolve({ canceled: false, filePaths });
}, ['/path/to/file.txt']);
// Stub the save dialog.
await electronApp.evaluate(({ dialog }, filePath) => {
dialog.showSaveDialog = () => Promise.resolve({ canceled: false, filePath });
}, '/path/to/saved.txt');
// Stub showMessageBox to click the first button.
await electronApp.evaluate(({ dialog }) => {
dialog.showMessageBox = () => Promise.resolve({ response: 0, checkboxChecked: false });
});
The replacement persists until the application is closed. Synchronous variants
(showOpenDialogSync, showSaveDialogSync, showMessageBoxSync) can be stubbed the same way — just return the
value directly instead of a Promise.
Launches electron application specified with the [option: executablePath].
executablePath <[string]>Launches given Electron application. If not specified, launches the default Electron
executable installed in this package, located at node_modules/.bin/electron.
args <[Array]<[string]>>Additional arguments to pass to the application when launching. You typically pass the main script name here.
cwd <[string]>Current working directory to launch application from.
env <[Object]<[string], [string]>>Specifies environment variables that will be visible to Electron. Defaults to process.env.
timeout <[float]>Maximum time in milliseconds to wait for the application to start. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.