2021-08-16 12:12:34 +00:00
|
|
|
# Selenium and WebDriver
|
2014-09-12 16:07:21 +00:00
|
|
|
|
2015-03-04 13:59:27 +00:00
|
|
|
From [ChromeDriver - WebDriver for Chrome][chrome-driver]:
|
2014-09-12 16:07:21 +00:00
|
|
|
|
|
|
|
> 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.
|
|
|
|
|
2016-05-17 22:13:26 +00:00
|
|
|
## Setting up Spectron
|
|
|
|
|
|
|
|
[Spectron][spectron] is the officially supported ChromeDriver testing framework
|
2020-11-02 09:58:14 +00:00
|
|
|
for Electron. It is built on top of [WebdriverIO](https://webdriver.io/) and
|
2016-05-17 22:14:50 +00:00
|
|
|
has helpers to access Electron APIs in your tests and bundles ChromeDriver.
|
2016-05-17 22:13:26 +00:00
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2016-05-17 22:13:26 +00:00
|
|
|
$ npm install --save-dev spectron
|
|
|
|
```
|
|
|
|
|
2016-08-16 21:50:21 +00:00
|
|
|
```javascript
|
2016-05-17 22:13:26 +00:00
|
|
|
// A simple test to verify a visible window is opened with a title
|
2018-10-16 17:41:42 +00:00
|
|
|
const Application = require('spectron').Application
|
|
|
|
const assert = require('assert')
|
2016-05-17 22:13:26 +00:00
|
|
|
|
2018-10-16 17:41:42 +00:00
|
|
|
const myApp = new Application({
|
2016-05-17 22:13:26 +00:00
|
|
|
path: '/Applications/MyApp.app/Contents/MacOS/MyApp'
|
|
|
|
})
|
|
|
|
|
2018-10-16 17:41:42 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-06-14 13:36:54 +00:00
|
|
|
// Stop the application
|
2018-10-16 17:41:42 +00:00
|
|
|
await app.stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyWindowIsVisibleWithTitle(myApp)
|
2016-05-17 22:13:26 +00:00
|
|
|
```
|
2014-09-20 14:39:52 +00:00
|
|
|
|
2014-09-12 16:07:21 +00:00
|
|
|
## Setting up with WebDriverJs
|
|
|
|
|
2020-11-02 09:58:14 +00:00
|
|
|
[WebDriverJs](https://www.selenium.dev/selenium/docs/api/javascript/index.html) provides
|
2014-09-12 16:07:21 +00:00
|
|
|
a Node package for testing with web driver, we will use it as an example.
|
|
|
|
|
2015-09-01 02:32:25 +00:00
|
|
|
### 1. Start ChromeDriver
|
2014-09-12 16:07:21 +00:00
|
|
|
|
|
|
|
First you need to download the `chromedriver` binary, and run it:
|
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2016-05-17 22:09:09 +00:00
|
|
|
$ npm install electron-chromedriver
|
|
|
|
$ ./node_modules/.bin/chromedriver
|
2014-09-12 16:07:21 +00:00
|
|
|
Starting ChromeDriver (v2.10.291558) on port 9515
|
|
|
|
Only local connections are allowed.
|
|
|
|
```
|
|
|
|
|
|
|
|
Remember the port number `9515`, which will be used later
|
|
|
|
|
|
|
|
### 2. Install WebDriverJS
|
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2014-09-12 16:07:21 +00:00
|
|
|
$ npm install selenium-webdriver
|
|
|
|
```
|
|
|
|
|
2015-09-01 02:32:25 +00:00
|
|
|
### 3. Connect to ChromeDriver
|
2014-09-12 16:07:21 +00:00
|
|
|
|
2018-05-08 05:16:09 +00:00
|
|
|
The usage of `selenium-webdriver` with Electron is the same with
|
|
|
|
upstream, except that you have to manually specify how to connect
|
|
|
|
chrome driver and where to find Electron's binary:
|
2014-09-12 16:07:21 +00:00
|
|
|
|
|
|
|
```javascript
|
2016-07-26 01:39:25 +00:00
|
|
|
const webdriver = require('selenium-webdriver')
|
2014-09-12 16:07:21 +00:00
|
|
|
|
2016-05-04 18:11:51 +00:00
|
|
|
const driver = new webdriver.Builder()
|
2015-06-09 16:18:47 +00:00
|
|
|
// The "9515" is the port opened by chrome driver.
|
|
|
|
.usingServer('http://localhost:9515')
|
2015-11-10 19:55:00 +00:00
|
|
|
.withCapabilities({
|
2021-03-18 20:35:04 +00:00
|
|
|
'goog:chromeOptions': {
|
2015-11-10 19:55:00 +00:00
|
|
|
// Here is the path to your Electron binary.
|
2016-07-26 01:39:25 +00:00
|
|
|
binary: '/Path-to-Your-App.app/Contents/MacOS/Electron'
|
2015-11-10 19:55:00 +00:00
|
|
|
}
|
|
|
|
})
|
2021-03-18 20:35:04 +00:00
|
|
|
.forBrowser('chrome') // note: use .forBrowser('electron') for selenium-webdriver <= 3.6.0
|
2016-07-26 01:39:25 +00:00
|
|
|
.build()
|
2014-09-12 16:07:21 +00:00
|
|
|
|
2016-07-26 01:39:25 +00:00
|
|
|
driver.get('http://www.google.com')
|
|
|
|
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver')
|
|
|
|
driver.findElement(webdriver.By.name('btnG')).click()
|
2016-05-04 18:11:51 +00:00
|
|
|
driver.wait(() => {
|
2016-07-26 01:39:25 +00:00
|
|
|
return driver.getTitle().then((title) => {
|
|
|
|
return title === 'webdriver - Google Search'
|
|
|
|
})
|
|
|
|
}, 1000)
|
2014-09-12 16:07:21 +00:00
|
|
|
|
2016-07-26 01:39:25 +00:00
|
|
|
driver.quit()
|
2014-09-12 16:07:21 +00:00
|
|
|
```
|
|
|
|
|
2015-08-21 10:09:31 +00:00
|
|
|
## Setting up with WebdriverIO
|
|
|
|
|
2020-11-02 09:58:14 +00:00
|
|
|
[WebdriverIO](https://webdriver.io/) provides a Node package for testing with web
|
2015-09-01 02:32:25 +00:00
|
|
|
driver.
|
2015-08-21 10:09:31 +00:00
|
|
|
|
2015-09-01 02:32:25 +00:00
|
|
|
### 1. Start ChromeDriver
|
2015-08-21 10:09:31 +00:00
|
|
|
|
|
|
|
First you need to download the `chromedriver` binary, and run it:
|
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2016-05-17 22:09:09 +00:00
|
|
|
$ npm install electron-chromedriver
|
|
|
|
$ ./node_modules/.bin/chromedriver --url-base=wd/hub --port=9515
|
2015-08-21 10:09:31 +00:00
|
|
|
Starting ChromeDriver (v2.10.291558) on port 9515
|
|
|
|
Only local connections are allowed.
|
|
|
|
```
|
|
|
|
|
|
|
|
Remember the port number `9515`, which will be used later
|
|
|
|
|
2015-08-25 06:15:59 +00:00
|
|
|
### 2. Install WebdriverIO
|
2015-08-21 10:09:31 +00:00
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2015-08-21 10:09:31 +00:00
|
|
|
$ npm install webdriverio
|
|
|
|
```
|
|
|
|
|
|
|
|
### 3. Connect to chrome driver
|
2015-09-01 02:32:25 +00:00
|
|
|
|
2015-08-21 10:09:31 +00:00
|
|
|
```javascript
|
2016-07-26 01:39:25 +00:00
|
|
|
const webdriverio = require('webdriverio')
|
2016-05-04 18:11:51 +00:00
|
|
|
const options = {
|
|
|
|
host: 'localhost', // Use localhost as chrome driver server
|
2018-09-13 16:10:51 +00:00
|
|
|
port: 9515, // "9515" is the port opened by chrome driver.
|
2016-05-04 18:11:51 +00:00
|
|
|
desiredCapabilities: {
|
|
|
|
browserName: 'chrome',
|
2019-09-24 20:36:09 +00:00
|
|
|
'goog:chromeOptions': {
|
2016-05-04 18:11:51 +00:00
|
|
|
binary: '/Path-to-Your-App/electron', // Path to your Electron binary.
|
2018-09-13 16:10:51 +00:00
|
|
|
args: [/* cli arguments */] // Optional, perhaps 'app=' + /path/to/your/app/
|
2015-08-25 06:15:59 +00:00
|
|
|
}
|
2016-05-04 18:11:51 +00:00
|
|
|
}
|
2016-07-26 01:39:25 +00:00
|
|
|
}
|
2015-08-21 10:09:31 +00:00
|
|
|
|
2020-07-09 17:18:49 +00:00
|
|
|
const client = webdriverio.remote(options)
|
2015-08-25 06:15:59 +00:00
|
|
|
|
|
|
|
client
|
2016-05-04 18:11:51 +00:00
|
|
|
.init()
|
|
|
|
.url('http://google.com')
|
|
|
|
.setValue('#q', 'webdriverio')
|
|
|
|
.click('#btnG')
|
|
|
|
.getTitle().then((title) => {
|
2016-07-26 01:39:25 +00:00
|
|
|
console.log('Title was: ' + title)
|
2016-05-04 18:11:51 +00:00
|
|
|
})
|
2016-07-26 01:39:25 +00:00
|
|
|
.end()
|
2015-08-21 10:09:31 +00:00
|
|
|
```
|
|
|
|
|
2015-06-09 09:29:47 +00:00
|
|
|
## Workflow
|
|
|
|
|
2018-05-08 05:16:09 +00:00
|
|
|
To test your application without rebuilding Electron,
|
2021-06-14 19:02:23 +00:00
|
|
|
[place](application-distribution.md)
|
2015-09-01 02:32:25 +00:00
|
|
|
your app source into Electron's resource directory.
|
2015-06-09 09:29:47 +00:00
|
|
|
|
2018-07-20 17:58:19 +00:00
|
|
|
Alternatively, pass an argument to run with your Electron binary that points to
|
2015-11-10 19:55:00 +00:00
|
|
|
your app's folder. This eliminates the need to copy-paste your app into
|
|
|
|
Electron's resource directory.
|
|
|
|
|
2015-03-04 13:59:27 +00:00
|
|
|
[chrome-driver]: https://sites.google.com/a/chromium.org/chromedriver/
|
2018-01-12 15:24:48 +00:00
|
|
|
[spectron]: https://electronjs.org/spectron
|