@@ -10,7 +10,7 @@ module.exports = {
10
10
*/
11
11
loadPage : function ( url , waitInSeconds ) {
12
12
13
- // use either passed in timeout or global 10 seconds default
13
+ // use either passed in timeout or global default
14
14
var timeout = ( waitInSeconds ) ? ( waitInSeconds * 1000 ) : DEFAULT_TIMEOUT ;
15
15
16
16
// load the url and wait for it to complete
@@ -23,20 +23,17 @@ module.exports = {
23
23
24
24
/**
25
25
* returns the value of an attribute on an element
26
- * @param {string } selector - css selector used to find the element
26
+ * @param {string } htmlCssSelector - HTML css selector used to find the element
27
27
* @param {string } attributeName - attribute name to retrieve
28
28
* @returns {string } the value of the attribute or empty string if not found
29
29
* @example
30
30
* helpers.getAttributeValue('body', 'class');
31
31
*/
32
- getAttributeValue : function ( selector , attributeName ) {
32
+ getAttributeValue : function ( htmlCssSelector , attributeName ) {
33
33
34
34
// get the element from the page
35
- return driver . findElement ( by . css ( selector ) ) . then ( function ( attributeValue ) {
36
- return attributeValue ;
37
- } )
38
- . catch ( function ( ) {
39
- return '' ;
35
+ return driver . findElement ( by . css ( htmlCssSelector ) ) . then ( function ( el ) {
36
+ return el . getAttribute ( attributeName ) ;
40
37
} ) ;
41
38
} ,
42
39
@@ -127,6 +124,34 @@ module.exports = {
127
124
128
125
// grab matching elements
129
126
return driver . findElements ( by . js ( clickElementInDom , cssSelector , textToMatch ) ) ;
127
+ } ,
128
+
129
+ /**
130
+ * Waits until a HTML attribute equals a particular value
131
+ * @param {string } elementSelector - HTML element CSS selector
132
+ * @param {string } attributeName - name of the attribute to inspect
133
+ * @param {string } attributeValue - value to wait for attribute to equal
134
+ * @param {integer } waitInSeconds - number of milliseconds to wait for page to load
135
+ * @returns {Promise } resolves if attribute eventually equals, otherwise rejects
136
+ * @example
137
+ * helpers.waitUntilAttributeEquals('html', 'data-busy', 'false', 5);
138
+ */
139
+ waitUntilAttributeEquals : function ( elementSelector , attributeName , attributeValue , waitInSeconds ) {
140
+
141
+ // use either passed in timeout or global default
142
+ var timeout = ( waitInSeconds ) ? ( waitInSeconds * 1000 ) : DEFAULT_TIMEOUT ;
143
+
144
+ // repeatedly execute the test until it's true or we timeout
145
+ return driver . wait ( function ( ) {
146
+
147
+ // get the html attribute value using another helper method
148
+ return helpers . getAttributeValue ( elementSelector , attributeName ) . then ( function ( value ) {
149
+
150
+ // inspect the value
151
+ return value === attributeValue ;
152
+ } ) ;
153
+
154
+ } , timeout ) ;
130
155
}
131
156
132
157
} ;
0 commit comments