* 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)} * @template T
(valueFunctionGetter)
| 1604 | * @template T |
| 1605 | */ |
| 1606 | function getWaitFunction(valueFunctionGetter) { |
| 1607 | return (conditionFn, opt_mutate) => { |
| 1608 | /** |
| 1609 | * Each call to `waitForValue` gets its own value function thunk. |
| 1610 | * This simulates the value returned by a WebDriver framework for |
| 1611 | * a request for a value e.g. from the DOM. |
| 1612 | * See {@link ../../build-system/tasks/e2e/selenium-webdriver-controller.js#getElementText} |
| 1613 | */ |
| 1614 | const valueFunction = valueFunctionGetter(); |
| 1615 | |
| 1616 | opt_mutate = opt_mutate || ((x) => x); |
| 1617 | return new Promise((resolve, reject) => { |
| 1618 | /** |
| 1619 | * Poll for the new value. |
| 1620 | * See {@link ../../build-system/tasks/e2e/selenium-webdriver-controller.js#getWaitFn_} |
| 1621 | */ |
| 1622 | const id = setInterval(async () => { |
| 1623 | let value; |
| 1624 | try { |
| 1625 | value = await opt_mutate(await valueFunction()); |
| 1626 | } catch (e) { |
| 1627 | clearInterval(id); |
| 1628 | reject(e); |
| 1629 | return; |
| 1630 | } |
| 1631 | |
| 1632 | /** |
| 1633 | * This resolves the promise that the Chai wrapper `expect.js` awaits. |
| 1634 | * The condition is passed in by the expectations and it |
| 1635 | * stops polling when the condition matches. |
| 1636 | * See {@link ../../build-system/tasks/e2e/expect.js#valueSatisfiesExpectation} |
| 1637 | */ |
| 1638 | if (conditionFn(value)) { |
| 1639 | clearInterval(id); |
| 1640 | resolve(value); |
| 1641 | } |
| 1642 | }, 4); |
| 1643 | }); |
| 1644 | }; |
| 1645 | } |
no test coverage detected