Skip to content

Commit 1c0db36

Browse files
author
Temilayo kufeji
committed
Integrate Applitools with selenium cucumber js .
The Applitools Eyes Selenium JavaScript SDK allows you to easily add visual checkpoints to your JavaScript Selenium tests. It takes care of getting screenshots of your application from the underlying WebDriver, sending them to the Eyes server for validation and failing the test in case differences are found.
1 parent 6497b3e commit 1c0db36

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@ program
5353
.option('-n, --noScreenshot [optional]', 'disable auto capturing of screenshots when an error is encountered')
5454
.parse(process.argv);
5555

56-
program.on('--help', function() {
56+
program.on('--help', function () {
5757
console.log(' For more details please visit https://github.com/john-doherty/selenium-cucumber-js#readme\n');
5858
});
5959

6060
// store browserName globally (used within world.js to build driver)
6161
global.browserName = program.browser;
6262

63+
// store Eyes Api globally (used within world.js to set Eyes)
64+
global.eyeskey = config.eye_key
65+
6366
// used within world.js to import page objects
6467
global.pageObjectPath = path.resolve(program.pageObjects);
6568

6669
// used within world.js to output reports
6770
global.reportsPath = path.resolve(program.reports);
68-
if (!fs.existsSync(program.reports)){
71+
if (!fs.existsSync(program.reports)) {
6972
fs.makeTreeSync(program.reports);
7073
}
7174

@@ -82,7 +85,7 @@ global.junitPath = path.resolve(program.junit || program.reports);
8285
global.DEFAULT_TIMEOUT = global.DEFAULT_TIMEOUT || program.timeOut || 10 * 1000;
8386

8487
// used within world.js to import shared objects into the shared namespace
85-
global.sharedObjectPaths = program.sharedObjects.map(function(item) {
88+
global.sharedObjectPaths = program.sharedObjects.map(function (item) {
8689
return path.resolve(item);
8790
});
8891

@@ -111,8 +114,8 @@ process.argv.push(path.resolve(program.steps));
111114
// add tag
112115
if (program.tags) {
113116
program.tags.forEach(function (tag) {
114-
process.argv.push('-t');
115-
process.argv.push(tag);
117+
process.argv.push('-t');
118+
process.argv.push(tag);
116119
})
117120
}
118121

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
"fs-plus": "2.9.1",
5353
"geckodriver": "1.1.2",
5454
"merge": "1.2.0",
55-
"phantomjs-prebuilt": "2.1.12",
5655
"require-dir": "0.3.2",
5756
"selenium-webdriver": "3.0.0"
5857
},
5958
"devDependencies": {
6059
"eslint": "^3.19.0",
6160
"eslint-config-airbnb-base": "^11.2.0",
62-
"eslint-plugin-import": "^2.2.0"
61+
"eslint-plugin-import": "^2.2.0",
62+
"eyes.selenium": "0.0.72"
6363
},
6464
"eslintConfig": {
6565
"extends": "airbnb-base",

runtime/world.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ var assert = require('chai').assert;
1616
var reporter = require('cucumber-html-reporter');
1717
var cucumberJunit = require('cucumber-junit');
1818

19+
// Initialize the eyes SDK and set your private API key.
20+
var Eyes = require('eyes.selenium').Eyes;
21+
1922
// drivers
2023
var FireFoxDriver = require('./firefoxDriver.js');
2124
var PhantomJSDriver = require('./phantomDriver.js');
@@ -34,19 +37,23 @@ function getDriverInstance() {
3437

3538
case 'firefox': {
3639
driver = new FireFoxDriver();
37-
} break;
40+
}
41+
break;
3842

3943
case 'phantomjs': {
4044
driver = new PhantomJSDriver();
41-
} break;
45+
}
46+
break;
4247

4348
case 'electron': {
4449
driver = new electronDriver();
45-
} break;
50+
}
51+
break;
4652

4753
case 'chrome': {
4854
driver = new ChromeDriver();
49-
} break;
55+
}
56+
break;
5057

5158
// try to load from file
5259
default: {
@@ -62,6 +69,21 @@ function getDriverInstance() {
6269
return driver;
6370
}
6471

72+
73+
/**
74+
* Initialize the eyes SDK and set your private API key via the config file.*/
75+
function getEyesInstance() {
76+
77+
var eyes = new Eyes();
78+
79+
//retrieve apikey from config file in the project root as defined by the user
80+
eyes.setApiKey(eyeskey);
81+
82+
return eyes;
83+
84+
85+
}
86+
6587
function consoleInfo() {
6688
var args = [].slice.call(arguments),
6789
output = chalk.bgBlue.white('\n>>>>> \n' + args + '\n<<<<<\n');
@@ -77,6 +99,7 @@ function createWorld() {
7799

78100
var runtime = {
79101
driver: null, // the browser object
102+
eyes: null,
80103
selenium: selenium, // the raw nodejs selenium driver
81104
By: selenium.By, // in keeping with Java expose selenium By
82105
by: selenium.By, // provide a javascript lowercase version
@@ -112,7 +135,7 @@ function importSupportObjects() {
112135

113136
if (fs.existsSync(itemPath)) {
114137

115-
var dir = requireDir(itemPath, { camelcase: true });
138+
var dir = requireDir(itemPath, {camelcase: true});
116139

117140
merge(allDirs, dir);
118141
}
@@ -130,7 +153,7 @@ function importSupportObjects() {
130153
if (global.pageObjectPath && fs.existsSync(global.pageObjectPath)) {
131154

132155
// require all page objects using camel case as object names
133-
global.page = requireDir(global.pageObjectPath, { camelcase: true });
156+
global.page = requireDir(global.pageObjectPath, {camelcase: true});
134157
}
135158

136159
// add helpers
@@ -149,12 +172,14 @@ module.exports = function () {
149172
// set the default timeout for all tests
150173
this.setDefaultTimeout(global.DEFAULT_TIMEOUT);
151174

152-
// create the driver before scenario if it's not instantiated
153-
this.registerHandler('BeforeScenario', function(scenario) {
175+
// create the driver and applitools eyes before scenario if it's not instantiated
176+
this.registerHandler('BeforeScenario', function (scenario) {
154177

155-
if (!global.driver) {
178+
if (!global.driver || !global.eyes) {
156179
global.driver = getDriverInstance();
180+
global.eyes = getEyesInstance();
157181
}
182+
158183
});
159184

160185
this.registerHandler('AfterFeatures', function (features, done) {
@@ -196,14 +221,20 @@ module.exports = function () {
196221

197222
scenario.attach(new Buffer(screenShot, 'base64'), 'image/png');
198223

199-
return driver.close().then(function() {
224+
return driver.close().then(function () {
200225
return driver.quit();
201-
});
226+
})
227+
}).then(function () {
228+
// If the test was aborted before eyes.close was called ends the test as aborted.
229+
return eyes.abortIfNotClosed();
202230
});
203231
}
204232

205-
return driver.close().then(function() {
233+
return driver.close().then(function () {
206234
return driver.quit();
235+
}).then(function () {
236+
// If the test was aborted before eyes.close was called ends the test as aborted.
237+
return eyes.abortIfNotClosed();
207238
});
208239
});
209240
};

0 commit comments

Comments
 (0)