/
recursive-lookup.js
91 lines (76 loc) · 2.4 KB
/
recursive-lookup.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
const EventEmitter = require('events');
const Utils = require('../../utils');
/**
* Search for an element on the page, starting with the first element of the array, and where each element in the passed array is nested under the previous one. The located element will be returned as a WebElement JSON object.
*
* @param {Array} elements An array of ancestor element objects containing selector and locateStrategy properties
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @api protocol
*/
class RecursiveLookupBase extends EventEmitter {
constructor(nightwatchInstance) {
super();
this.elementLocator = nightwatchInstance.elementLocator;
this.transport = nightwatchInstance.transport;
this.createPromise();
}
getNextElement() {
return this.allElements.shift();
}
createPromise() {
this.deferred = Utils.createPromise();
return this;
}
/**
* In case we need a collection of elements, we only need to retrieve it if it's the last request from
* the recursive locate chain
* @param {Boolean} returnSingleElement
* @return {Boolean}
*/
shouldReturnSingleElement(returnSingleElement) {
if (!returnSingleElement && this.allElements.length > 0) {
return true;
}
return returnSingleElement;
}
recursiveElementLookup({result, returnSingleElement}) {
let element = this.getNextElement();
if (element) {
this.elementLocator.findElement({
id: this.transport.getElementId(Utils.isDefined(result.value) ? result.value : result),
element,
transportAction: this.transportAction,
commandName: this.commandName,
returnSingleElement: this.shouldReturnSingleElement(returnSingleElement),
cacheElementId: false
})
.then(result => {
this.recursiveElementLookup({
result,
returnSingleElement
});
})
.catch(result => {
this.deferred.reject(result);
});
} else {
if (result.error instanceof Error) {
this.deferred.reject(result.error);
return;
}
this.deferred.resolve(result);
}
}
/**
*
* @param {string|object} result
* @param {function} callback
* @return {Promise}
*/
complete(result, callback) {
callback(result);
this.emit('complete');
return result;
}
}
module.exports = RecursiveLookupBase;