Automating Electron apps with Selenium WebDriver lets you run end-to-end tests, scrape data, or perform repetitive UI tasks on desktop applications built with Electron. This guide walks you through the setup and a practical script, assuming you have basic knowledge of JavaScript and Node.js.
What You’ll Need
- Node.js (version 14 or later) installed on your system.
- An Electron app to automate (either your own or one you have permission to test).
- A code editor and terminal.
- Basic understanding of JavaScript promises and async/await.
Step-by-Step
- Install the required packages. In your project directory, run:
npm install selenium-webdriver. This installs the Selenium WebDriver bindings for JavaScript. - Download the correct ChromeDriver. Electron uses Chromium, so you need a ChromeDriver version that matches the Electron version. Find your Electron version by running
npx electron --versionin the app’s directory. Then download the corresponding ChromeDriver from the official Chrome for Testing site (chromedriver.chromium.org) and place it in a known location, e.g.,./drivers/. - Set up the WebDriver builder. In your test script, require the Selenium module and create a driver instance. Use the
withCapabilitiesmethod to pass Electron-specific options. The key is to set the binary path to your Electron executable and add the app’s main script as an argument. - Write a basic automation script. Here’s a minimal example that launches an Electron app and gets its title:
const { Builder } = require('selenium-webdriver'); const chrome = require('selenium-webdriver/chrome'); const path = require('path'); async function example() { const driver = await new Builder() .forBrowser('chrome') .setChromeOptions(new chrome.Options() .setBinaryPath('/path/to/electron') // e.g., ./node_modules/.bin/electron .addArguments('app=/path/to/your/app')) .build(); await driver.get('http://localhost:3000'); // or your app's URL const title = await driver.getTitle(); console.log('Title:', title); await driver.quit(); } example(); - Interact with elements. Use the standard Selenium selectors to find elements. For example, to click a button with id “submit”:
await driver.findElement(By.id('submit')).click();. To type into an input:await driver.findElement(By.name('q')).sendKeys('Hello');. - Handle waiting and async operations. Use
driver.wait()with conditions likeuntil.elementLocated()to ensure elements are present before interacting. This is crucial because Electron apps may have delayed rendering. - Run your script. Execute the script with Node:
node test.js. You should see the Electron window open and perform the automated steps.
Common Mistakes
- Wrong ChromeDriver version. Using a ChromeDriver that doesn’t match the Electron’s Chromium version results in a session creation error. Always check the Electron version and download the matching driver.
- Incorrect binary path. The
setBinaryPathmust point to the Electron executable, not the app’s main JavaScript file. On Windows, this is usuallyelectron.exe; on macOS, it’s inside the .app bundle. - Forgetting to pass the app path. If you don’t add the
app=argument, Electron will open the default app or show an error. Make sure the path is absolute or correctly relative. - Not using async/await. Selenium WebDriver calls are asynchronous. Forgetting
awaitleads to undefined behavior and flaky tests.
Tips
- Use a dedicated test account or environment to avoid altering production data.
- Set a reasonable implicit wait time (e.g.,
driver.manage().setTimeouts({ implicit: 5000 })) to handle slow loading. - For debugging, add
driver.sleep(5000)before quitting to visually inspect the state. - Consider using a test runner like Mocha or Jest to organize multiple test cases.
- Keep your automation scripts in a separate directory from the app code to avoid accidental inclusion in the app bundle.
Troubleshooting
- Session not created error. This usually indicates a ChromeDriver/Electron version mismatch. Double-check both versions and re-download the correct driver.
- App doesn’t launch. Verify the binary path and the app argument. Try running the command manually in the terminal to see any error messages.
- Cannot find elements. Ensure the app is fully loaded by using explicit waits. Also, inspect the app’s DOM using the Chrome DevTools (you can enable remote debugging with the
--remote-debugging-port=9222argument). - Script hangs or times out. Increase the timeouts in the driver configuration. Also, check if there are any modal dialogs or popups that block interaction.
Frequently Asked Questions
Can I use Selenium with Electron’s main process?
No, Selenium interacts with the renderer (UI) process, not the main Node.js process. For main process testing, consider using Electron’s built-in testing utilities.
Do I need to modify my Electron app to make it testable?
Not necessarily. However, if your app uses remote URLs, ensure they are accessible. For local file paths, you may need to use file:// URLs.
Is there a way to run tests without a visible window?
Yes, you can use the --headless flag in Chrome options, but note that not all Electron features may work in headless mode.
How do I handle authentication or login in my Electron app?
You can automate the login form like any other web element. Alternatively, you can set cookies or use environment variables to bypass login for testing.
Can I use Selenium Grid for Electron apps?
Yes, you can connect to a Selenium Grid, but the grid node must have the same ChromeDriver and Electron setup as described above.
What if my Electron app uses multiple windows?
Selenium can handle multiple windows using driver.getAllWindowHandles() and driver.switchTo().window(handle). Each window is treated as a separate tab.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Enable Privacy Indicators on Chromebook
- How to Calculate Taxes on Overtime Pay
- VPN Not Working on iPhone? Fixes That Actually Work
- Hide & Unhide Apps on Samsung Phones
- Fix DNS Server Unavailable Error in Windows 11
- Fix iOS 17 Keyboard Not Working Properly