Skip to content

Commit 05794d5

Browse files
Keep alive support for remote selenium testing
1 parent 36bf555 commit 05794d5

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

lib/remote/HttpCommandExecutor.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ class HttpCommandExecutor implements WebDriverCommandExecutor {
100100
protected $url;
101101
protected $sessionID;
102102
protected $capabilities;
103+
protected $curl;
103104

104105
public function __construct($url, $session_id) {
105106
$this->url = $url;
106107
$this->sessionID = $session_id;
107108
$this->capabilities = $this->execute('getSession', array());
109+
$this->curl = curl_init($url);
108110
}
109111

110112
public function execute($name, array $params = array()) {
@@ -114,7 +116,9 @@ public function execute($name, array $params = array()) {
114116
'name' => $name,
115117
'parameters' => $params,
116118
);
117-
$raw = self::remoteExecute($command);
119+
if(is_null($this->curl))
120+
$this->curl = curl_init($this->url);
121+
$raw = self::remoteExecute($command,$this->curl);
118122
return $raw['value'];
119123
}
120124

@@ -128,10 +132,11 @@ public function execute($name, array $params = array()) {
128132
*
129133
* @return array The response of the command.
130134
*/
131-
public static function remoteExecute($command) {
135+
public static function remoteExecute($command, $curl) {
132136
if (!isset(self::$commands[$command['name']])) {
133137
throw new Exception($command['name']." is not a valid command.");
134138
}
139+
$flag = 0;
135140
$raw = self::$commands[$command['name']];
136141
$extra_opts = array();
137142

@@ -143,10 +148,35 @@ public static function remoteExecute($command) {
143148
$raw['method'],
144149
sprintf("%s%s", $command['url'], $raw['url']),
145150
$command,
151+
$curl,
146152
$extra_opts
147153
);
148154
}
149155

156+
public static function remoteExecuteInit($command) {
157+
if (!isset(self::$commands[$command['name']])) {
158+
throw new Exception($command['name']." is not a valid command.");
159+
}
160+
$curl = curl_init();
161+
$raw = self::$commands[$command['name']];
162+
$extra_opts = array();
163+
164+
if ($command['name'] == 'newSession') {
165+
$extra_opts[CURLOPT_FOLLOWLOCATION] = true;
166+
}
167+
168+
$ret_value = self::curl(
169+
$raw['method'],
170+
sprintf("%s%s", $command['url'], $raw['url']),
171+
$command,
172+
$curl,
173+
$extra_opts
174+
);
175+
176+
curl_close($curl);
177+
return $ret_value;
178+
}
179+
150180
/**
151181
* Curl request to webdriver server.
152182
*
@@ -155,10 +185,13 @@ public static function remoteExecute($command) {
155185
* @param command The Command object, modelled as a hash.
156186
* @param extra_opts key => value pairs of curl options for curl_setopt()
157187
*/
188+
189+
158190
protected static function curl(
159191
$http_method,
160192
$url,
161193
$command,
194+
$curl,
162195
$extra_opts = array()) {
163196

164197
$params = $command['parameters'];
@@ -185,8 +218,7 @@ protected static function curl(http://github.com/%3C/div%3E%3C/code%3E%3C/div%3E%3C/td%3E%3C/tr%3E%3Ctr%20class=%22diff-line-row%22%3E%3Ctd%20data-grid-cell-id=%22diff-00b9e1a13f81274c9438ea7b8b04439deb5b9024e14273a54de0306d74d657f3-185-218-0%22%20data-selected=%22false%22%20role=%22gridcell%22%20style=%22background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">185
218
json_encode($params)));
186219
}
187220

188-
$curl = curl_init($url);
189-
221+
curl_setopt($curl, CURLOPT_URL, $url);
190222
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
191223
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
192224
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
@@ -223,8 +255,6 @@ protected static function curl(http://github.com/%3C/div%3E%3C/code%3E%3C/div%3E%3C/td%3E%3C/tr%3E%3Ctr%20class=%22diff-line-row%22%3E%3Ctd%20data-grid-cell-id=%22diff-00b9e1a13f81274c9438ea7b8b04439deb5b9024e14273a54de0306d74d657f3-223-255-0%22%20data-selected=%22false%22%20role=%22gridcell%22%20style=%22background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">223
255
}
224256
throw new WebDriverCurlException($msg . "\n\n" . $error);
225257
}
226-
curl_close($curl);
227-
228258
$results = json_decode($raw_results, true);
229259

230260
$value = null;
@@ -252,4 +282,7 @@ public function getSessionID() {
252282
return $this->sessionID;
253283
}
254284

285+
public function close(){
286+
curl_close($this->curl);
287+
}
255288
}

lib/remote/RemoteWebDriver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
'name' => 'newSession',
3131
'parameters' => array('desiredCapabilities' => $desired_capabilities),
3232
);
33-
$response = HttpCommandExecutor::remoteExecute($command);
33+
$response = HttpCommandExecutor::remoteExecuteInit($command);
3434

3535
$this->executor = new HttpCommandExecutor(
3636
$url,
@@ -45,7 +45,7 @@ public function __construct(
4545
*/
4646
public function close() {
4747
$this->executor->execute('closeCurrentWindow', array());
48-
48+
$this->executor->close();
4949
return $this;
5050
}
5151

@@ -314,4 +314,4 @@ public function getActiveElement() {
314314
private function newElement($id) {
315315
return new RemoteWebElement($this->executor, $id);
316316
}
317-
}
317+
}

0 commit comments

Comments
 (0)