@@ -37,9 +37,11 @@ export function waitForFunction(fn, arg, options = {}) {
37
37
timeElapsed += settings . delay ;
38
38
39
39
if ( timeElapsed >= settings . timeout ) {
40
- reject (
41
- `waitForFunction() did not return a truthy value\n\n${ fn . toString ( ) } \n\n`
42
- ) ;
40
+ const msg = `waitForFunction did not return a truthy value (${
41
+ settings . timeout
42
+ } ms): ${ fn . toString ( ) } \n`;
43
+
44
+ reject ( msg ) ;
43
45
}
44
46
} , settings . delay ) ;
45
47
} ) ;
@@ -71,9 +73,9 @@ export function waitForSelector(cssSelector, options = {}) {
71
73
timeElapsed += settings . delay ;
72
74
73
75
if ( timeElapsed >= settings . timeout ) {
74
- reject (
75
- `waitForSelector() was unable to find CSS selector ${ cssSelector } `
76
- ) ;
76
+ const msg = `waitForSelector did not match CSS selector ' ${ cssSelector } ' ( ${ settings . timeout } ms)` ;
77
+
78
+ reject ( msg ) ;
77
79
}
78
80
} , settings . delay ) ;
79
81
} ) ;
@@ -94,20 +96,31 @@ export function waitForText(cssSelector, text, options = {}) {
94
96
} ;
95
97
96
98
return new Promise ( ( resolve , reject ) => {
99
+ let timeElapsed = 0 ;
100
+
97
101
waitForSelector ( cssSelector , settings )
98
102
. then ( elm => {
99
- const isMatch = elm . textContent . includes ( text ) ;
100
-
101
- if ( isMatch ) {
102
- resolve ( elm ) ;
103
- } else {
104
- reject (
105
- `waitForText() did not find "${ text } " in CSS selector ${ cssSelector } `
106
- ) ;
107
- }
103
+ const int = setInterval ( ( ) => {
104
+ const isMatch = elm . textContent . includes ( text ) ;
105
+
106
+ if ( isMatch ) {
107
+ clearInterval ( int ) ;
108
+ resolve ( true ) ;
109
+ }
110
+
111
+ timeElapsed += settings . delay ;
112
+
113
+ if ( timeElapsed >= settings . timeout ) {
114
+ const msg = `waitForText did not find '${ text } ' in CSS selector '${ cssSelector } ' (${ settings . timeout } ms): '${ elm . textContent } '` ;
115
+
116
+ reject ( msg ) ;
117
+ }
118
+ } , settings . delay ) ;
108
119
} )
109
120
. catch ( ( ) => {
110
- reject ( `waitForText() was unable to find CSS selector ${ cssSelector } ` ) ;
121
+ const msg = `waitForText did not match CSS selector '${ cssSelector } ' (${ settings . timeout } ms)` ;
122
+
123
+ reject ( msg ) ;
111
124
} ) ;
112
125
} ) ;
113
126
}
0 commit comments