Skip to content

Commit f85d466

Browse files
committed
add Timeout helpers
1 parent a801a0b commit f85d466

File tree

1 file changed

+76
-11
lines changed

1 file changed

+76
-11
lines changed

lib/WebDriver/Timeouts.php

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @copyright 2011 Anthon Pang
55
* @license Apache-2.0
66
*
7-
* @package WebDriver
8-
*
97
* @author Anthon Pang <apang@softwaredevelopment.ca>
108
*/
119

@@ -16,23 +14,90 @@
1614
/**
1715
* WebDriver\Timeouts class
1816
*
19-
* @package WebDriver
20-
*
21-
* @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.
22-
* @method void implicit_wait($json) Set the amount of time the driver should wait when searching for elements.
17+
* @method void async_script($parameters) 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.
18+
* @method void implicit_wait($parameters) Set the amount of time the driver should wait when searching for elements.
2319
*/
2420
class Timeouts extends AbstractWebDriver
2521
{
22+
// Timeout name constants
23+
const IMPLICIT = 'implicit';
24+
const PAGE_LOAD = 'pageLoad';
25+
const SCRPT = 'script';
26+
2627
/**
2728
* {@inheritdoc}
2829
*/
2930
protected function methods()
3031
{
31-
return array(
32+
return [
3233
// Legacy JSON Wire Protocol
33-
'async_script' => array('POST'),
34-
'implicit_wait' => array('POST'),
35-
);
34+
'async_script' => ['POST'],
35+
'implicit_wait' => ['POST'],
36+
];
37+
}
38+
39+
/**
40+
* Implicitly wait: /session/:sessionId/timeout/implicit_wait (POST)
41+
*
42+
* @deprecated
43+
*
44+
* @param array|integer $ms Parameters {ms: ...}
45+
*
46+
* @return \WebDriver\Timeouts
47+
*/
48+
public function implicitlyWait($ms)
49+
{
50+
$parameters = is_array($ms)
51+
? $ms
52+
: ['ms' => $ms];
53+
54+
// trigger_error(__METHOD__ . ': use "setTimeout()" instead', E_USER_DEPRECATED);
55+
56+
$result = $this->curl('POST', '/implicit_wait', $parameters);
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* Set script timeout: /session/:sessionId/timeout/async_script (POST)
63+
*
64+
* @deprecated
65+
*
66+
* @param array|integer $ms Parameters {ms: ...}
67+
*
68+
* @return \WebDriver\Timeouts
69+
*/
70+
public function setScriptTimeout($ms)
71+
{
72+
$parameters = is_array($ms)
73+
? $ms
74+
: ['ms' => $ms];
75+
76+
// trigger_error(__METHOD__ . ': use "setTimeout()" instead', E_USER_DEPRECATED);
77+
78+
$result = $this->curl('POST', '/async_script', $parameters);
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* Set timeout: /session/:sessionId/timeouts (POST)
85+
*
86+
* @param string|array $type Timeout name (see constants above)
87+
* @param integer $timeout Duration in milliseconds
88+
*
89+
* @return \WebDriver\Timeouts
90+
*/
91+
public function setTimeout($type, $timeout = null)
92+
{
93+
// set timeouts
94+
$parameters = func_num_args() === 1 && is_array($type)
95+
? $type
96+
: [$type => $timeout];
97+
98+
$this->curl('POST', '', $parameters);
99+
100+
return $this;
36101
}
37102

38103
/**
@@ -47,7 +112,7 @@ protected function methods()
47112
*
48113
* @throws \Exception if thrown by callback, or \WebDriver\Exception\Timeout if helper times out
49114
*/
50-
public function wait($callback, $maxIterations = 1, $sleep = 0, $args = array())
115+
public function wait($callback, $maxIterations = 1, $sleep = 0, $args = [])
51116
{
52117
$i = max(1, $maxIterations);
53118

0 commit comments

Comments
 (0)