从ChromeDriver-WebDriver for Chrome:
WebDriver是一个开源工具,可用于跨多种浏览器自动测试Web应用程序。它提供了导航到网页,用户输入,JavaScript执行等功能。ChromeDriver是独立的服务器,可为Chromium实现WebDriver的有线协议。它是由Chromium和WebDriver团队的成员开发的。
Spectron是官方支持的Electron ChromeDriver测试框架。它建立在WebdriverIO之上,并具有帮助您访问测试中的Electron API的功能,并捆绑了ChromeDriver。
$ npm install --save-dev spectron
// A simple test to verify a visible window is opened with a title const Application = require('spectron').Application const assert = require('assert') const myApp = new Application({ path: '/Applications/MyApp.app/Contents/MacOS/MyApp' }) const verifyWindowIsVisibleWithTitle = async (app) => { await app.start() try { // Check if the window is visible const isVisible = await app.browserWindow.isVisible() // Verify the window is visible assert.strictEqual(isVisible, true) // Get the window's title const title = await app.client.getTitle() // Verify the window's title assert.strictEqual(title, 'My App') } catch (error) { // Log any failures console.error('Test failed', error.message) } // Stop the application await app.stop() } verifyWindowIsVisibleWithTitle(myApp)
WebDriverJs提供了一个Node包,用于使用Web驱动程序进行测试,我们将以它为例。
首先,您需要下载chromedriver
二进制文件并运行它:
$ npm install electron-chromedriver
$ ./node_modules/.bin/chromedriver
Starting ChromeDriver (v2.10.291558) on port 9515
Only local connections are allowed.
记住端口号9515
,稍后将使用
$ npm install selenium-webdriver
selenium-webdriver
与Electron的用法与上游相同,不同之处在于您必须手动指定如何连接chrome驱动程序以及在何处找到Electron的二进制文件:
const webdriver = require('selenium-webdriver')
const driver = new webdriver.Builder()
// The "9515" is the port opened by chrome driver.
.usingServer('http://localhost:9515')
.withCapabilities({
chromeOptions: {
// Here is the path to your Electron binary.
binary: '/Path-to-Your-App.app/Contents/MacOS/Electron'
}
})
.forBrowser('electron')
.build()
driver.get('http://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()
WebdriverIO提供了一个Node包,用于使用Web驱动程序进行测试。
首先,您需要下载chromedriver
二进制文件并运行它:
$ npm install electron-chromedriver
$ ./node_modules/.bin/chromedriver --url-base=wd/hub --port=9515
Starting ChromeDriver (v2.10.291558) on port 9515
Only local connections are allowed.
记住端口号9515
,稍后将使用
$ npm install webdriverio
const webdriverio = require('webdriverio')
const options = {
host: 'localhost', // Use localhost as chrome driver server
port: 9515, // "9515" is the port opened by chrome driver.
desiredCapabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
binary: '/Path-to-Your-App/electron', // Path to your Electron binary.
args: [/* cli arguments */] // Optional, perhaps 'app=' + /path/to/your/app/
}
}
}
const client = webdriverio.remote(options)
client
.init()
.url('http://google.com')
.setValue('#q', 'webdriverio')
.click('#btnG')
.getTitle().then((title) => {
console.log('Title was: ' + title)
})
.end()