Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract selenium install logic to a module #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,6 @@ module.exports = {
}
}
}
/**
* selenium-download does exactly what it's name suggests;
* downloads (or updates) the version of Selenium (& chromedriver)
* on your localhost where it will be used by Nightwatch.
/the following code checks for the existence of `selenium.jar` before trying to run our tests.
*/

require('fs').stat(BINPATH + 'selenium.jar', function (err, stat) { // got it?
if (err || !stat || stat.size < 1) {
require('selenium-download').ensure(BINPATH, function(error) {
if (error) throw new Error(error); // no point continuing so exit!
console.log('✔ Selenium & Chromedriver downloaded to:', BINPATH);
});
}
});

function padLeft (count) { // theregister.co.uk/2016/03/23/npm_left_pad_chaos/
return count < 10 ? '0' + count : count.toString();
Expand Down Expand Up @@ -282,12 +267,33 @@ We have a slightly more _evolved_ `nightwatch.conf.js` (_with Saucelabs_) see:
[github.com/dwyl/learn-nightwatch/**nightwatch.conf.js**](https://github.com/dwyl/learn-nightwatch/blob/master/nightwatch.conf.js)


### 7) Running config file
### 7) Installing Selenium

Thanks to [selenium-download](https://github.com/groupon/selenium-download) we can download and install a selenium instance locally in our project. We have this small javaScript to download and install selenium:

You will need to run the config file you created to download the Selenium driver.
`install-selenium.js`
```js
const BINPATH = require('./constants').BINPATH;

/**
* selenium-download does exactly what it's name suggests;
* downloads (or updates) the version of Selenium (& chromedriver)
* on your localhost where it will be used by Nightwatch.
*/
require('fs').stat(BINPATH + 'selenium.jar', function (err, stat) { // got it?
if (err || !stat || stat.size < 1) {
require('selenium-download').ensure(BINPATH, function(error) {
if (error) throw new Error(error); // no point continuing so exit!
console.log('✔ Selenium & Chromedriver downloaded to:', BINPATH);
});
}
});
```

You can copy it and just call it to install selenium locally to your `BINPATH`:

```sh
node nightwatch.conf.BASIC.js
node install-selenium.js
```

### 8) Create Your Nightwatch Test
Expand Down Expand Up @@ -360,10 +366,10 @@ have been installed. e.g:

```js
"scripts": {
"postinstall": "node nightwatch.conf.js"
"postinstall": "node install-selenium.js"
}
```

So whenever an `npm install` or `yarn` command is invoked, the `selenium-download` package will download and install `selenium`.

### Saucelabs

Expand Down
7 changes: 7 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const CONSTANTS = {};

CONSTANTS.PKG = require('./package.json');// so we can get the version of the project
CONSTANTS.BINPATH = './node_modules/nightwatch/bin/'; // change if required
CONSTANTS.SCREENSHOT_PATH = "./node_modules/nightwatch/screenshots/" + CONSTANTS.PKG.version + "/";

module.exports = CONSTANTS;
15 changes: 15 additions & 0 deletions install-selenium.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const BINPATH = require('./constants').BINPATH;

/**
* selenium-download does exactly what it's name suggests;
* downloads (or updates) the version of Selenium (& chromedriver)
* on your localhost where it will be used by Nightwatch.
*/
require('fs').stat(BINPATH + 'selenium.jar', function (err, stat) { // got it?
if (err || !stat || stat.size < 1) {
require('selenium-download').ensure(BINPATH, function(error) {
if (error) throw new Error(error); // no point continuing so exit!
console.log('✔ Selenium & Chromedriver downloaded to:', BINPATH);
});
}
});
25 changes: 6 additions & 19 deletions nightwatch.conf.BASIC.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require('env2')('.env'); // optionally store youre Evironment Variables in .env
const SCREENSHOT_PATH = "./screenshots/";
const BINPATH = './node_modules/nightwatch/bin/';

const CONSTANTS = require('./constants');

const PKG = CONSTANTS.PKG; // so we can get the version of the project
const BINPATH = CONSTANTS.BINPATH // change if required.
const SCREENSHOT_PATH = CONSTANTS.SCREENSHOT_PATH;

// we use a nightwatch.conf.js file so we can include comments and helper functions
module.exports = {
Expand Down Expand Up @@ -39,23 +43,6 @@ module.exports = {
}
}

/**
* selenium-download does exactly what it's name suggests;
* downloads (or updates) the version of Selenium (& chromedriver)
* on your localhost where it will be used by Nightwatch.
/the following code checks for the existence of `selenium.jar` before trying to run our tests.
*/

require('fs').stat(BINPATH + 'selenium.jar', function (err, stat) { // got it?
if (err || !stat || stat.size < 1) {
require('selenium-download').ensure(BINPATH, function(error) {
if (error) throw new Error(error); // no point continuing so exit!
console.log('✔ Selenium & Chromedriver downloaded to:', BINPATH);
});
}
});


function padLeft (count) { // theregister.co.uk/2016/03/23/npm_left_pad_chaos/
return count < 10 ? '0' + count : count.toString();
}
Expand Down
24 changes: 8 additions & 16 deletions nightwatch.conf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require('env2')('.env'); // optionally store your environment variables in .env
const PKG = require('./package.json'); // so we can get the version of the project
const BINPATH = './node_modules/nightwatch/bin/'; // change if required.
const SCREENSHOT_PATH = "./node_modules/nightwatch/screenshots/" + PKG.version + "/";

const CONSTANTS = require('./constants');

const PKG = CONSTANTS.PKG; // so we can get the version of the project
const BINPATH = CONSTANTS.BINPATH // change if required.
const SCREENSHOT_PATH = CONSTANTS.SCREENSHOT_PATH;


const config = { // we use a nightwatch.conf.js file so we can include comments and helper functions
"src_folders": [
Expand Down Expand Up @@ -116,19 +120,7 @@ const config = { // we use a nightwatch.conf.js file so we can include comments
}
module.exports = config;

/**
* selenium-download does exactly what it's name suggests;
* downloads (or updates) the version of Selenium (& chromedriver)
* on your localhost where it will be used by Nightwatch.
*/
require('fs').stat(BINPATH + 'selenium.jar', function (err, stat) { // got it?
if (err || !stat || stat.size < 1) {
require('selenium-download').ensure(BINPATH, function(error) {
if (error) throw new Error(error); // no point continuing so exit!
console.log('✔ Selenium & Chromedriver downloaded to:', BINPATH);
});
}
});


function padLeft (count) { // theregister.co.uk/2016/03/23/npm_left_pad_chaos/
return count < 10 ? '0' + count : count.toString();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"node": "4.4.6"
},
"scripts": {
"postinstall": "node nightwatch.conf.js",
"postinstall": "node install-selenium.js",
"test": "./node_modules/.bin/nightwatch --env local",
"ie": "./node_modules/.bin/nightwatch -e ie11",
"sauce": "./node_modules/.bin/nightwatch -e chrome,ie11,android_s4_emulator,iphone_6_simulator",
Expand Down