自动化测试
测试自动化是一种验证您的应用是否如你预期那样正常工作的有效方法。 Electron已然不再维护自己的测试解决方案, 本指南将会介绍几种方法,让您可以在 Electron 应用上进行端到端自动测试。
使用 WebDriver 接口
引自 ChromeDriver - WebDriver for Chrome:
WebDriver 是一款开源的支持多浏览器的自动化测试工具。 它提供了操作网页、用户输入、JavaScript 执行等能力。 ChromeDriver 是一个实现了 WebDriver 与 Chromium 联接协议的独立服务。 它也是由开发了 Chromium 和 WebDriver 的团队开发的。
有几种方法可以使用 WebDriver 设置测试。
使用 WebdriverIO
WebdriverIO (WDIO) 是一个自动化测试框架,它提供了一个 Node.js 软件包用于测试 Web 驱动程序。 它的生态系统还包括各种插件(例如报告器和服务) ,可以帮助你把测试设置放在一起。
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
npm init wdio@latest ./
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?".
将 WDIO 连接到 Electron 应用程序
After running the configuration wizard, your wdio.conf.js
should include roughly the following content:
export const config = {
// ...
services: ['electron'],
capabilities: [{
browserName: 'electron',
'wdio:electronServiceOptions': {
// WebdriverIO can automatically find your bundled application
// if you use Electron Forge or electron-builder, otherwise you
// can define it here, e.g.:
// appBinaryPath: './path/to/bundled/application.exe',
appArgs: ['foo', 'bar=baz']
}
}]
// ...
}
编写测试
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.:
import { browser, $, expect } from '@wdio/globals'
describe('keyboard input', () => {
it('should detect keyboard input', async () => {
await browser.keys(['y', 'o'])
await expect($('keypress-count')).toHaveText('YO')
})
})
Furthermore, WebdriverIO allows you to access Electron APIs to get static information about your application:
import { browser, $, expect } from '@wdio/globals'
describe('when the make smaller button is clicked', () => {
it('should decrease the window height and width by 10 pixels', async () => {
const boundsBefore = await browser.electron.browserWindow('getBounds')
expect(boundsBefore.width).toEqual(210)
expect(boundsBefore.height).toEqual(310)
await $('.make-smaller').click()
const boundsAfter = await browser.electron.browserWindow('getBounds')
expect(boundsAfter.width).toEqual(200)
expect(boundsAfter.height).toEqual(300)
})
})
or to retrieve other Electron process information:
import fs from 'node:fs'
import path from 'node:path'
import { browser, expect } from '@wdio/globals'
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), { encoding: 'utf-8' }))
const { name, version } = packageJson
describe('electron APIs', () => {
it('should retrieve app metadata through the electron API', async () => {
const appName = await browser.electron.app('getName')
expect(appName).toEqual(name)
const appVersion = await browser.electron.app('getVersion')
expect(appVersion).toEqual(version)
})
it('should pass args through to the launched application', async () => {
// custom args are set in the wdio.conf.js file as they need to be set before WDIO starts
const argv = await browser.electron.mainProcess('argv')
expect(argv).toContain('--foo')
expect(argv).toContain('--bar=baz')
})
})
运行测试
执行命令:
$ 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.
使用 Selenium
Selenium 是一个Web自动化框架,以多种语言公开与 WebDriver API 的绑定方式。 Node.js 环境下, 可以通过 NPM 安装 selenium-webdriver
包来使用此框架。
运行 ChromeDriver 服务
为了与 Electron 一起使用 Selenium ,你需要下载 electron-chromedriver
二进制文件并运行它:
- npm
- Yarn
npm install --save-dev electron-chromedriver
./node_modules/.bin/chromedriver
Starting ChromeDriver (v2.10.291558) on port 9515
Only local connections are allowed.