element-commands.js
13.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
var util = require('util');
var events = require('events');
var Logger = require('../util/logger.js');
var Utils = require('../util/utils.js');
module.exports = function(client) {
var Protocol = require('./protocol.js')(client);
var returnValue = {};
var elementCommands = {};
/**
* Simulates a click event on the given DOM element. Uses `elementIdClick` protocol command.
*
* ```
* this.demoTest = function (client) {
* client.click("#main ul li a.first");
* };
* ```
*
* @method click
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdClick
* @api commands
*/
elementCommands.click = 'elementIdClick';
/**
* Clear a textarea or a text input element's value. Uses `elementIdValue` protocol command.
*
* ```
* this.demoTest = function (client) {
* client.clearValue('input[type=text]');
* };
* ```
*
* @method clearValue
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdClear
* @api commands
*/
elementCommands.clearValue = 'elementIdClear';
/**
* Retrieve the value of an attribute for a given DOM element. Uses `elementIdAttribute` protocol command.
*
* ```
* this.demoTest = function (client) {
* client.getAttribute("#main ul li a.first", "href", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value, "#home");
* });
* };
* ```
*
* @method getAttribute
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {string} attribute The attribute name to inspect.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdAttribute
* @returns {*} The value of the attribute
* @api commands
*/
elementCommands.getAttribute = ['elementIdAttribute', 1];
/**
* Retrieve the value of a css property for a given DOM element. Uses `elementIdCssProperty` protocol command.
*
* ```
* this.demoTest = function (client) {
* client.getCssProperty("#main ul li a.first", "display", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value, 'inline');
* });
* };
* ```
*
* @method getCssProperty
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {string} cssProperty The CSS property to inspect.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdCssProperty
* @returns {*} The value of the css property
* @api commands
*/
elementCommands.getCssProperty = ['elementIdCssProperty', 1];
/**
* Determine an element's size in pixels. Uses `elementIdSize` protocol command.
*
* ```
* this.demoTest = function (client) {
* client.getElementSize("#main ul li a.first", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value.width, 500);
* this.assert.equal(result.value.height, 20);
* });
* };
* ```
*
* @method getElementSize
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdSize
* @returns {{width: number, height: number}} The width and height of the element in pixels
* @api commands
*/
elementCommands.getElementSize = 'elementIdSize';
/**
* Determine an element's location on the page. The point (0, 0) refers to the upper-left corner of the page.
*
* The element's coordinates are returned as a JSON object with x and y properties. Uses `elementIdLocation` protocol command.
*
* ```
* this.demoTest = function (client) {
* client.getLocation("#main ul li a.first", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value.x, 200);
* this.assert.equal(result.value.y, 200);
* });
* };
* ```
*
* @method getLocation
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdLocation
* @returns {x:number, y:number} The X and Y coordinates for the element on the page.
* @api commands
*/
elementCommands.getLocation = 'elementIdLocation';
/**
* Determine an element's location on the screen once it has been scrolled into view. Uses `elementIdLocationInView` protocol command.
*
* ```
* this.demoTest = function (browser) {
* browser.getLocationInView("#main ul li a.first", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value.x, 200);
* this.assert.equal(result.value.y, 200);
* });
* };
* ```
*
* @method getLocationInView
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdLocationInView
* @returns {x: number, y: number} The X and Y coordinates for the element on the page.
* @api commands
*/
elementCommands.getLocationInView = 'elementIdLocationInView';
/**
* Query for an element's tag name. Uses `elementIdName` protocol command.
*
* ```
* this.demoTest = function (client) {
* client.getTagName("#main ul li .first", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value, "a");
* });
* };
* ```
*
* @method getTagName
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdName
* @returns {number} The element's tag name, as a lowercase string.
* @api commands
*/
elementCommands.getTagName = 'elementIdName';
/**
* Returns the visible text for the element. Uses `elementIdText` protocol command.
*
* ```
* this.demoTest = function (browser) {
* browser.getText("#main ul li a.first", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value, "nightwatchjs.org");
* });
* };
* ```
*
* @method getText
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdText
* @returns {string} The element's visible text.
* @api commands
*/
elementCommands.getText = 'elementIdText';
/**
* Returns a form element current value. Uses `elementIdValue` protocol command.
*
* ```
* this.demoTest = function (browser) {
* browser.getValue("form.login input[type=text]", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value, "enter username");
* });
* };
* ```
*
* @method getValue
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdValue
* @returns {string} The element's value.
* @api commands
*/
elementCommands.getValue = 'elementIdValue';
/**
* Determine if an element is currently displayed. Uses `elementIdDisplayed` protocol command.
*
* ```
* this.demoTest = function (browser) {
* browser.isVisible('#main', function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value, true);
* });
* };
* ```
*
* @method isVisible
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdDisplayed
* @api commands
*/
elementCommands.isVisible = 'elementIdDisplayed';
/**
* Move the mouse by an offset of the specified element. Uses `moveTo` protocol command.
*
* ```
* this.demoTest = function (browser) {
* browser.moveToElement('#main', 10, 10);
* };
* ```
*
* @method moveToElement
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {number} xoffset X offset to move to, relative to the top-left corner of the element.
* @param {number} yoffset Y offset to move to, relative to the top-left corner of the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see moveTo
* @api commands
*/
elementCommands.moveToElement = ['moveTo', 2];
/**
* Sends some text to an element. Can be used to set the value of a form element or to send a sequence of key strokes to an element. Any UTF-8 character may be specified.
*
* An object map with available keys and their respective UTF-8 characters, as defined on [W3C WebDriver draft spec](http://www.w3.org/TR/webdriver/#character-types), is loaded onto the main Nightwatch instance as `client.Keys`.
*
* ```
* // send some simple text to an input
* this.demoTest = function (browser) {
* browser.setValue('input[type=text]', 'nightwatch');
* };
* //
* // send some text to an input and hit enter.
* this.demoTest = function (browser) {
* browser.setValue('input[type=text]', ['nightwatch', browser.Keys.ENTER]);
* };
* ```
*
* @link /session/:sessionId/element/:id/value
* @method setValue
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {string|array} value The text to send to the element or key strokes.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdValue
* @api commands
*/
elementCommands.setValue = ['elementIdValue', 1];
/**
* Submit a FORM element. The submit command may also be applied to any element that is a descendant of a FORM element. Uses `submit` protocol command.
*
* ```
* this.demoTest = function (browser) {
* browser.submitForm('form.login');
* };
* ```
*
* @method submitForm
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see submit
* @api commands
*/
elementCommands.submitForm = 'submit';
function addElementCommand(protocolAction, extraArgs) {
extraArgs = extraArgs || 0;
var expectedArgs = 3 + extraArgs;
return function commandActionFn() {
var originalStackTrace = commandActionFn.stackTrace;
var noopFn = function() {};
var args = Array.prototype.slice.call(arguments, 0);
if (typeof args[args.length-1] !== 'function') {
args.push(noopFn);
}
var defaultUsing = client.locateStrategy || 'css selector';
if (expectedArgs - args.length === 1) {
args.unshift(defaultUsing);
}
if (args.length < expectedArgs - 1 || args.length > expectedArgs) {
throw new Error(protocolAction + ' method expects ' + (expectedArgs - 1) + ' or ' + expectedArgs + ' arguments - ' + args.length + ' given.');
}
var using = args.shift();
var value = args.shift();
var callback = args.pop();
return new CommandAction(using, value, protocolAction, args, callback, originalStackTrace);
};
}
function CommandAction(using, value, protocolAction, args, callback, originalStackTrace) {
events.EventEmitter.call(this);
var $this = this;
var el = Protocol.element(using, value, function(result) {
if (result.status !== 0) {
callback.call(client.api, result);
var errorMessage = 'ERROR: Unable to locate element: "' + value + '" using: ' + using;
var stack = originalStackTrace.split('\n');
stack.shift();
Utils.showStackTraceWithHeadline(errorMessage, stack);
client.results.errors++;
client.errors.push(errorMessage + '\n' + stack.join('\n'));
$this.emit('complete', el, $this);
} else {
result = result.value.ELEMENT;
args.push(function(r) {
callback.call(client.api, r);
});
args.unshift(result);
var c = Protocol[protocolAction].apply(Protocol, args).once('complete', function() {
$this.emit('complete', c, $this);
});
}
});
}
util.inherits(CommandAction, events.EventEmitter);
Object.keys(elementCommands).forEach(function(commandName) {
var args = elementCommands[commandName];
if (!Array.isArray(args)) {
args = [args];
}
returnValue[commandName] = addElementCommand.apply(client.api, args);
});
// alias
returnValue.sendKeys = returnValue.setValue;
return returnValue;
};