forked from john-doherty/selenium-cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
executable file
·132 lines (107 loc) · 5.18 KB
/
helpers.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
module.exports = {
/**
* returns a promise that is called when the url has loaded and the body element is present
* @param {string} url - url to load
* @param {integer} waitInSeconds - number of milliseconds to wait for page to load
* @returns {Promise} resolved when url has loaded otherwise rejects
* @example
* helpers.loadPage('http://www.google.com');
*/
loadPage: function(url, waitInSeconds) {
// use either passed in timeout or global 10 seconds default
var timeout = (waitInSeconds) ? (waitInSeconds * 1000) : DEFAULT_TIMEOUT;
// load the url and wait for it to complete
return driver.get(url).then(function() {
// now wait for the body element to be present
return driver.wait(until.elementLocated(by.css('body')), timeout);
});
},
/**
* returns the value of an attribute on an element
* @param {string} selector - css selector used to find the element
* @param {string} attributeName - attribute name to retrieve
* @returns {string} the value of the attribute or empty string if not found
* @example
* helpers.getAttributeValue('body', 'class');
*/
getAttributeValue: function (selector, attributeName) {
// get the element from the page
return driver.findElement(by.css(selector)).then(function(attributeValue) {
return attributeValue;
})
.catch(function() {
return '';
});
},
/**
* returns list of elements matching a query selector who's inner text matches param.
* WARNING: The element returned might not be visible in the DOM and will therefore have restricted interactions
* @param {string} cssSelector - css selector used to get list of elements
* @param {string} textToMatch - inner text to match (does not have to be visible)
* @returns {Promise} resolves with list of elements if query matches, otherwise rejects
* @example
* helpers.getElementsContainingText('nav[role="navigation"] ul li a', 'Safety Boots')
*/
getElementsContainingText: function(cssSelector, textToMatch) {
// method to execute within the DOM to find elements containing text
function findElementsContainingText(query, content) {
var results = []; // array to hold results
// workout which property to use to get inner text
var txtProp = ('textContent' in document) ? 'textContent' : 'innerText';
// get the list of elements to inspect
var elements = document.querySelectorAll(query);
for (var i = 0, l = elements.length; i < l; i++) {
if (elements[i][txtProp] === content) {
results.push(elements[i]);
}
}
return results;
}
// grab matching elements
return driver.findElements(by.js(findElementsContainingText, cssSelector, textToMatch));
},
/**
* returns first elements matching a query selector who's inner text matches textToMatch param
* @param {string} cssSelector - css selector used to get list of elements
* @param {string} textToMatch - inner text to match (does not have to be visible)
* @returns {Promise} resolves with first element containing text otherwise rejects
* @example
* helpers.getFirstElementContainingText('nav[role="navigation"] ul li a', 'Safety Boots').click();
*/
getFirstElementContainingText: function(cssSelector, textToMatch) {
return helpers.getElementsContainingText(cssSelector, textToMatch).then(function(elements) {
return elements[0];
});
},
/**
* clicks an element (or multiple if present) that is not visible, useful in situations where a menu needs a hover before a child link appears
* @param {string} cssSelector - css selector used to locate the elements
* @param {string} textToMatch - text to match inner content (if present)
* @returns {Promise} resolves if element found and clicked, otherwise rejects
* @example
* helpers.clickHiddenElement('nav[role="navigation"] ul li a','Safety Boots');
*/
clickHiddenElement: function(cssSelector, textToMatch) {
// method to execute within the DOM to find elements containing text
function clickElementInDom(query, content) {
// get the list of elements to inspect
var elements = document.querySelectorAll(query);
// workout which property to use to get inner text
var txtProp = ('textContent' in document) ? 'textContent' : 'innerText';
for (var i = 0, l = elements.length; i < l; i++) {
// if we have content, only click items matching the content
if (content) {
if (elements[i][txtProp] === content) {
elements[i].click();
}
}
// otherwise click all
else {
elements[i].click();
}
}
}
// grab matching elements
return driver.findElements(by.js(clickElementInDom, cssSelector, textToMatch));
}
};