Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit adb651a

Browse files
author
whhone
committed
make RemoteWebDriver flexiable to construct and allow create by session id
1 parent dd30a29 commit adb651a

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

lib/remote/RemoteWebDriver.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@ class RemoteWebDriver implements WebDriver {
2020
protected $keyboard;
2121
protected $touch;
2222

23-
public function __construct(
23+
protected function __construct() {}
24+
25+
/**
26+
* Construct the RemoteWebDriver by a desired capabilities.
27+
*
28+
* @param string $url The url of the remote server
29+
* @param array $desired_capabilities The webdriver desired capabilities
30+
* @param int $timeout_in_ms
31+
* @return RemoteWebDriver
32+
*/
33+
public static function create(
2434
$url = 'http://localhost:4444/wd/hub',
2535
$desired_capabilities = array(),
26-
$timeout_in_ms = 300000) {
27-
36+
$timeout_in_ms = 300000
37+
) {
2838
$url = preg_replace('#/+$#', '', $url);
2939
$command = array(
3040
'url' => $url,
@@ -38,10 +48,32 @@ public function __construct(
3848
)
3949
);
4050

41-
$this->executor = new HttpCommandExecutor(
51+
$driver = new RemoteWebDriver();
52+
$executor = new HttpCommandExecutor(
4253
$url,
4354
$response['sessionId']
4455
);
56+
return $driver->setCommandExecutor($executor);
57+
}
58+
59+
/**
60+
* [Experimental] Construct the RemoteWebDriver by an existing session.
61+
*
62+
* This constructor can boost the performance a lot by reusing the same
63+
* browser for the whole test suite. You do not have to pass the desired
64+
* capabilities because the session was created before.
65+
*
66+
* @param string $url The url of the remote server
67+
* @param string $session_id The existing session id
68+
* @return RemoteWebDriver
69+
*/
70+
public static function createBySessionID(
71+
$session_id,
72+
$url = 'http://localhost:4444/wd/hub'
73+
) {
74+
$driver = new RemoteWebDriver();
75+
$driver->setCommandExecutor(new HttpCommandExecutor($url, $session_id));
76+
return $driver;
4577
}
4678

4779
/**
@@ -320,4 +352,24 @@ public function getActiveElement() {
320352
private function newElement($id) {
321353
return new RemoteWebElement($this->executor, $id);
322354
}
355+
356+
/**
357+
* Set the command executor of this RemoteWebdrver
358+
*
359+
* @param WebDriverCommandExecutor $executor
360+
* @return WebDriver
361+
*/
362+
public function setCommandExecutor(WebDriverCommandExecutor $executor) {
363+
$this->executor = $executor;
364+
return $this;
365+
}
366+
367+
/**
368+
* Set the command executor of this RemoteWebdriver
369+
*
370+
* @return WebDriverCommandExecutor
371+
*/
372+
public function getCommandExecutor() {
373+
return $this->executor;
374+
}
323375
}

0 commit comments

Comments
 (0)