Automated Testing

Test automation is an efficient way of validating that your application code works as intended. While Electron doesn’t actively maintain its own testing solution, this guide will go over a couple ways you can run end-to-end automated tests on your Electron app.

Using the WebDriver interface

From ChromeDriver - WebDriver for Chrome:

WebDriver is an open source tool for automated testing of web apps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. ChromeDriver is a standalone server which implements WebDriver’s wire protocol for Chromium. It is being developed by members of the Chromium and WebDriver teams.

There are a few ways that you can set up testing using WebDriver.

With WebdriverIO

WebdriverIO (WDIO) is a test automation framework that provides a Node.js package for testing with WebDriver. Its ecosystem also includes various plugins (e.g. reporter and services) that can help you put together your test setup.

If you already have an existing WebdriverIO setup, it is recommended to update your dependencies and validate your existing configuration with how it is outlined in the docs.

Install the test runner

If you don’t use WebdriverIO in your project yet, you can add it by running the starter toolkit in your project root directory:

  • npm
  • Yarn
  1. npm init wdio@latest ./
  1. yarn create wdio@latest ./

This starts a configuration wizard that helps you put together the right setup, installs all necessary packages, and generates a wdio.conf.js configuration file. Make sure to select “Desktop Testing - of Electron Applications” on one of the first questions asking “What type of testing would you like to do?”.

Connect WDIO to your Electron app

After running the configuration wizard, your wdio.conf.js should include roughly the following content:

wdio.conf.js

  1. export const config = {
  2. // ...
  3. services: ['electron'],
  4. capabilities: [{
  5. browserName: 'electron',
  6. 'wdio:electronServiceOptions': {
  7. // WebdriverIO can automatically find your bundled application
  8. // if you use Electron Forge or electron-builder, otherwise you
  9. // can define it here, e.g.:
  10. // appBinaryPath: './path/to/bundled/application.exe',
  11. appArgs: ['foo', 'bar=baz']
  12. }
  13. }]
  14. // ...
  15. }

Write your tests

Use the WebdriverIO API to interact with elements on the screen. The framework provides custom “matchers” that make asserting the state of your application easy, e.g.:

  1. import { browser, $, expect } from '@wdio/globals'
  2. describe('keyboard input', () => {
  3. it('should detect keyboard input', async () => {
  4. await browser.keys(['y', 'o'])
  5. await expect($('keypress-count')).toHaveText('YO')
  6. })
  7. })

Furthermore, WebdriverIO allows you to access Electron APIs to get static information about your application:

  1. import { browser, $, expect } from '@wdio/globals'
  2. describe('when the make smaller button is clicked', () => {
  3. it('should decrease the window height and width by 10 pixels', async () => {
  4. const boundsBefore = await browser.electron.browserWindow('getBounds')
  5. expect(boundsBefore.width).toEqual(210)
  6. expect(boundsBefore.height).toEqual(310)
  7. await $('.make-smaller').click()
  8. const boundsAfter = await browser.electron.browserWindow('getBounds')
  9. expect(boundsAfter.width).toEqual(200)
  10. expect(boundsAfter.height).toEqual(300)
  11. })
  12. })

or to retrieve other Electron process information:

  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. import { browser, expect } from '@wdio/globals'
  4. const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), { encoding: 'utf-8' }))
  5. const { name, version } = packageJson
  6. describe('electron APIs', () => {
  7. it('should retrieve app metadata through the electron API', async () => {
  8. const appName = await browser.electron.app('getName')
  9. expect(appName).toEqual(name)
  10. const appVersion = await browser.electron.app('getVersion')
  11. expect(appVersion).toEqual(version)
  12. })
  13. it('should pass args through to the launched application', async () => {
  14. // custom args are set in the wdio.conf.js file as they need to be set before WDIO starts
  15. const argv = await browser.electron.mainProcess('argv')
  16. expect(argv).toContain('--foo')
  17. expect(argv).toContain('--bar=baz')
  18. })
  19. })

Run your tests

To run your tests:

  1. $ npx wdio run wdio.conf.js

WebdriverIO helps launch and shut down the application for you.

More documentation

Find more documentation on Mocking Electron APIs and other useful resources in the official WebdriverIO documentation.

With Selenium

Selenium is a web automation framework that exposes bindings to WebDriver APIs in many languages. Their Node.js bindings are available under the selenium-webdriver package on NPM.

Run a ChromeDriver server

In order to use Selenium with Electron, you need to download the electron-chromedriver binary, and run it:

  • npm
  • Yarn
  1. npm install --save-dev electron-chromedriver
  2. ./node_modules/.bin/chromedriver
  3. Starting ChromeDriver (v2.10.291558) on port 9515
  4. Only local connections are allowed.
  1. yarn add --dev electron-chromedriver
  2. ./node_modules/.bin/chromedriver
  3. Starting ChromeDriver (v2.10.291558) on port 9515
  4. Only local connections are allowed.

Remember the port number 9515, which will be used later.

Connect Selenium to ChromeDriver

Next, install Selenium into your project:

  • npm
  • Yarn
  1. npm install --save-dev selenium-webdriver
  1. yarn add --dev selenium-webdriver

Usage of selenium-webdriver with Electron is the same as with normal websites, except that you have to manually specify how to connect ChromeDriver and where to find the binary of your Electron app:

test.js

  1. const webdriver = require('selenium-webdriver')
  2. const driver = new webdriver.Builder()
  3. // The "9515" is the port opened by ChromeDriver.
  4. .usingServer('http://localhost:9515')
  5. .withCapabilities({
  6. 'goog:chromeOptions': {
  7. // Here is the path to your Electron binary.
  8. binary: '/Path-to-Your-App.app/Contents/MacOS/Electron'
  9. }
  10. })
  11. .forBrowser('chrome') // note: use .forBrowser('electron') for selenium-webdriver <= 3.6.0
  12. .build()
  13. driver.get('https://www.google.com')
  14. driver.findElement(webdriver.By.name('q')).sendKeys('webdriver')
  15. driver.findElement(webdriver.By.name('btnG')).click()
  16. driver.wait(() => {
  17. return driver.getTitle().then((title) => {
  18. return title === 'webdriver - Google Search'
  19. })
  20. }, 1000)
  21. driver.quit()

Using Playwright

Microsoft Playwright is an end-to-end testing framework built using browser-specific remote debugging protocols, similar to the Puppeteer headless Node.js API but geared towards end-to-end testing. Playwright has experimental Electron support via Electron’s support for the Chrome DevTools Protocol (CDP).

Install dependencies

You can install Playwright through your preferred Node.js package manager. The Playwright team recommends using the PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD environment variable to avoid unnecessary browser downloads when testing an Electron app.

  • npm
  • Yarn
  1. PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm install --save-dev playwright
  1. PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 yarn add --dev playwright

Playwright also comes with its own test runner, Playwright Test, which is built for end-to-end testing. You can also install it as a dev dependency in your project:

  • npm
  • Yarn
  1. npm install --save-dev @playwright/test
  1. yarn add --dev @playwright/test

Automated Testing - 图1Dependencies

This tutorial was written playwright@1.16.3 and @playwright/test@1.16.3. Check out Playwright’s releases page to learn about changes that might affect the code below.

Automated Testing - 图2Using third-party test runners

If you’re interested in using an alternative test runner (e.g. Jest or Mocha), check out Playwright’s Third-Party Test Runner guide.

Write your tests

Playwright launches your app in development mode through the _electron.launch API. To point this API to your Electron app, you can pass the path to your main process entry point (here, it is main.js).

  1. const { _electron: electron } = require('playwright')
  2. const { test } = require('@playwright/test')
  3. test('launch app', async () => {
  4. const electronApp = await electron.launch({ args: ['main.js'] })
  5. // close app
  6. await electronApp.close()
  7. })

After that, you will access to an instance of Playwright’s ElectronApp class. This is a powerful class that has access to main process modules for example:

  1. const { _electron: electron } = require('playwright')
  2. const { test } = require('@playwright/test')
  3. test('get isPackaged', async () => {
  4. const electronApp = await electron.launch({ args: ['main.js'] })
  5. const isPackaged = await electronApp.evaluate(async ({ app }) => {
  6. // This runs in Electron's main process, parameter here is always
  7. // the result of the require('electron') in the main app script.
  8. return app.isPackaged
  9. })
  10. console.log(isPackaged) // false (because we're in development mode)
  11. // close app
  12. await electronApp.close()
  13. })

It can also create individual Page objects from Electron BrowserWindow instances. For example, to grab the first BrowserWindow and save a screenshot:

  1. const { _electron: electron } = require('playwright')
  2. const { test } = require('@playwright/test')
  3. test('save screenshot', async () => {
  4. const electronApp = await electron.launch({ args: ['main.js'] })
  5. const window = await electronApp.firstWindow()
  6. await window.screenshot({ path: 'intro.png' })
  7. // close app
  8. await electronApp.close()
  9. })

Putting all this together using the PlayWright Test runner, let’s create a example.spec.js test file with a single test and assertion:

example.spec.js

  1. const { _electron: electron } = require('playwright')
  2. const { test, expect } = require('@playwright/test')
  3. test('example test', async () => {
  4. const electronApp = await electron.launch({ args: ['.'] })
  5. const isPackaged = await electronApp.evaluate(async ({ app }) => {
  6. // This runs in Electron's main process, parameter here is always
  7. // the result of the require('electron') in the main app script.
  8. return app.isPackaged
  9. })
  10. expect(isPackaged).toBe(false)
  11. // Wait for the first BrowserWindow to open
  12. // and return its Page object
  13. const window = await electronApp.firstWindow()
  14. await window.screenshot({ path: 'intro.png' })
  15. // close app
  16. await electronApp.close()
  17. })

Then, run Playwright Test using npx playwright test. You should see the test pass in your console, and have an intro.png screenshot on your filesystem.

  1. $ npx playwright test
  2. Running 1 test using 1 worker
  3. example.spec.js:4:1 example test (1s)

Automated Testing - 图3info

Playwright Test will automatically run any files matching the .*(test|spec)\.(js|ts|mjs) regex. You can customize this match in the Playwright Test configuration options.

Automated Testing - 图4Further reading

Check out Playwright’s documentation for the full Electron and ElectronApplication class APIs.

Using a custom test driver

It’s also possible to write your own custom driver using Node.js’ built-in IPC-over-STDIO. Custom test drivers require you to write additional app code, but have lower overhead and let you expose custom methods to your test suite.

To create a custom driver, we’ll use Node.js’ child_process API. The test suite will spawn the Electron process, then establish a simple messaging protocol:

testDriver.js

  1. const childProcess = require('node:child_process')
  2. const electronPath = require('electron')
  3. // spawn the process
  4. const env = { /* ... */ }
  5. const stdio = ['inherit', 'inherit', 'inherit', 'ipc']
  6. const appProcess = childProcess.spawn(electronPath, ['./app'], { stdio, env })
  7. // listen for IPC messages from the app
  8. appProcess.on('message', (msg) => {
  9. // ...
  10. })
  11. // send an IPC message to the app
  12. appProcess.send({ my: 'message' })

From within the Electron app, you can listen for messages and send replies using the Node.js process API:

main.js

  1. // listen for messages from the test suite
  2. process.on('message', (msg) => {
  3. // ...
  4. })
  5. // send a message to the test suite
  6. process.send({ my: 'message' })

We can now communicate from the test suite to the Electron app using the appProcess object.

For convenience, you may want to wrap appProcess in a driver object that provides more high-level functions. Here is an example of how you can do this. Let’s start by creating a TestDriver class:

testDriver.js

  1. class TestDriver {
  2. constructor ({ path, args, env }) {
  3. this.rpcCalls = []
  4. // start child process
  5. env.APP_TEST_DRIVER = 1 // let the app know it should listen for messages
  6. this.process = childProcess.spawn(path, args, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], env })
  7. // handle rpc responses
  8. this.process.on('message', (message) => {
  9. // pop the handler
  10. const rpcCall = this.rpcCalls[message.msgId]
  11. if (!rpcCall) return
  12. this.rpcCalls[message.msgId] = null
  13. // reject/resolve
  14. if (message.reject) rpcCall.reject(message.reject)
  15. else rpcCall.resolve(message.resolve)
  16. })
  17. // wait for ready
  18. this.isReady = this.rpc('isReady').catch((err) => {
  19. console.error('Application failed to start', err)
  20. this.stop()
  21. process.exit(1)
  22. })
  23. }
  24. // simple RPC call
  25. // to use: driver.rpc('method', 1, 2, 3).then(...)
  26. async rpc (cmd, ...args) {
  27. // send rpc request
  28. const msgId = this.rpcCalls.length
  29. this.process.send({ msgId, cmd, args })
  30. return new Promise((resolve, reject) => this.rpcCalls.push({ resolve, reject }))
  31. }
  32. stop () {
  33. this.process.kill()
  34. }
  35. }
  36. module.exports = { TestDriver }

In your app code, can then write a simple handler to receive RPC calls:

main.js

  1. const METHODS = {
  2. isReady () {
  3. // do any setup needed
  4. return true
  5. }
  6. // define your RPC-able methods here
  7. }
  8. const onMessage = async ({ msgId, cmd, args }) => {
  9. let method = METHODS[cmd]
  10. if (!method) method = () => new Error('Invalid method: ' + cmd)
  11. try {
  12. const resolve = await method(...args)
  13. process.send({ msgId, resolve })
  14. } catch (err) {
  15. const reject = {
  16. message: err.message,
  17. stack: err.stack,
  18. name: err.name
  19. }
  20. process.send({ msgId, reject })
  21. }
  22. }
  23. if (process.env.APP_TEST_DRIVER) {
  24. process.on('message', onMessage)
  25. }

Then, in your test suite, you can use your TestDriver class with the test automation framework of your choosing. The following example uses ava, but other popular choices like Jest or Mocha would work as well:

test.js

  1. const test = require('ava')
  2. const electronPath = require('electron')
  3. const { TestDriver } = require('./testDriver')
  4. const app = new TestDriver({
  5. path: electronPath,
  6. args: ['./app'],
  7. env: {
  8. NODE_ENV: 'test'
  9. }
  10. })
  11. test.before(async t => {
  12. await app.isReady
  13. })
  14. test.after.always('cleanup', async t => {
  15. await app.stop()
  16. })