跳到主要內容

自動化測試

測試自動化是驗證您的應用程式碼是否如預期運作的有效方法。雖然 Electron 沒有積極維護自己的測試解決方案,本指南將介紹幾種在您的 Electron 應用程式上執行端對端自動化測試的方法。

使用 WebDriver 介面

來自 ChromeDriver - Chrome 的 WebDriver

WebDriver 是一個開源工具,用於跨多個瀏覽器自動化測試 Web 應用程式。它提供導航到網頁、使用者輸入、JavaScript 執行等功能。ChromeDriver 是一個獨立伺服器,為 Chromium 實作 WebDriver 的線路協定。它由 Chromium 和 WebDriver 團隊的成員開發。

您可以使用 WebDriver 設定測試的幾種方法。

使用 WebdriverIO

WebdriverIO (WDIO) 是一個測試自動化框架,提供一個 Node.js 套件,用於使用 WebDriver 進行測試。它的生態系統還包括各種外掛程式(例如 reporter 和 services),可以幫助您組合您的測試設定。

如果您已經有現有的 WebdriverIO 設定,建議您更新您的依賴項,並根據文件中概述的方式驗證您現有的配置。

安裝測試執行器

如果您在專案中尚未使用 WebdriverIO,您可以透過在專案根目錄中執行 starter toolkit 來新增它

npm init wdio@latest ./

這會啟動一個配置精靈,幫助您組合正確的設定、安裝所有必要的套件,並產生一個 wdio.conf.js 配置文件。請務必在其中一個最初的問題 "您想進行哪種類型的測試?" 中選擇 "桌面測試 - Electron 應用程式"

將 WDIO 連接到您的 Electron 應用程式

執行配置精靈後,您的 wdio.conf.js 應該包含大致以下內容

wdio.conf.js
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']
}
}]
// ...
}

編寫您的測試

使用 WebdriverIO API 與螢幕上的元素互動。該框架提供自訂的「matchers」,使斷言您的應用程式狀態變得容易,例如:

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')
})
})

此外,WebdriverIO 允許您存取 Electron API 以取得有關您的應用程式的靜態資訊

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)
})
})

或檢索其他 Electron 程序資訊

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 幫助您啟動和關閉應用程式。

更多文件

官方 WebdriverIO 文件中找到更多關於模擬 Electron API 和其他有用資源的文件。

使用 Selenium

Selenium 是一個 Web 自動化框架,它在多種語言中公開了 WebDriver API 的綁定。他們的 Node.js 綁定在 NPM 上的 selenium-webdriver 套件下可用。

執行 ChromeDriver 伺服器

為了將 Selenium 與 Electron 一起使用,您需要下載 electron-chromedriver 二進制文件,並執行它

npm install --save-dev electron-chromedriver
./node_modules/.bin/chromedriver
Starting ChromeDriver (v2.10.291558) on port 9515
Only local connections are allowed.

記住端口號 9515,稍後將會使用。

將 Selenium 連接到 ChromeDriver

接下來,將 Selenium 安裝到您的專案中

npm install --save-dev selenium-webdriver

selenium-webdriver 與 Electron 的使用方式與一般網站相同,除了您必須手動指定如何連接 ChromeDriver 以及在哪裡找到您的 Electron 應用程式的二進制文件

test.js
const webdriver = require('selenium-webdriver')
const driver = new webdriver.Builder()
// The "9515" is the port opened by ChromeDriver.
.usingServer('http://localhost:9515')
.withCapabilities({
'goog:chromeOptions': {
// Here is the path to your Electron binary.
binary: '/Path-to-Your-App.app/Contents/MacOS/Electron'
}
})
.forBrowser('chrome') // note: use .forBrowser('electron') for selenium-webdriver <= 3.6.0
.build()
driver.get('https://www.google.com')
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver')
driver.findElement(webdriver.By.name('btnG')).click()
driver.wait(() => {
return driver.getTitle().then((title) => {
return title === 'webdriver - Google Search'
})
}, 1000)
driver.quit()

使用 Playwright

Microsoft Playwright 是一個端對端測試框架,使用瀏覽器特定的遠端除錯協議構建,類似於 Puppeteer 無頭 Node.js API,但專注於端對端測試。Playwright 透過 Electron 對 Chrome DevTools Protocol (CDP) 的支援,具有實驗性的 Electron 支援。

安裝依賴項

您可以透過您偏好的 Node.js 套件管理器安裝 Playwright。它帶有自己的 測試執行器,專為端對端測試而構建

npm install --save-dev @playwright/test
依賴項

本教學課程是使用 @playwright/test@1.41.1 編寫的。查看 Playwright 的發行說明頁面,了解可能影響以下程式碼的變更。

編寫您的測試

Playwright 透過 _electron.launch API 在開發模式下啟動您的應用程式。要將此 API 指向您的 Electron 應用程式,您可以傳遞主程序入口點的路徑(在這裡是 main.js)。

const { test, _electron: electron } = require('@playwright/test')

test('launch app', async () => {
const electronApp = await electron.launch({ args: ['main.js'] })
// close app
await electronApp.close()
})

之後,您將存取 Playwright 的 ElectronApp 類別的實例。這是一個功能強大的類別,可以存取主程序模組,例如

const { test, _electron: electron } = require('@playwright/test')

test('get isPackaged', async () => {
const electronApp = await electron.launch({ args: ['main.js'] })
const isPackaged = await electronApp.evaluate(async ({ app }) => {
// This runs in Electron's main process, parameter here is always
// the result of the require('electron') in the main app script.
return app.isPackaged
})
console.log(isPackaged) // false (because we're in development mode)
// close app
await electronApp.close()
})

它還可以從 Electron BrowserWindow 實例建立個別的 Page 物件。例如,要抓取第一個 BrowserWindow 並儲存螢幕截圖

const { test, _electron: electron } = require('@playwright/test')

test('save screenshot', async () => {
const electronApp = await electron.launch({ args: ['main.js'] })
const window = await electronApp.firstWindow()
await window.screenshot({ path: 'intro.png' })
// close app
await electronApp.close()
})

將所有這些與 Playwright 測試執行器結合使用,讓我們建立一個 example.spec.js 測試檔案,其中包含單個測試和斷言

example.spec.js
const { test, expect, _electron: electron } = require('@playwright/test')

test('example test', async () => {
const electronApp = await electron.launch({ args: ['.'] })
const isPackaged = await electronApp.evaluate(async ({ app }) => {
// This runs in Electron's main process, parameter here is always
// the result of the require('electron') in the main app script.
return app.isPackaged
})

expect(isPackaged).toBe(false)

// Wait for the first BrowserWindow to open
// and return its Page object
const window = await electronApp.firstWindow()
await window.screenshot({ path: 'intro.png' })

// close app
await electronApp.close()
})

然後,使用 npx playwright test 執行 Playwright Test。您應該在控制台中看到測試通過,並在您的文件系統上擁有一個 intro.png 螢幕截圖。

☁  $ npx playwright test

Running 1 test using 1 worker

✓ example.spec.js:4:1 › example test (1s)
資訊

Playwright Test 將自動執行任何符合 .*(test|spec)\.(js|ts|mjs) 正則表達式的文件。您可以在 Playwright Test 配置選項中自訂此匹配項。它也開箱即用地支援 TypeScript。

延伸閱讀

查看 Playwright 的文件,以獲取完整的 ElectronElectronApplication 類別 API。

使用自訂測試驅動程式

也可以使用 Node.js 的內建 IPC-over-STDIO 撰寫您自己的自訂驅動程式。自訂測試驅動程式需要您編寫額外的應用程式碼,但開銷較低,並且讓您向測試套件公開自訂方法。

要建立自訂驅動程式,我們將使用 Node.js 的 child_process API。測試套件將產生 Electron 程序,然後建立一個簡單的訊息傳遞協議

testDriver.js
const childProcess = require('node:child_process')
const electronPath = require('electron')

// spawn the process
const env = { /* ... */ }
const stdio = ['inherit', 'inherit', 'inherit', 'ipc']
const appProcess = childProcess.spawn(electronPath, ['./app'], { stdio, env })

// listen for IPC messages from the app
appProcess.on('message', (msg) => {
// ...
})

// send an IPC message to the app
appProcess.send({ my: 'message' })

在 Electron 應用程式中,您可以使用 Node.js process API 監聽訊息並發送回覆

main.js
// listen for messages from the test suite
process.on('message', (msg) => {
// ...
})

// send a message to the test suite
process.send({ my: 'message' })

我們現在可以使用 appProcess 物件從測試套件與 Electron 應用程式進行通訊。

為了方便起見,您可能希望將 appProcess 包裝在一個驅動程式物件中,該物件提供更高級別的功能。這是一個如何做到這一點的範例。讓我們從建立一個 TestDriver 類別開始

testDriver.js
class TestDriver {
constructor ({ path, args, env }) {
this.rpcCalls = []

// start child process
env.APP_TEST_DRIVER = 1 // let the app know it should listen for messages
this.process = childProcess.spawn(path, args, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], env })

// handle rpc responses
this.process.on('message', (message) => {
// pop the handler
const rpcCall = this.rpcCalls[message.msgId]
if (!rpcCall) return
this.rpcCalls[message.msgId] = null
// reject/resolve
if (message.reject) rpcCall.reject(message.reject)
else rpcCall.resolve(message.resolve)
})

// wait for ready
this.isReady = this.rpc('isReady').catch((err) => {
console.error('Application failed to start', err)
this.stop()
process.exit(1)
})
}

// simple RPC call
// to use: driver.rpc('method', 1, 2, 3).then(...)
async rpc (cmd, ...args) {
// send rpc request
const msgId = this.rpcCalls.length
this.process.send({ msgId, cmd, args })
return new Promise((resolve, reject) => this.rpcCalls.push({ resolve, reject }))
}

stop () {
this.process.kill()
}
}

module.exports = { TestDriver }

在您的應用程式碼中,可以編寫一個簡單的處理程序來接收 RPC 呼叫

main.js
const METHODS = {
isReady () {
// do any setup needed
return true
}
// define your RPC-able methods here
}

const onMessage = async ({ msgId, cmd, args }) => {
let method = METHODS[cmd]
if (!method) method = () => new Error('Invalid method: ' + cmd)
try {
const resolve = await method(...args)
process.send({ msgId, resolve })
} catch (err) {
const reject = {
message: err.message,
stack: err.stack,
name: err.name
}
process.send({ msgId, reject })
}
}

if (process.env.APP_TEST_DRIVER) {
process.on('message', onMessage)
}

然後,在您的測試套件中,您可以將您的 TestDriver 類別與您選擇的測試自動化框架一起使用。以下範例使用 ava,但其他流行的選擇,如 Jest 或 Mocha 也同樣適用

test.js
const test = require('ava')
const electronPath = require('electron')
const { TestDriver } = require('./testDriver')

const app = new TestDriver({
path: electronPath,
args: ['./app'],
env: {
NODE_ENV: 'test'
}
})
test.before(async t => {
await app.isReady
})
test.after.always('cleanup', async t => {
await app.stop()
})