Using Playwright with Electron-Based Applications
Electron has become a popular framework for building cross-platform desktop applications using web technologies like JavaScript, HTML, and CSS. Applications such as Visual Studio Code, Slack, and Discord are built on Electron. As these apps grow in complexity, the need for automated end-to-end testing becomes essential. Playwright, a powerful end-to-end testing framework developed by Microsoft, is a great fit — even for Electron apps.
In this blog, we’ll explore how to use Playwright to automate testing for Electron-based applications, including setup, code examples, and best practices.
Why Use Playwright for Electron?
While Electron apps run on desktops, they’re essentially Chromium-based browsers running inside a native shell. Playwright is built to automate Chromium, Firefox, and WebKit — making it ideal for interacting with the web contents of Electron apps.
Key benefits of using Playwright for Electron:
Full browser control including Chromium (used by Electron)
Built-in support for context isolation and multiple windows
Powerful selectors (text, CSS, XPath, role-based)
Support for headless and headed modes
Cross-platform testing on Windows, macOS, and Linux
Prerequisites
To get started, you’ll need:
Node.js (v16+ recommended)
An Electron application (demo or production)
Playwright installed
Install Playwright using npm:
bash
npm install -D playwright
Install Electron if not already part of your project:
bash
Copy
Edit
npm install electron
Step 1: Launching Electron with Playwright
Unlike testing web pages, Electron testing involves launching the app through Electron’s main process.
Create a script to launch Electron manually and hook it with Playwright:
javascript
const { _electron: electron } = require('playwright');
const path = require('path');
(async () => {
const electronApp = await electron.launch({
args: [path.join(__dirname, 'path-to-your-electron-main.js')],
});
// Get the first BrowserWindow
const window = await electronApp.firstWindow();
// Perform actions
await window.click('text=Login');
await window.fill('#username', 'admin');
await window.fill('#password', 'admin123');
await window.click('button[type="submit"]');
// Take a screenshot
await window.screenshot({ path: 'login-screen.png' });
await electronApp.close();
})();
The _electron API in Playwright provides methods to control the Electron app from both the main and renderer processes.
Step 2: Interacting with the App
Once you have access to the window, you can use all standard Playwright commands:
click(selector)
fill(selector, value)
waitForSelector(selector)
evaluate(pageFunction)
screenshot(options)
For multi-window apps, use:
javascript
const allWindows = electronApp.windows();
To listen for events like opening new windows:
javascript
electronApp.on('window', window => {
console.log('New window opened');
});
Step 3: Writing Tests with Test Runner
You can integrate this with the Playwright Test Runner:
bash
Copy
Edit
npx playwright test
Create a test file:
javascript
Copy
Edit
import { test, expect, _electron as electron } from '@playwright/test';
test('should login successfully in Electron app', async () => {
const electronApp = await electron.launch({ args: ['main.js'] });
const window = await electronApp.firstWindow();
await window.fill('#username', 'admin');
await window.fill('#password', 'admin123');
await window.click('button[type="submit"]');
await expect(window.locator('.dashboard')).toBeVisible();
await electronApp.close();
});
Best Practices
Use data-test selectors for robust element targeting.
Avoid hard-coded waits; prefer waitForSelector or expect.
Test in both development and packaged Electron modes.
Capture screenshots and videos for debugging failures.
Final Thoughts
Playwright offers a powerful way to automate testing for Electron apps with minimal setup and deep integration into Chromium. It enables you to write fast, reliable tests that mimic real user interactions inside desktop applications. Whether you're testing a simple login flow or a complex multi-window app, Playwright can help you ensure quality and stability across every feature of your Electron-based software.
Learn Playwright Testing Training
Read More : Creating Test Templates for Rapid Prototyping in Playwright
Read More: Continuous Monitoring Using Playwright Scripts
Read More: Automating Bulk Form Submissions in Playwright
Visit IHUB Talent Institute Hyderabad
Comments
Post a Comment