forked from john-doherty/selenium-cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
executable file
·349 lines (280 loc) · 13.8 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
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
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 seconds 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 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} htmlCssSelector - HTML 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 (htmlCssSelector, attributeName) {
// get the element from the page
return driver.findElement(by.css(htmlCssSelector)).then(function(el) {
return el.getAttribute(attributeName);
});
},
/**
* 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].trim() === content.trim()) {
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));
},
/**
* Waits until a HTML attribute equals a particular value
* @param {string} elementSelector - HTML element CSS selector
* @param {string} attributeName - name of the attribute to inspect
* @param {string} attributeValue - value to wait for attribute to equal
* @param {integer} waitInMilliseconds - number of milliseconds to wait for page to load
* @returns {Promise} resolves if attribute eventually equals, otherwise rejects
* @example
* helpers.waitUntilAttributeEquals('html', 'data-busy', 'false', 5000);
*/
waitUntilAttributeEquals: function(elementSelector, attributeName, attributeValue, waitInMilliseconds) {
// use either passed in timeout or global default
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
// readable error message
var timeoutMessage = attributeName + ' does not equal ' + attributeValue + ' after ' + waitInMilliseconds + ' milliseconds';
// repeatedly execute the test until it's true or we timeout
return driver.wait(function() {
// get the html attribute value using helper method
return helpers.getAttributeValue(elementSelector, attributeName).then(function(value) {
// inspect the value
return value === attributeValue;
});
}, timeout, timeoutMessage);
},
/**
* Waits until a HTML attribute exists
* @param {string} elementSelector - HTML element CSS selector
* @param {string} attributeName - name of the attribute to inspect
* @param {integer} waitInMilliseconds - number of milliseconds to wait for page to load
* @returns {Promise} resolves if attribute exists within timeout, otherwise rejects
* @example
* helpers.waitUntilAttributeExists('html', 'data-busy', 5000);
*/
waitUntilAttributeExists: function(elementSelector, attributeName, waitInMilliseconds) {
// use either passed in timeout or global default
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
// readable error message
var timeoutMessage = attributeName + ' does not exists after ' + waitInMilliseconds + ' milliseconds';
// repeatedly execute the test until it's true or we timeout
return driver.wait(function() {
// get the html attribute value using helper method
return helpers.getAttributeValue(elementSelector, attributeName).then(function(value) {
// attribute exists if value is not null
return value !== null;
});
}, timeout, timeoutMessage);
},
/**
* Waits until a HTML attribute no longer exists
* @param {string} elementSelector - HTML element CSS selector
* @param {string} attributeName - name of the attribute to inspect
* @param {integer} waitInMilliseconds - number of milliseconds to wait for page to load
* @returns {Promise} resolves if attribute is removed within timeout, otherwise rejects
* @example
* helpers.waitUntilAttributeDoesNotExists('html', 'data-busy', 5000);
*/
waitUntilAttributeDoesNotExists: function(elementSelector, attributeName, waitInMilliseconds) {
// use either passed in timeout or global default
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
// readable error message
var timeoutMessage = attributeName + ' still exists after ' + waitInMilliseconds + ' milliseconds';
// repeatedly execute the test until it's true or we timeout
return driver.wait(function() {
// get the html attribute value using helper method
return helpers.getAttributeValue(elementSelector, attributeName).then(function(value) {
// attribute exists if value is not null
return value === null;
});
}, timeout, timeoutMessage);
},
/**
* Waits until an css element exists and returns it
* @param {string} elementSelector - HTML element CSS selector
* @param {integer} waitInMilliseconds - (optional) number of milliseconds to wait for the element
* @returns {Promise} a promisse that will resolve if the element is found within timeout
* @example
* helpers.waitForCssXpathElement('#login-button', 5000);
*/
waitForCssXpathElement: function (elementSelector, waitInMilliseconds){
// use either passed in timeout or global default
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
// if the locator starts with '//' assume xpath, otherwise css
var selector = (localizador.indexOf('//') === 0) ? "xpath" : "css";
// readable error message
var timeoutMessage = attributeName + ' still exists after ' + waitInMilliseconds + ' milliseconds';
// wait until the element exists
return driver.wait(selenium.until.elementLocated({ [selector]: elementSelector }), timeout, timeoutMessage);
},
/**
* Scroll until element is visible
* @param {WebElement} elemento - selenium web element
* @returns {Promise} a promise that will resolve to the scripts return value.
* @example
* helpers.scrollToElement(webElement);
*/
scrollToElement: function (element) {
return driver.executeScript('return arguments[0].scrollIntoView(false);', element);
},
/**
* Select a value inside a dropdown list by its text
* @param {string} elementSelector - css or xpath selector
* @param {string} optionName - name of the option to be chosen
* @param {Promise} a promise that will resolve when the click command has completed
* @example
* helpers.selectByVisibleText('#country', 'Brazil');
*/
selectDropdownValueByVisibleText: async function (elementSelector, optionName) {
var select = await helpers.waitForCssXpathElement(elementSelector);
var selectElements = await select.findElements({ css: 'option' });
var options = [];
for (var option of selectElements) {
options.push((await option.getText()).toUpperCase());
}
optionName = optionName.toUpperCase();
return selectElements[options.indexOf(optionName)].click();
},
/**
* Awaits and returns an array of all windows opened
* @param {integer} waitInMilliseconds - (optional) number of milliseconds to wait for the result
* @returns {Promise} a promise that will resolve with an array of window handles.
* @example
* helpers.waitForNewWindows();
*/
waitForNewWindows: async function (waitInMilliseconds) {
// use either passed in timeout or global default
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
var windows = [];
for (var i = 0; i < timeout; i += 1000) {
windows = await driver.getAllWindowHandles(); // procura por todas as windows abertas
if (windows.length > 1) return windows;
await driver.sleep(1000);
}
},
/**
* Get the content value of a :before pseudo element
* @param {string} cssSelector - css selector of element to inspect
* @returns {Promise} executes .then with value
* @example
* helpers.getPseudoElementBeforeValue('body header').then(function(value) {
* console.log(value);
* });
*/
getPseudoElementBeforeValue: function(cssSelector) {
function getBeforeContentValue(qs) {
var el = document.querySelector(qs);
var styles = el ? window.getComputedStyle(el, ':before') : null;
return styles ? styles.getPropertyValue('content') : '';
}
return driver.executeScript(getBeforeContentValue, cssSelector);
},
/**
* Get the content value of a :after pseudo element
* @param {string} cssSelector - css selector of element to inspect
* @returns {Promise} executes .then with value
* @example
* helpers.getPseudoElementAfterValue('body header').then(function(value) {
* console.log(value);
* });
*/
getPseudoElementAfterValue: function(cssSelector) {
function getAfterContentValue(qs) {
var el = document.querySelector(qs);
var styles = el ? window.getComputedStyle(el, ':after') : null;
return styles ? styles.getPropertyValue('content') : '';
}
return driver.executeScript(getAfterContentValue, cssSelector);
},
clearCookies: function() {
return driver.manage().deleteAllCookies();
},
clearStorages: function() {
return driver.executeScript('window.localStorage.clear(); window.sessionStorage.clear();')
},
clearCookiesAndStorages: function() {
return helpers.clearCookies().then(helpers.clearStorages());
}
};