forked from nightwatchjs/nightwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.js
299 lines (241 loc) · 7 KB
/
globals.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const path = require('path');
const lodashClone = require('lodash.clone');
const TestHooks = require('./hooks.js');
const Context = require('./context.js');
const Utils = require('../utils');
const {Logger} = Utils;
const PluginLoader = require('../api/_loaders/plugin.js');
class GlobalsContext extends Context {
throwError(err) {
err.detailedErr = err.message;
err.message = `cannot read external global file using "${this.settings.globals_path}"`;
err.showTrace = false;
throw err;
}
get isTestHook() {
return true;
}
constructor(settings, argv, currentEnv) {
super({modulePath: settings.globals_path, settings, argv});
this.currentEnv = currentEnv;
this.argv = argv;
}
get testsuite() {
return this.module;
}
init(asyncLoading) {
return this.initModule(() => {
if (asyncLoading) {
return this.loadModule();
}
return this.loadModuleSync();
}, () => {
this.externalGlobals = Object.assign({}, this.__module);
this.__module = this.settings.globals;
this.mergeGlobalsOntoSettings();
this.setCurrentEnv();
}, asyncLoading);
}
initModule(loadFn, callback, asyncLoading) {
if (!this.settings.globals_path) {
this.__module = this.settings.globals;
this.setCurrentEnv();
if (asyncLoading) {
return Promise.resolve();
}
return;
}
const result = loadFn.call(this);
if (result instanceof Promise) {
return result.then(() => {
callback.call(this);
});
}
callback.call(this);
}
async loadModule(isRetry = false) {
try {
this.__module = await Utils.requireModule(this.modulePath);
if (!this.__module) {
throw new Error(`Empty module provided in: "${this.modulePath}".`);
}
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND' && !isRetry) {
this.modulePath = path.join(Utils.getConfigFolder(this.argv), this.settings.globals_path);
await this.loadModule(true);
return this;
}
this.throwError(err);
}
}
loadModuleSync(isRetry = false) {
try {
this.__module = Utils.requireModule(this.modulePath);
if (!this.__module) {
throw new Error(`Empty module provided in: "${this.modulePath}".`);
}
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND' && !isRetry) {
this.modulePath = path.join(Utils.getConfigFolder(this.argv), this.settings.globals_path);
this.loadModuleSync(true);
return this;
}
this.throwError(err);
}
return this;
}
/**
* Merges the contents of the external globals back to the settings object in order for it to be available between
* test runs;
*
* This can be either as it is, so that any changes on the globals are maintained, or as a deep copy
*
* @return {GlobalsContext}
*/
mergeGlobalsOntoSettings() {
let externalGlobals;
if (this.settings.persist_globals) {
externalGlobals = this.externalGlobals;
} else {
// if we already have globals, make a copy of them
externalGlobals = lodashClone(this.externalGlobals, true);
}
Object.assign(this.__module, externalGlobals);
return this;
}
extendContextWithApi(context, api) {
if (('client' in context) && this.modulePath &&
context.client && !('currentTest' in context.client)) {
throw new Error('There is already a .client property defined in: ' + this.modulePath);
}
Object.defineProperty(context, 'client', {
enumerable: false,
configurable: false,
writable: true,
value: api
});
}
callAsync({fnName, api, expectedArgs = 2, isTestHook = false, isES6Async, done = function() {}}) {
if (isES6Async && isTestHook && expectedArgs === 1) {
return this.module[fnName].call(this.module, api)
.then(function(result) {
return done(result);
})
.catch(function(err) {
Logger.error(err);
return done(err);
});
}
let fnAsync = Utils.makeFnAsync(expectedArgs, this.module[fnName], this.module);
let args = this.getHookFnArgs(done, api, expectedArgs);
return fnAsync.apply(this.module, args);
}
/**
*
* @param {string} fnName
* @param args
*/
call(fnName, ...args) {
const client = args[0];
if (client) {
client.isES6AsyncTestcase = this.isES6Async(fnName);
args[0] = client.api;
}
return this.module[fnName].apply(this.module, args);
}
hasHook(key) {
return typeof this.moduleCopy[key] == 'function';
}
getKey(key) {
return this.moduleCopy[key];
}
/**
*
* @param {function} done
* @param {Object} api
* @param {Number} expectedArgs
* @return []
*/
getHookFnArgs(done, api, expectedArgs) {
return expectedArgs === 1 ? [done] : [api, done];
}
setCurrentEnv() {
this.moduleCopy = lodashClone(this.module, true);
// select globals from the current environment
if (this.currentEnv) {
/*eslint no-prototype-builtins: 'warn'*/
if (this.currentEnv && this.__module.hasOwnProperty(this.currentEnv)) {
Object.assign(this.__module, this.module[this.currentEnv]);
}
}
}
}
class Globals {
constructor(settings, argv, currentEnv = '') {
this.settings = settings;
this.argv = argv;
this.currentEnv = currentEnv;
}
get globals() {
return this.context.module;
}
loadPlugins() {
const definition = this.settings.plugins || [];
this.plugins = definition.reduce((prev, pluginName) => {
const plugin = PluginLoader.load(pluginName);
if (plugin.globals) {
prev.push(plugin);
}
return prev;
}, []);
}
init() {
this.mergeWithExisting();
this.setupGlobalHooks();
this.loadPlugins();
}
readExternal(asyncLoading) {
this.context = new GlobalsContext(this.settings, this.argv, this.currentEnv);
return this.context.init(asyncLoading);
}
/**
* Shallow merge with existing globals on the settings object
*/
mergeWithExisting() {
const settingsCopy = lodashClone(this.settings, true);
delete settingsCopy.globals;
try {
Object.defineProperty(this.globals, 'settings', {
enumerable: false,
configurable: false,
get() {
return settingsCopy;
}
});
// eslint-disable-next-line
} catch (err) {}
this.settings.globals = this.globals;
}
setupGlobalHooks() {
this.hooks = new TestHooks(this.context, {
isGlobal: true,
asyncHookTimeout: this.settings.globals.asyncHookTimeout
});
}
async runPluginHook(hookName, args) {
if (this.plugins.length === 0) {
return;
}
await Promise.all(this.plugins.map(plugin => {
if (Utils.isFunction(plugin.globals[hookName])) {
return plugin.globals[hookName].apply(this.settings.globals, args);
}
return Promise.resolve();
}));
}
async runGlobalHook(key, args = []) {
await this.hooks[key].run();
await this.runPluginHook(key, args);
}
}
module.exports = Globals;