PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Component/Helper/Wait.php

https://github.com/andreaswolf/Menta
PHP | 217 lines | 77 code | 24 blank | 116 comment | 1 complexity | ec9c7c95de61073e9b89264c043abb2b MD5 | raw file
  1. <?php
  2. /**
  3. * Wait
  4. *
  5. * All wait* methods in this class wait a given time for a condition to be met.
  6. * If the condition was met before waiting times out true will be return, otherwise false
  7. *
  8. * @author Fabrizio Branca
  9. * @since 2011-11-18
  10. */
  11. class Menta_Component_Helper_Wait extends Menta_Component_Abstract {
  12. /**
  13. * @var int default timeout
  14. */
  15. protected $defaultTimeout = 30;
  16. /**
  17. * @var int default sleep value;
  18. */
  19. protected $defaultSleep = 1;
  20. /**
  21. * Set default timeout
  22. *
  23. * @param $defaultTimeout
  24. * @return Menta_Component_Helper_Wait
  25. */
  26. public function setDefaultTimeout($defaultTimeout) {
  27. $this->defaultTimeout = intval($defaultTimeout);
  28. return $this;
  29. }
  30. /**
  31. * Set default timeout
  32. *
  33. * @param $defaultSleep
  34. * @return Menta_Component_Helper_Wait
  35. */
  36. public function setDefaultSleep($defaultSleep) {
  37. $this->defaultSleep = intval($defaultSleep);
  38. return $this;
  39. }
  40. /**
  41. * Waiting a given time for a callback to return true
  42. *
  43. * @static
  44. * @param $callback
  45. * @param int $timeout
  46. * @param int $sleep
  47. * @return bool|mixed
  48. */
  49. public function wait($callback, $timeout=null, $sleep=null) {
  50. $timeout = is_null($timeout) ? $this->defaultTimeout : $timeout;
  51. $sleep = is_null($sleep) ? $this->defaultSleep : $sleep;
  52. do {
  53. $result = call_user_func($callback);
  54. if ($result) {
  55. return $result;
  56. }
  57. sleep($sleep);
  58. $timeout -= $sleep;
  59. } while($timeout > 0);
  60. return false;
  61. }
  62. /**
  63. * Wait for element present
  64. *
  65. * @param string $locator
  66. * @param integer $timeout
  67. * @param int $sleep
  68. * @return bool
  69. */
  70. public function waitForElementPresent($locator, $timeout=null, $sleep=null) {
  71. $parent = $this;
  72. return $this->wait(function() use ($locator, $parent) {
  73. return $parent->getHelperCommon()->isElementPresent($locator); /* @var $parent Menta_Component_Helper_Wait */
  74. }, $timeout, $sleep);
  75. }
  76. /**
  77. * Wait for element not present
  78. *
  79. * @param string|array|WebDriver_Element $locator
  80. * @param int $timeout
  81. * @param int $sleep
  82. * @return bool
  83. */
  84. public function waitForElementNotPresent($locator, $timeout=null, $sleep=null) {
  85. $parent = $this;
  86. return $this->wait(function() use ($locator, $parent) {
  87. return !$parent->getHelperCommon()->isElementPresent($locator); /* @var $parent Menta_Component_Helper_Wait */
  88. }, $timeout, $sleep);
  89. }
  90. /**
  91. * Wait for text present
  92. *
  93. * @param string|array|WebDriver_Element $text
  94. * @param int $timeout
  95. * @param int $sleep
  96. * @return bool
  97. */
  98. public function waitForTextPresent($text, $timeout=null, $sleep=null) {
  99. $parent = $this;
  100. return $this->wait(function() use ($text, $parent) {
  101. return $parent->getHelperCommon()->isTextPresent($text); /* @var $parent Menta_Component_Helper_Wait */
  102. }, $timeout, $sleep);
  103. }
  104. /**
  105. * Wait for text not present
  106. *
  107. * @param string|array|WebDriver_Element $text
  108. * @param int $timeout
  109. * @param int $sleep
  110. * @return bool
  111. */
  112. public function waitForTextNotPresent($text, $timeout=null, $sleep=null) {
  113. $parent = $this;
  114. return $this->wait(function() use ($text, $parent) {
  115. return !$parent->getHelperCommon()->isTextPresent($text); /* @var $parent Menta_Component_Helper_Wait */
  116. }, $timeout, $sleep);
  117. }
  118. /**
  119. * Wait for condition (js snippet)
  120. *
  121. * @param string|array|WebDriver_Element $jsSnippet
  122. * @param int $timeout
  123. * @param int $sleep
  124. * @return bool
  125. */
  126. public function waitForCondition($jsSnippet, $timeout=NULL, $sleep=null) {
  127. $parent = $this;
  128. return $this->wait(function() use ($jsSnippet, $parent) {
  129. return $parent->getHelperCommon()->getEval($jsSnippet); /* @var $parent Menta_Component_Helper_Wait */
  130. }, $timeout, $sleep);
  131. }
  132. /**
  133. * Wait for element visible
  134. *
  135. * @param string $locator
  136. * @param integer $timeout
  137. * @param int $sleep
  138. * @return bool
  139. */
  140. public function waitForElementVisible($locator, $timeout=null, $sleep=null) {
  141. $parent = $this;
  142. return $this->wait(function() use ($locator, $parent) {
  143. return $parent->getHelperCommon()->isVisible($locator); /* @var $parent Menta_Component_Helper_Wait */
  144. }, $timeout, $sleep);
  145. }
  146. /**
  147. * Wait for element not visible
  148. *
  149. * @param string|array|WebDriver_Element $locator
  150. * @param int $timeout
  151. * @param int $sleep
  152. * @return bool
  153. */
  154. public function waitForElementNotVisible($locator, $timeout=null, $sleep=null) {
  155. $parent = $this;
  156. return $this->wait(function() use ($locator, $parent) {
  157. return !$parent->getHelperCommon()->isVisible($locator); /* @var $parent Menta_Component_Helper_Wait */
  158. }, $timeout, $sleep);
  159. }
  160. /**
  161. * Wait for element visible
  162. *
  163. * @deprecated
  164. * @param string $locator
  165. * @param integer $timeout
  166. * @param int $sleep
  167. * @return bool
  168. */
  169. public function waitForVisible($locator, $timeout=null, $sleep=null) {
  170. return $this->waitForElementVisible($locator, $timeout, $sleep);
  171. }
  172. /**
  173. * Wait for element not visible
  174. *
  175. * @deprecated
  176. * @param string|array|WebDriver_Element $locator
  177. * @param int $timeout
  178. * @param int $sleep
  179. * @return bool
  180. */
  181. public function waitForNotVisible($locator, $timeout=null, $sleep=null) {
  182. return $this->waitForElementNotVisible($locator, $timeout, $sleep);
  183. }
  184. /**
  185. * Get common helper
  186. * (Needs to be public because it is called inside closures)
  187. *
  188. * @return Menta_Component_Helper_Common
  189. */
  190. public function getHelperCommon() {
  191. return Menta_ComponentManager::get('Menta_Component_Helper_Common');
  192. }
  193. }