-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathworld.js
executable file
·267 lines (207 loc) · 7.98 KB
/
world.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'use strict';
/**
* world.js is loaded by the cucumber framework before loading the step definitions and feature files
* it is responsible for setting up and exposing the driver/browser/expect/assert etc required within each step definition
*/
var fs = require('fs-plus');
var path = require('path');
var requireDir = require('require-dir');
var merge = require('merge');
var chalk = require('chalk');
var selenium = require('selenium-webdriver');
var expect = require('chai').expect;
var assert = require('chai').assert;
var reporter = require('cucumber-html-reporter');
var cucumberJunit = require('cucumber-junit');
// Initialize the eyes SDK and set your private API key.
var Eyes = require('eyes.selenium').Eyes;
// drivers
var FireFoxDriver = require('./firefoxDriver.js');
var PhantomJSDriver = require('./phantomDriver.js');
var ElectronDriver = require('./electronDriver.js');
var ChromeDriver = require('./chromeDriver');
/**
* create the selenium browser based on global var set in index.js
* @returns {ThenableWebDriver} selenium web driver
*/
function getDriverInstance() {
var driver;
switch (browserName || '') {
case 'firefox': {
driver = new FireFoxDriver();
}
break;
case 'phantomjs': {
driver = new PhantomJSDriver();
}
break;
case 'electron': {
driver = new ElectronDriver();
}
break;
case 'chrome': {
driver = new ChromeDriver();
}
break;
// try to load from file
default: {
var driverFileName = path.resolve(process.cwd(), browserName);
if (!fs.isFileSync(driverFileName)) {
throw new Error('Could not find driver file: ' + driverFileName);
}
driver = require(driverFileName)();
}
}
return driver;
}
/**
* Initialize the eyes SDK and set your private API key via the config file.
*/
function getEyesInstance() {
if (global.eyesKey) {
var eyes = new Eyes();
// retrieve eyes api key from config file in the project root as defined by the user
eyes.setApiKey(global.eyesKey);
return eyes;
}
return null;
}
function consoleInfo() {
var args = [].slice.call(arguments),
output = chalk.bgBlue.white('\n>>>>> \n' + args + '\n<<<<<\n');
console.log(output);
}
/**
* Creates a list of variables to expose globally and therefore accessible within each step definition
* @returns {void}
*/
function createWorld() {
var runtime = {
driver: null, // the browser object
eyes: null,
selenium: selenium, // the raw nodejs selenium driver
By: selenium.By, // in keeping with Java expose selenium By
by: selenium.By, // provide a javascript lowercase version
until: selenium.until, // provide easy access to selenium until methods
expect: expect, // expose chai expect to allow variable testing
assert: assert, // expose chai assert to allow variable testing
trace: consoleInfo, // expose an info method to log output to the console in a readable/visible format
page: global.page || {}, // empty page objects placeholder
shared: global.shared || {} // empty shared objects placeholder
};
// expose properties to step definition methods via global variables
Object.keys(runtime).forEach(function (key) {
if (key === 'driver' && browserTeardownStrategy !== 'always') {
return;
}
// make property/method available as a global (no this. prefix required)
global[key] = runtime[key];
});
}
/**
* Import shared objects, pages object and helpers into global scope
* @returns {void}
*/
function importSupportObjects() {
// import shared objects from multiple paths (after global vars have been created)
if (global.sharedObjectPaths && Array.isArray(global.sharedObjectPaths) && global.sharedObjectPaths.length > 0) {
var allDirs = {};
// first require directories into objects by directory
global.sharedObjectPaths.forEach(function (itemPath) {
if (fs.existsSync(itemPath)) {
var dir = requireDir(itemPath, { camelcase: true, recurse: true });
merge(allDirs, dir);
}
});
// if we managed to import some directories, expose them
if (Object.keys(allDirs).length > 0) {
// expose globally
global.shared = allDirs;
}
}
// import page objects (after global vars have been created)
if (global.pageObjectPath && fs.existsSync(global.pageObjectPath)) {
// require all page objects using camel case as object names
global.page = requireDir(global.pageObjectPath, { camelcase: true, recurse: true });
}
// add helpers
global.helpers = require('../runtime/helpers.js');
}
function closeBrowser() {
// firefox quits on driver.close on the last window
return driver.close().then(function () {
if (browserName !== 'firefox'){
return driver.quit();
}
});
}
function teardownBrowser() {
switch (browserTeardownStrategy) {
case 'none':
return Promise.resolve();
case 'clear':
return helpers.clearCookiesAndStorages();
default:
return closeBrowser(driver);
}
}
// export the "World" required by cucumber to allow it to expose methods within step def's
module.exports = function () {
createWorld();
importSupportObjects();
// this.World must be set!
this.World = createWorld;
// set the default timeout for all tests
this.setDefaultTimeout(global.DEFAULT_TIMEOUT);
// create the driver and applitools eyes before scenario if it's not instantiated
this.registerHandler('BeforeScenario', function (scenario) {
if (!global.driver) {
global.driver = getDriverInstance();
}
if (!global.eyes) {
global.eyes = getEyesInstance();
}
});
this.registerHandler('AfterFeatures', function (features, done) {
var cucumberReportPath = path.resolve(global.reportsPath, 'cucumber-report.json');
if (global.reportsPath && fs.existsSync(global.reportsPath)) {
// generate the HTML report
var reportOptions = {
theme: 'bootstrap',
jsonFile: cucumberReportPath,
output: path.resolve(global.reportsPath, 'cucumber-report.html'),
reportSuiteAsScenarios: true,
launchReport: (!global.disableLaunchReport),
ignoreBadJsonFile: true
};
reporter.generate(reportOptions);
// grab the file data
var reportRaw = fs.readFileSync(cucumberReportPath).toString().trim();
var xmlReport = cucumberJunit(reportRaw);
var junitOutputPath = path.resolve(global.junitPath, 'junit-report.xml');
fs.writeFileSync(junitOutputPath, xmlReport);
}
if (browserTeardownStrategy !== 'always') {
closeBrowser().then(() => done());
}
else {
new Promise((resolve) => resolve(done()));
}
});
// executed after each scenario (always closes the browser to ensure fresh tests)
this.After(function (scenario) {
if (scenario.isFailed() && !global.noScreenshot) {
// add a screenshot to the error report
return driver.takeScreenshot().then(function (screenShot) {
scenario.attach(new Buffer(screenShot, 'base64'), 'image/png');
return teardownBrowser().then(function() {
if (eyes) {
// If the test was aborted before eyes.close was called ends the test as aborted.
return eyes.abortIfNotClosed();
}
});
});
}
return teardownBrowser();
});
};