Frontend Development 10 min read

Automating Electron Application Testing with Selenium on macOS

This guide explains how to set up Selenium to drive Chrome and Electron applications on macOS, covering ChromeDriver version matching, configuring Electron's binary location, building and packaging the app, and using remote debugging to test already‑running Electron instances.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Automating Electron Application Testing with Selenium on macOS

Background – An internal automation testing project required testing an IM communication client built with Electron on macOS. The article records the process of using Selenium to automate Electron applications.

Electron Overview – Electron combines Chromium and Node.js, allowing developers to create cross‑platform desktop apps with JavaScript, HTML, and CSS. The resulting binaries are large because they bundle Chromium.

Using Selenium with Chrome – Determine the Chrome version, download the matching ChromeDriver (or use a mirror), and add it to the PATH or specify its path in code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
# driver = webdriver.Chrome(executable_path="/path/to/chromedriver")

driver.get("https://www.google.com")
assert "Google" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("360")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

Using Selenium with Electron – To control an Electron app, first find the Chromium version used by the app (e.g., via Chrome DevTools or by reading the error message when the wrong ChromeDriver is used). Then set the binary_location option to point to the Electron executable.

Creating the Electron Project

mkdir electroltest
cd electroltest
npm init -y
npm install electron electron-packager electron-installer-dmg --save

Update package.json with scripts for start, build, and dmg generation.

{
  "name": "electroltest",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron .",
    "build": "electron-packager .",
    "dmg": "electron-installer-dmg ./electroltest-darwin-x64/electroltest.app/electroltest"
  },
  "dependencies": {
    "electron": "^15.1.2",
    "electron-installer-dmg": "^3.0.0",
    "electron-packager": "^15.4.0"
  }
}

Add main.js to create a BrowserWindow and load index.html :

const { app, BrowserWindow } = require('electron')
const path = require('path')
function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: { preload: path.join(__dirname, 'preload.js') }
  })
  win.loadFile('index.html')
}
app.whenReady().then(() => {
  createWindow()
  app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow() })
})
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })

Provide index.html and preload.js as shown in the source, then run npm run start to verify the app works.

Packaging the Electron App – Use the defined scripts:

npm run build   # runs electron-packager
npm run dmg     # creates a macOS .dmg installer

After successful packaging, the .dmg can be installed on macOS.

Testing the Packaged App with Selenium – Install Selenium for Python ( pip install selenium ) and write a test that sets options.binary_location to the packaged app path:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

options = webdriver.ChromeOptions()
options.binary_location = "/Applications/electroltest.app/Contents/MacOS/electroltest"

driver = webdriver.Chrome(chrome_options=options)
time.sleep(3)
print(driver.page_source)

driver.get("http://www.google.com")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("360")
elem.send_keys(Keys.RETURN)

driver.close()
driver.quit()

Testing an Already‑Running Electron App – Launch the app with remote debugging enabled:

/Applications/electroltest.app/Contents/MacOS/electroltest --remote-debugging-port=9222

Then configure Selenium to connect to that debugger address:

options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(chrome_options=options)

This allows testing of an app that is already open, which is useful for complex login flows.

Summary

Find the matching ChromeDriver for the Electron‑bundled Chromium version.

Set binary_location to the Electron executable when creating the Selenium driver.

When testing a running app, start Electron with --remote-debugging-port and connect Selenium via debuggerAddress .

AutomationtestingElectronmacOSSeleniumChromeDriver
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.