WaitFor.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Helper class with common "wait for" functions.
  24. */
  25. class WaitFor {
  26. /**
  27. * Waits for the element to be visible.
  28. *
  29. * @param Actor $actor the Actor used to find the element.
  30. * @param Locator $elementLocator the locator for the element.
  31. * @param float $timeout the number of seconds (decimals allowed) to wait at
  32. * most for the element to be visible.
  33. * @param float $timeoutStep the number of seconds (decimals allowed) to
  34. * wait before checking the visibility again.
  35. * @return boolean true if the element is visible before (or exactly when)
  36. * the timeout expires, false otherwise.
  37. */
  38. public static function elementToBeEventuallyShown(Actor $actor, Locator $elementLocator, $timeout = 10, $timeoutStep = 1) {
  39. $elementShownCallback = function () use ($actor, $elementLocator) {
  40. try {
  41. return $actor->find($elementLocator)->isVisible();
  42. } catch (NoSuchElementException $exception) {
  43. return false;
  44. }
  45. };
  46. return Utils::waitFor($elementShownCallback, $timeout, $timeoutStep);
  47. }
  48. /**
  49. * Waits for the element to be hidden (either not visible or not found in
  50. * the DOM).
  51. *
  52. * @param Actor $actor the Actor used to find the element.
  53. * @param Locator $elementLocator the locator for the element.
  54. * @param float $timeout the number of seconds (decimals allowed) to wait at
  55. * most for the element to be hidden.
  56. * @param float $timeoutStep the number of seconds (decimals allowed) to
  57. * wait before checking the visibility again.
  58. * @return boolean true if the element is hidden before (or exactly when)
  59. * the timeout expires, false otherwise.
  60. */
  61. public static function elementToBeEventuallyNotShown(Actor $actor, Locator $elementLocator, $timeout = 10, $timeoutStep = 1) {
  62. $elementNotShownCallback = function () use ($actor, $elementLocator) {
  63. try {
  64. return !$actor->find($elementLocator)->isVisible();
  65. } catch (NoSuchElementException $exception) {
  66. return true;
  67. }
  68. };
  69. return Utils::waitFor($elementNotShownCallback, $timeout, $timeoutStep);
  70. }
  71. }