* Simulate the WebDriver polling functionality to get the latest value * and mutate it with any `then` blocks that have been chained to the * ControllerPromise. * See ../../build-system/tasks/e2e/expect.js for real usage * @param {function(): function():(!Promise |T)}
(valueFunctionGetter)
| 253 | * @template T |
| 254 | */ |
| 255 | function getWaitFunction(valueFunctionGetter) { |
| 256 | return (conditionFn, opt_mutate) => { |
| 257 | /** |
| 258 | * Each call to `waitForValue` gets its own value function thunk. |
| 259 | * This simulates the value returned by a WebDriver framework for |
| 260 | * a request for a value e.g. from the DOM. |
| 261 | * See {@link ../../build-system/tasks/e2e/selenium-webdriver-controller.js#getElementText} |
| 262 | */ |
| 263 | const valueFunction = valueFunctionGetter(); |
| 264 | |
| 265 | opt_mutate = opt_mutate || ((x) => x); |
| 266 | return new Promise((resolve, reject) => { |
| 267 | /** |
| 268 | * Poll for the new value. |
| 269 | * See {@link ../../build-system/tasks/e2e/selenium-webdriver-controller.js#getWaitFn_} |
| 270 | */ |
| 271 | const id = setInterval(async () => { |
| 272 | let value; |
| 273 | try { |
| 274 | value = await opt_mutate(await valueFunction()); |
| 275 | } catch (e) { |
| 276 | clearInterval(id); |
| 277 | reject(e); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * This resolves the promise that the Chai wrapper `expect.js` awaits. |
| 283 | * The condition is passed in by the expectations and it |
| 284 | * stops polling when the condition matches. |
| 285 | * See {@link ../../build-system/tasks/e2e/expect.js#valueSatisfiesExpectation} |
| 286 | */ |
| 287 | if (conditionFn(value)) { |
| 288 | clearInterval(id); |
| 289 | resolve(value); |
| 290 | } |
| 291 | }, 4); |
| 292 | }); |
| 293 | }; |
| 294 | } |
| 295 | }); |
| 296 | }); |
no test coverage detected