@@ -40,7 +40,8 @@ module.exports = {
40
40
} ,
41
41
42
42
/**
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
44
45
* @param {string } css selector used to get list of elements
45
46
* @param {string } inner text to match (does not have to be visible)
46
47
* @returns {Promise }
@@ -82,9 +83,48 @@ module.exports = {
82
83
* helpers.getFirstElementContainingText('nav[role="navigation"] ul li a', 'Safety Boots').click();
83
84
*/
84
85
getFirstElementContainingText : function ( cssSelector , textToMatch ) {
86
+
85
87
return helpers . getElementsContainingText ( cssSelector , textToMatch ) . then ( function ( elements ) {
86
88
return elements [ 0 ] ;
87
89
} ) ;
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 ) ) ;
88
128
}
89
129
90
130
} ;
0 commit comments