forked from john-doherty/selenium-cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
90 lines (74 loc) · 3.19 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
module.exports = {
/**
* returns a promise that is called when the url has loaded and the body element is present
* @param {string} url to load
* @returns {Promise}
* @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} css selector used to find the element
* @param {string} 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 mathes param
* @param {string} css selector used to get list of elements
* @param {string} inner text to match (does not have to be visible)
* @returns {Promise}
* @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} css selector used to get list of elements
* @param {string} inner text to match (does not have to be visible)
* @returns {Promise}
* @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];
});
}
};