2014-09-12 16:14:24 +00:00
|
|
|
# Using 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.
|
|
|
|
|
2015-09-02 17:23:37 +00:00
|
|
|
In order to use `chromedriver` with Electron you have to tell it where to
|
|
|
|
find Electron and make it think Electron is the Chrome browser.
|
2014-09-20 14:39:52 +00:00
|
|
|
|
2014-09-12 16:07:21 +00:00
|
|
|
## Setting up with WebDriverJs
|
|
|
|
|
2015-09-01 02:32:25 +00:00
|
|
|
[WebDriverJs](https://code.google.com/p/selenium/wiki/WebDriverJs) 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:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ ./chromedriver
|
|
|
|
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
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ npm install selenium-webdriver
|
|
|
|
```
|
|
|
|
|
2015-09-01 02:32:25 +00:00
|
|
|
### 3. Connect to ChromeDriver
|
2014-09-12 16:07:21 +00:00
|
|
|
|
2015-04-16 03:31:12 +00:00
|
|
|
The usage of `selenium-webdriver` with Electron is basically the same with
|
2014-09-12 16:07:21 +00:00
|
|
|
upstream, except that you have to manually specify how to connect chrome driver
|
2015-04-16 03:31:12 +00:00
|
|
|
and where to find Electron's binary:
|
2014-09-12 16:07:21 +00:00
|
|
|
|
|
|
|
```javascript
|
2015-11-12 13:20:09 +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({
|
|
|
|
chromeOptions: {
|
|
|
|
// Here is the path to your Electron binary.
|
2016-02-12 00:11:02 +00:00
|
|
|
binary: '/Path-to-Your-App.app/Contents/MacOS/Electron',
|
2015-11-10 19:55:00 +00:00
|
|
|
}
|
|
|
|
})
|
2015-06-09 16:18:47 +00:00
|
|
|
.forBrowser('electron')
|
|
|
|
.build();
|
2014-09-12 16:07:21 +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(() => {
|
2014-09-12 16:07:21 +00:00
|
|
|
return driver.getTitle().then(function(title) {
|
|
|
|
return title === 'webdriver - Google Search';
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
driver.quit();
|
|
|
|
```
|
|
|
|
|
2015-08-21 10:09:31 +00:00
|
|
|
## Setting up with WebdriverIO
|
|
|
|
|
2015-09-01 02:32:25 +00:00
|
|
|
[WebdriverIO](http://webdriver.io/) provides a Node package for testing with web
|
|
|
|
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:
|
|
|
|
|
|
|
|
```bash
|
2015-10-03 05:53:52 +00:00
|
|
|
$ 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
|
|
|
|
|
|
|
```bash
|
|
|
|
$ 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
|
2015-11-12 13:20:09 +00:00
|
|
|
const webdriverio = require('webdriverio');
|
2016-05-04 18:11:51 +00:00
|
|
|
const options = {
|
|
|
|
host: 'localhost', // Use localhost as chrome driver server
|
|
|
|
port: 9515, // "9515" is the port opened by chrome driver.
|
|
|
|
desiredCapabilities: {
|
|
|
|
browserName: 'chrome',
|
|
|
|
chromeOptions: {
|
|
|
|
binary: '/Path-to-Your-App/electron', // Path to your Electron binary.
|
|
|
|
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
|
|
|
}
|
2015-08-21 10:09:31 +00:00
|
|
|
};
|
|
|
|
|
2015-08-25 06:15:59 +00:00
|
|
|
var client = webdriverio.remote(options);
|
|
|
|
|
|
|
|
client
|
2016-05-04 18:11:51 +00:00
|
|
|
.init()
|
|
|
|
.url('http://google.com')
|
|
|
|
.setValue('#q', 'webdriverio')
|
|
|
|
.click('#btnG')
|
|
|
|
.getTitle().then((title) => {
|
|
|
|
console.log('Title was: ' + title);
|
|
|
|
})
|
|
|
|
.end();
|
2015-08-21 10:09:31 +00:00
|
|
|
```
|
|
|
|
|
2015-06-09 09:29:47 +00:00
|
|
|
## Workflow
|
|
|
|
|
2015-09-01 02:32:25 +00:00
|
|
|
To test your application without rebuilding Electron, simply
|
2016-03-31 23:49:59 +00:00
|
|
|
[place](https://github.com/electron/electron/blob/master/docs/tutorial/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
|
|
|
|
2015-11-10 19:55:00 +00:00
|
|
|
Alternatively, pass an argument to run with your electron binary that points to
|
|
|
|
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/
|