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