@@ -20,11 +20,21 @@ class RemoteWebDriver implements WebDriver {
20
20
protected $ keyboard ;
21
21
protected $ touch ;
22
22
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 (
24
34
$ url = 'http://localhost:4444/wd/hub ' ,
25
35
$ desired_capabilities = array (),
26
- $ timeout_in_ms = 300000 ) {
27
-
36
+ $ timeout_in_ms = 300000
37
+ ) {
28
38
$ url = preg_replace ('#/+$# ' , '' , $ url );
29
39
$ command = array (
30
40
'url ' => $ url ,
@@ -38,10 +48,32 @@ public function __construct(
38
48
)
39
49
);
40
50
41
- $ this ->executor = new HttpCommandExecutor (
51
+ $ driver = new RemoteWebDriver ();
52
+ $ executor = new HttpCommandExecutor (
42
53
$ url ,
43
54
$ response ['sessionId ' ]
44
55
);
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 ;
45
77
}
46
78
47
79
/**
@@ -320,4 +352,24 @@ public function getActiveElement() {
320
352
private function newElement ($ id ) {
321
353
return new RemoteWebElement ($ this ->executor , $ id );
322
354
}
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
+ }
323
375
}
0 commit comments