使用 Selenium 和 WebDriver

引自 ChromeDriver - WebDriver for Chrome:

WebDriver 是一款开源的支持多浏览器的自动化测试工具。 它提供了操作网页、用户输入、JavaScript 执行等能力。 ChromeDriver 是一个实现了 WebDriver 与 Chromium 联接协议的独立服务。 它也是由开发了 Chromium 和 WebDriver 的团队开发的。

配置 Spectron

Spectron 是 Electron 官方支持的 ChromeDriver 测试框架。 它是建立在 WebdriverIO 的顶层,并且 帮助你在测试中访问 Electron API 和绑定 ChromeDriver。

  1. $ npm install --save-dev spectron
  1. // 一个简单的验证测试和一个带标题的可是窗口
  2. const Application = require('spectron').Application
  3. const assert = require('assert')
  4. const myApp = new Application({
  5. path: '/Applications/MyApp.app/Contents/MacOS/MyApp'
  6. })
  7. const verifyWindowIsVisibleWithTitle = async (app) => {
  8. await app.start()
  9. try {
  10. // 检查窗口是否可见
  11. const isVisible = await app.browserWindow.isVisible()
  12. // 验证窗口是否可见
  13. assert.strictEqual(isVisible, true)
  14. // 获取窗口标题
  15. const title = await app.client.getTitle()
  16. // 验证窗口标题
  17. assert.strictEqual(title, 'My App')
  18. } catch (error) {
  19. // 记录任何故障
  20. console.error('Test failed', error.message)
  21. }
  22. // 停止应用
  23. await app.stop()
  24. }
  25. verifyWindowIsVisibleWithTitle(myApp)

通过 WebDriverJs 配置

WebDriverJs 是一个可以配合 WebDriver 做测试的 node 模块,我们会用它来做个演示。

1. 启动 ChromeDriver

首先,你要下载 chromedriver,然后运行以下命令:

  1. $ npm install electron-chromedriver
  2. $ ./node_modules/.bin/chromedriver
  3. Starting ChromeDriver (v2.10.291558) on port 9515
  4. Only local connections are allowed.

记住 9515 这个端口号,我们后面会用到

2. 安装 WebDriverJS

  1. $ npm install selenium-webdriver

3. 连接到 ChromeDriver

在 Electron 下使用 selenium-webdriver 和其平时的用法并没有大的差异,只是你需要手动设置连接 ChromeDriver,以及 Electron 的路径:

  1. const webdriver = require('selenium-webdriver')
  2. const driver = new webdriver.Builder()
  3. // "9515" 是ChromeDriver使用的端口
  4. .usingServer('http://localhost:9515')
  5. .withCapabilities({
  6. chromeOptions: {
  7. // 这里设置Electron的路径
  8. binary: '/Path-to-Your-App.app/Contents/MacOS/Electron'
  9. }
  10. })
  11. .forBrowser('electron')
  12. .build()
  13. driver.get('http://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()

通过 WebdriverIO 配置

WebdriverIO 也是一个配合 WebDriver 用来测试的 node 模块.

1. 启动 ChromeDriver

首先,你要下载 chromedriver,然后运行以下命令:

  1. $ npm install electron-chromedriver
  2. $ ./node_modules/.bin/chromedriver --url-base=wd/hub --port=9515
  3. Starting ChromeDriver (v2.10.291558) on port 9515
  4. Only local connections are allowed.

记住 9515 这个端口号,我们后面会用到

2. 安装 WebdriverIO

  1. $ npm install webdriverio

3. 连接到 chrome driver

  1. const webdriverio = require('webdriverio')
  2. const options = {
  3. host: 'localhost', // Use localhost as chrome driver server
  4. port: 9515, // "9515" is the port opened by chrome driver.
  5. desiredCapabilities: {
  6. browserName: 'chrome',
  7. chromeOptions: {
  8. binary: '/Path-to-Your-App/electron', // Electron的路径
  9. args: [/* cli arguments */] // Optional, perhaps 'app=' + /path/to/your/app/
  10. }
  11. }
  12. }
  13. let client = webdriverio.remote(options)
  14. client
  15. .init()
  16. .url('http://google.com')
  17. .setValue('#q', 'webdriverio')
  18. .click('#btnG')
  19. .getTitle().then((title) => {
  20. console.log('Title was: ' + title)
  21. })
  22. .end()

工作流

无需重新编译 Electron,只要把 app 的源码放到 Electron的资源目录 里就可直接开始测试了。

当然,你也可以在运行Electron时传入参数指定你的应用所在文件夹。这样可以免去拷贝粘贴应用到Electron资源目录的步骤。