annotate vendor/instaclick/php-webdriver/lib/WebDriver/Timeouts.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Copyright 2011-2017 Anthon Pang. All Rights Reserved.
Chris@0 4 *
Chris@0 5 * Licensed under the Apache License, Version 2.0 (the "License");
Chris@0 6 * you may not use this file except in compliance with the License.
Chris@0 7 * You may obtain a copy of the License at
Chris@0 8 *
Chris@0 9 * http://www.apache.org/licenses/LICENSE-2.0
Chris@0 10 *
Chris@0 11 * Unless required by applicable law or agreed to in writing, software
Chris@0 12 * distributed under the License is distributed on an "AS IS" BASIS,
Chris@0 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Chris@0 14 * See the License for the specific language governing permissions and
Chris@0 15 * limitations under the License.
Chris@0 16 *
Chris@0 17 * @package WebDriver
Chris@0 18 *
Chris@0 19 * @author Anthon Pang <apang@softwaredevelopment.ca>
Chris@0 20 */
Chris@0 21
Chris@0 22 namespace WebDriver;
Chris@0 23
Chris@0 24 use WebDriver\Exception as WebDriverException;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * WebDriver\Timeouts class
Chris@0 28 *
Chris@0 29 * @package WebDriver
Chris@0 30 *
Chris@0 31 * @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 32 * @method void implicit_wait($json) Set the amount of time the driver should wait when searching for elements.
Chris@0 33 */
Chris@0 34 final class Timeouts extends AbstractWebDriver
Chris@0 35 {
Chris@0 36 /**
Chris@0 37 * {@inheritdoc}
Chris@0 38 */
Chris@0 39 protected function methods()
Chris@0 40 {
Chris@0 41 return array(
Chris@0 42 'async_script' => array('POST'),
Chris@0 43 'implicit_wait' => array('POST'),
Chris@0 44 );
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * helper method to wait until user-defined condition is met
Chris@0 49 *
Chris@0 50 * @param function $callback callback which returns non-false result if wait condition was met
Chris@0 51 * @param integer $maxIterations maximum number of iterations
Chris@0 52 * @param integer $sleep sleep duration in seconds between iterations
Chris@0 53 * @param array $args optional args; if the callback needs $this, then pass it here
Chris@0 54 *
Chris@0 55 * @return mixed result from callback function
Chris@0 56 *
Chris@0 57 * @throws \Exception if thrown by callback, or \WebDriver\Exception\Timeout if helper times out
Chris@0 58 */
Chris@0 59 public function wait($callback, $maxIterations = 1, $sleep = 0, $args = array())
Chris@0 60 {
Chris@0 61 $i = max(1, $maxIterations);
Chris@0 62
Chris@0 63 while ($i-- > 0) {
Chris@0 64 $result = call_user_func_array($callback, $args);
Chris@0 65
Chris@0 66 if ($result !== false) {
Chris@0 67 return $result;
Chris@0 68 }
Chris@0 69
Chris@0 70 // don't sleep on the last iteration
Chris@0 71 $i && sleep($sleep);
Chris@0 72 }
Chris@0 73
Chris@0 74 throw WebDriverException::factory(WebDriverException::TIMEOUT, 'wait() method timed out');
Chris@0 75 }
Chris@0 76 }