api.js
16.4 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*!
* Module dependencies.
*/
var fs = require('fs');
var path = require('path');
var util = require('util');
var events = require('events');
var assertModule = require('assert');
var CommandQueue = require('./queue.js');
var Assertion = require('./assertion.js');
var Page = require('../page-object/page.js');
module.exports = new (function() {
var client;
var custom_commands_path;
var custom_assertions_path;
var page_objects_path;
var assertOperators = {
ok : ['ok', 'ko'],
equal : ['==', '!='],
notEqual : ['!=', '=='],
deepEqual : ['deepEqual', 'not deepEqual'],
notDeepEqual : ['not deepEqual', 'deepEqual'],
strictEqual : ['===', '!=='],
notStrictEqual : ['!==', '==='],
throws : ['throws', 'doesNotThrow'],
doesNotThrow : ['doesNotThrow', 'throws'],
fail : 'fail',
ifError : 'ifError'
};
/////////////////////////////////////////////////////////////////////
// Assertions
/////////////////////////////////////////////////////////////////////
/**
* Extends the node.js assert module
*
* @param prop
* @param abortOnFailure
* @returns {Function}
*/
function makeAssertion(prop, abortOnFailure) {
return function() {
var passed;
var expected = null;
var actual = null;
var lastArgument = arguments[arguments.length-1];
var isLastArgString = typeof lastArgument === 'string';
var message = isLastArgString &&
(arguments.length > 2 || typeof arguments[0] === 'boolean') &&
lastArgument || (typeof arguments[0] === 'function' && '[Function]');
var args = Array.prototype.slice.call(arguments, 0);
try {
assertModule[prop].apply(null, arguments);
passed = true;
message = 'Passed [' + prop + ']: ' + (message || getAssertMessage(prop, args, passed));
} catch (ex) {
passed = false;
message = 'Failed [' + prop + ']: ' + ('(' + ex.message + ')' || message || getAssertMessage(prop, args, passed));
actual = ex.actual;
expected = ex.expected;
}
return Assertion.assert(passed, actual, expected, message, abortOnFailure);
};
}
function getAssertMessage(prop, args, passed) {
if (!Array.isArray(assertOperators[prop])) {
return assertOperators[prop] || '';
}
var operator = passed ? assertOperators[prop][0] : assertOperators[prop][1];
if (args.length === 2) {
args.splice(1, 0, operator);
} else {
args.push(operator);
}
args = args.map(function(argument) {
if (argument && typeof argument == 'object') {
argument = util.inspect(argument);
}
return argument;
});
return args.join(' ');
}
/**
* Loads the available assertions
*/
function loadAssertions(parent) {
parent = parent || client.api;
parent.assert = {};
if (client.options.start_session) {
parent.verify = {};
}
for (var prop in assertModule) {
if (assertModule.hasOwnProperty(prop)) {
parent.assert[prop] = (function(prop) {
return makeAssertion(prop, true);
})(prop);
if (client.options.start_session) {
parent.verify[prop] = (function (prop) {
return makeAssertion(prop, false);
})(prop);
}
}
}
if (client.options.start_session) {
var dirPath = path.join(__dirname, './../api/assertions/');
loadAssertionFiles(dirPath, parent.assert, true);
loadAssertionFiles(dirPath, parent.verify, false);
}
}
/**
* Create an instance of an assertion
*
* @param {string} commandName
* @param {function} assertionFn
* @param {boolean} abortOnFailure
* @param {object} parent
* @returns {AssertionInstance}
*/
function createAssertion(commandName, assertionFn, abortOnFailure, parent) {
var assertion;
if (typeof assertionFn === 'object' && assertionFn.assertion) {
assertion = Assertion.factory(assertionFn.assertion, abortOnFailure, client);
addCommand(commandName, assertion._commandFn, assertion, parent);
return assertion;
}
// backwards compatibility
var module = loadCommandModule(assertionFn, client.api, {
abortOnFailure : abortOnFailure
});
addCommand(commandName, module.command, module.context, parent);
return assertion;
}
/**
* Loads the actual assertion files.
*
* @param {String} dirPath
* @param {Object} parent
* @param {Boolean} abortOnFailure
*/
function loadAssertionFiles(dirPath, parent, abortOnFailure) {
var commandFiles = fs.readdirSync(dirPath);
for (var i = 0, len = commandFiles.length; i < len; i++) {
if (path.extname(commandFiles[i]) === '.js') {
var commandName = path.basename(commandFiles[i], '.js');
var assertionFn = require(path.join(dirPath, commandFiles[i]));
createAssertion(commandName, assertionFn, abortOnFailure, parent);
}
}
}
/////////////////////////////////////////////////////////////////////
// Commands
/////////////////////////////////////////////////////////////////////
/**
* Loads selenium protocol actions
*/
function loadProtocolActions(parent) {
parent = parent || client.api;
var protocol = require('./../api/protocol.js')(client);
var actions = Object.keys(protocol);
actions.forEach(function(command) {
addCommand(command, protocol[command], client.api, parent);
});
}
/**
* Loads all the composite commands defined by nightwatch
*/
function loadAllCommands(parent) {
loadElementCommands(parent);
loadClientCommands(parent);
loadCommandFiles(client.api, parent, true);
}
/**
* Loads the element composite commands defined by nightwatch
*/
function loadElementCommands(parent) {
parent = parent || client.api;
var elementCommands = require('./../api/element-commands.js')(client);
var entries = Object.keys(elementCommands);
entries.forEach(function(command) {
addCommand(command, elementCommands[command], client.api, parent);
});
}
/**
* Loads all the client commands defined by nightwatch
*/
function loadClientCommands(parent) {
parent = parent || client.api;
var clientCommands = require('./../api/client-commands.js')(client);
var entries = Object.keys(clientCommands);
entries.forEach(function(command) {
addCommand(command, clientCommands[command], client.api, parent, true);
});
}
/**
* Loads the external commands
*/
function loadCommandFiles(context, parent, shouldLoadClientCommands) {
var relativePaths = ['./../api/element-commands/'];
if (shouldLoadClientCommands) {
relativePaths.push('./../api/client-commands/');
}
relativePaths.forEach(function(relativePath) {
var commandFiles = fs.readdirSync(path.join(__dirname, relativePath));
var commandName;
var commandModule;
for (var i = 0, len = commandFiles.length; i < len; i++) {
var ext = path.extname(commandFiles[i]);
commandName = path.basename(commandFiles[i], ext);
if (ext === '.js' && commandName.substr(0, 1) !== '_') {
commandModule = require(__dirname + relativePath + commandFiles[i]);
var m = loadCommandModule(commandModule, context);
addCommand(commandName, m.command, m.context, parent);
}
}
});
}
/**
* Loads a command module either specified as an object with a `command` method
* or specified as a function which will be instantiated (new function() {..})
*
* @param {object|function} module
* @param {object} context
* @param {object} [addt_props]
* @returns {{command: function, context: *}}
*/
function loadCommandModule(module, context, addt_props, return_val) {
var m = {command: null, context: context};
function F() {
if (typeof module === 'object') {
events.EventEmitter.call(this);
}
if (addt_props) {
for (var prop in addt_props) {
if (addt_props.hasOwnProperty(prop)) {
this[prop] = addt_props[prop];
}
}
}
this.client = client;
this.api = client.api;
if (typeof module === 'function') {
module.call(this);
}
}
if (typeof module === 'object' && module.command) {
util.inherits(F, events.EventEmitter);
F.prototype.command = function() {
return module.command.apply(this, arguments);
};
} else if (typeof module === 'function') {
F.prototype = Object.create(module.prototype);
F.prototype.constructor = F;
}
m.command = function commandFn() {
var instance = new F();
if (typeof module === 'function') {
context = m.context = instance;
}
instance.command.prototype.constructor.stackTrace = commandFn.stackTrace;
instance.command.apply(context, arguments);
return context;
};
return m;
}
/**
* Loads custom commands defined by the user
* @param {string} [dirPath]
* @param {object} [parent]
*/
function loadCustomCommands(dirPath, parent) {
if (!custom_commands_path && !dirPath) {
return;
}
dirPath = dirPath || custom_commands_path;
parent = parent || client.api;
if (Array.isArray(dirPath)) {
dirPath.forEach(function(folder) {
loadCustomCommands(folder, parent);
});
return;
}
var absPath = path.resolve(dirPath);
var commandFiles = fs.readdirSync(absPath);
commandFiles.forEach(function(file) {
var fullPath = path.join(absPath, file);
if (fs.lstatSync(fullPath).isDirectory()) {
parent[file] = parent[file] || {};
var pathFolder = path.join(dirPath, file);
loadCustomCommands(pathFolder, parent[file]);
} else if (path.extname(file) === '.js') {
var commandModule = require(fullPath);
var name = path.basename(file, '.js');
if (!commandModule) {
throw new Error('Module ' + file + 'should have a public method or function.');
}
var m = loadCommandModule(commandModule, client.api);
addCommand(name, m.command, m.context, parent, true);
}
});
}
/**
* Loads custom assertions, similarly to custom commands
* @param [folder]
*/
function loadCustomAssertions(folder, parent) {
folder = folder || custom_assertions_path;
parent = parent || client.api;
if (!custom_assertions_path) {
return;
}
if (Array.isArray(folder)) {
folder.forEach(function(folderName) {
loadCustomAssertions(folderName, parent);
});
return;
}
loadCustomAssertionFolder(folder, parent);
}
function loadCustomAssertionFolder(folderName, parent) {
var absPath = path.resolve(folderName);
loadAssertionFiles(absPath, parent.assert, true);
loadAssertionFiles(absPath, parent.verify, false);
}
function loadExpectAssertions(parent) {
parent = parent || client.api;
var Expect = require('../api/expect.js')(client);
var assertions = Object.keys(Expect);
try {
var chaiExpect = module.require('chai').expect;
parent.expect = function() {
return chaiExpect.apply(chaiExpect, arguments);
};
} catch (err) {
parent.expect = {};
}
assertions.forEach(function(assertion) {
parent.expect[assertion] = function() {
var args = Array.prototype.slice.call(arguments);
var command = Expect[assertion].apply(parent, args);
function F(element) {
events.EventEmitter.call(this);
this.client = client;
this.element = element;
}
util.inherits(F, events.EventEmitter);
F.prototype.command = function commandFn() {
this.element._stackTrace = commandFn.stackTrace;
this.element.locate(this);
return this;
};
var instance = new F(command.element);
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
CommandQueue.add(assertion, instance.command, instance, [], err.stack);
return command.expect;
};
});
}
/**
* Loads page object files
* @param {string} [dirPath]
*/
function loadPageObjects(dirPath, parent) {
if (!page_objects_path && !dirPath) {
return;
}
dirPath = dirPath || page_objects_path;
client.api.page = client.api.page || {};
parent = parent || client.api.page;
if (Array.isArray(dirPath)) {
dirPath.forEach(function(folder) {
loadPageObjects(folder);
});
return;
}
var absPath = path.resolve(dirPath);
var pageFiles = fs.readdirSync(absPath);
pageFiles.forEach(function(file) {
var fullPath = path.join(absPath, file);
if (fs.lstatSync(fullPath).isDirectory()) {
parent[file] = parent[file] || {};
var pathFolder = path.join(dirPath, file);
loadPageObjects(pathFolder, parent[file]);
} else if (path.extname(file) === '.js') {
var pageName = path.basename(file, '.js');
var pageFnOrObject = require(path.join(absPath, file));
addPageObject(pageName, pageFnOrObject, client.api, parent);
}
});
}
/**
* Instantiates the page object class
* @param {String} name
* @param {Object} pageFnOrObject
* @param {Object} context
* @param {Object} parent
*/
function addPageObject(name, pageFnOrObject, context, parent) {
parent[name] = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(context);
if (useEnhancedModel(pageFnOrObject)) {
var loadOntoPageObject = function(parent) {
if (client.options.start_session) {
loadElementCommands(parent);
loadCommandFiles(client.api, parent, false);
loadExpectAssertions(parent);
// Alias
parent.expect.section = parent.expect.element;
}
loadAssertions(parent);
loadCustomCommands(null, parent);
loadCustomAssertions(null, parent);
return parent;
};
pageFnOrObject.name = name;
return new Page(pageFnOrObject, loadOntoPageObject, context, client);
}
return new (function() {
if (typeof pageFnOrObject == 'function') {
return createPageObject(pageFnOrObject, args);
}
return pageFnOrObject;
})();
};
}
function useEnhancedModel(pageFnOrObject) {
return typeof pageFnOrObject == 'object' && (pageFnOrObject.elements || pageFnOrObject.sections);
}
/**
*
* @param pageFnOrObject
* @param args
* @returns {Object}
*/
function createPageObject(pageFnOrObject, args) {
function PageObject() {
return pageFnOrObject.apply(this, args);
}
PageObject.prototype = pageFnOrObject.prototype;
return new PageObject();
}
/**
* Adds a command/assertion to the queue.
*
* @param {String} name
* @param {Object} command
* @param {Object} context
* @param {Object} [parent]
*/
function addCommand(name, command, context, parent) {
parent = parent || client.api;
if (parent[name]) {
client.results.errors++;
var error = new Error('The command "' + name + '" is already defined!');
client.errors.push(error.stack);
throw error;
}
parent[name] = function commandFn() {
var args = Array.prototype.slice.call(arguments);
var originalStackTrace;
if (commandFn.stackTrace) {
originalStackTrace = commandFn.stackTrace;
} else {
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
originalStackTrace = err.stack;
}
CommandQueue.add(name, command, context, args, originalStackTrace);
return client.api; // for chaining
};
}
/**
* Initialize the api
*
* @param {Object} c The nightwatch client instance
* @api public
*/
this.init = function(c) {
client = c;
custom_commands_path = c.options.custom_commands_path;
custom_assertions_path = c.options.custom_assertions_path;
page_objects_path = c.options.page_objects_path;
return this;
};
/**
* Loads everything
*/
this.load = function() {
if (client.options.start_session) {
loadProtocolActions();
loadAllCommands();
loadPageObjects();
loadExpectAssertions();
}
loadAssertions();
loadCustomCommands();
loadCustomAssertions();
return this;
};
this.addCommand = addCommand;
this.loadCustomCommands = loadCustomCommands;
this.loadCustomAssertions = loadCustomAssertions;
this.loadPageObjects = loadPageObjects;
this.createAssertion = createAssertion;
})();