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