Skip to content

Commit f344e4f

Browse files
author
John Doherty
committed
Added helpers.clickHiddenElement method to click elements not visible in the DOM (hover menu items for example)
1 parent 0c4b4bb commit f344e4f

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

Diff for: runtime/helpers.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module.exports = {
4040
},
4141

4242
/**
43-
* returns list of elements matching a query selector who's inner text mathes param
43+
* returns list of elements matching a query selector who's inner text mathes param.
44+
* WARNING: The element returned might not be visible in the DOM and will therefore have restricted interactions
4445
* @param {string} css selector used to get list of elements
4546
* @param {string} inner text to match (does not have to be visible)
4647
* @returns {Promise}
@@ -82,9 +83,48 @@ module.exports = {
8283
* helpers.getFirstElementContainingText('nav[role="navigation"] ul li a', 'Safety Boots').click();
8384
*/
8485
getFirstElementContainingText: function(cssSelector, textToMatch){
86+
8587
return helpers.getElementsContainingText(cssSelector, textToMatch).then(function(elements){
8688
return elements[0];
8789
});
90+
},
91+
92+
/**
93+
* 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
94+
* @param {string} css selector used to locate the elements
95+
* @param {string} text to match inner content (if present)
96+
* @example
97+
* helpers.clickHiddenElement('nav[role="navigation"] ul li a','Safety Boots');
98+
*/
99+
clickHiddenElement: function(cssSelector, textToMatch) {
100+
101+
// method to execute within the DOM to find elements containing text
102+
function clickElementInDom(query, content) {
103+
104+
// get the list of elements to inspect
105+
var elements = document.querySelectorAll(query);
106+
107+
// workout which property to use to get inner text
108+
var txtProp = ('textContent' in document) ? 'textContent' : 'innerText';
109+
110+
for (var i=0, l=elements.length; i<l; i++) {
111+
112+
// if we have content, only click items matching the content
113+
if (content) {
114+
115+
if (elements[i][txtProp] === content){
116+
elements[i].click();
117+
}
118+
}
119+
// otherwise click all
120+
else {
121+
elements[i].click();
122+
}
123+
}
124+
};
125+
126+
// grab matching elements
127+
return driver.findElements(by.js(clickElementInDom, cssSelector, textToMatch));
88128
}
89129

90130
};

0 commit comments

Comments
 (0)