Chris@0: Chris@0: */ Chris@0: Chris@0: namespace WebDriver; Chris@0: Chris@0: use WebDriver\Exception as WebDriverException; Chris@0: Chris@0: /** Chris@0: * WebDriver\Timeouts class Chris@0: * Chris@0: * @package WebDriver Chris@0: * Chris@0: * @method void async_script($json) Set the amount of time, in milliseconds, that asynchronous scripts (executed by execute_async) are permitted to run before they are aborted and a timeout error is returned to the client. Chris@0: * @method void implicit_wait($json) Set the amount of time the driver should wait when searching for elements. Chris@0: */ Chris@0: final class Timeouts extends AbstractWebDriver Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function methods() Chris@0: { Chris@0: return array( Chris@0: 'async_script' => array('POST'), Chris@0: 'implicit_wait' => array('POST'), Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * helper method to wait until user-defined condition is met Chris@0: * Chris@0: * @param function $callback callback which returns non-false result if wait condition was met Chris@0: * @param integer $maxIterations maximum number of iterations Chris@0: * @param integer $sleep sleep duration in seconds between iterations Chris@0: * @param array $args optional args; if the callback needs $this, then pass it here Chris@0: * Chris@0: * @return mixed result from callback function Chris@0: * Chris@0: * @throws \Exception if thrown by callback, or \WebDriver\Exception\Timeout if helper times out Chris@0: */ Chris@0: public function wait($callback, $maxIterations = 1, $sleep = 0, $args = array()) Chris@0: { Chris@0: $i = max(1, $maxIterations); Chris@0: Chris@0: while ($i-- > 0) { Chris@0: $result = call_user_func_array($callback, $args); Chris@0: Chris@0: if ($result !== false) { Chris@0: return $result; Chris@0: } Chris@0: Chris@0: // don't sleep on the last iteration Chris@0: $i && sleep($sleep); Chris@0: } Chris@0: Chris@0: throw WebDriverException::factory(WebDriverException::TIMEOUT, 'wait() method timed out'); Chris@0: } Chris@0: }