13
13
// See the License for the specific language governing permissions and
14
14
// limitations under the License.
15
15
16
- class WebDriver {
17
-
18
- protected $ executor ;
19
-
20
- public function __construct (
21
- $ url = 'http://localhost:4444/wd/hub ' ,
22
- $ desired_capabilities = array ()) {
23
- $ url = preg_replace ('#/+$# ' , '' , $ url );
24
-
25
- $ command = array (
26
- 'url ' => $ url ,
27
- 'name ' => 'newSession ' ,
28
- 'parameters ' => array ('desiredCapabilities ' => $ desired_capabilities ),
29
- );
30
- $ response = WebDriverCommandExecutor::remoteExecute ($ command );
31
-
32
- $ this ->executor = new WebDriverCommandExecutor (
33
- $ url ,
34
- $ response ['sessionId ' ]
35
- );
36
- }
16
+ /**
17
+ * The interface for WebDriver.
18
+ */
19
+ interface WebDriver {
37
20
38
21
/**
39
22
* Close the current window.
40
23
*
41
24
* @return WebDriver The current instance.
42
25
*/
43
- public function close () {
44
- $ this ->executor ->execute ('closeCurrentWindow ' , array ());
45
-
46
- return $ this ;
47
- }
26
+ public function close ();
48
27
49
28
/**
50
29
* Find the first WebDriverElement using the given mechanism.
@@ -54,12 +33,7 @@ public function close() {
54
33
* WebDriverCommandExecutor if no element is found.
55
34
* @see WebDriverBy
56
35
*/
57
- public function findElement (WebDriverBy $ by ) {
58
- $ params = array ('using ' => $ by ->getMechanism (), 'value ' => $ by ->getValue ());
59
- $ raw_element = $ this ->executor ->execute ('findElement ' , $ params );
60
-
61
- return $ this ->newElement ($ raw_element ['ELEMENT ' ]);
62
- }
36
+ public function findElement (WebDriverBy $ by );
63
37
64
38
/**
65
39
* Find all WebDriverElements within the current page using the given
@@ -70,84 +44,57 @@ public function findElement(WebDriverBy $by) {
70
44
* nothing matches
71
45
* @see WebDriverBy
72
46
*/
73
- public function findElements (WebDriverBy $ by ) {
74
- $ params = array ('using ' => $ by ->getMechanism (), 'value ' => $ by ->getValue ());
75
- $ raw_elements = $ this ->executor ->execute ('findElements ' , $ params );
76
-
77
- $ elements = array ();
78
- foreach ($ raw_elements as $ raw_element ) {
79
- $ elements [] = $ this ->newElement ($ raw_element ['ELEMENT ' ]);
80
- }
81
- return $ elements ;
82
- }
47
+ public function findElements (WebDriverBy $ by );
83
48
84
49
/**
85
50
* Load a new web page in the current browser window.
86
51
*
87
52
* @return WebDriver The current instance.
88
53
*/
89
- public function get ($ url ) {
90
- $ params = array ('url ' => (string )$ url );
91
- $ this ->executor ->execute ('get ' , $ params );
92
-
93
- return $ this ;
94
- }
54
+ public function get ($ url );
95
55
96
56
/**
97
57
* Get a string representing the current URL that the browser is looking at.
98
58
*
99
59
* @return string The current URL.
100
60
*/
101
- public function getCurrentURL () {
102
- return $ this ->executor ->execute ('getCurrentURL ' );
103
- }
61
+ public function getCurrentURL ();
104
62
105
63
/**
106
64
* Get the source of the last loaded page.
107
65
*
108
66
* @return string The current page source.
109
67
*/
110
- public function getPageSource () {
111
- return $ this ->executor ->execute ('getPageSource ' );
112
- }
68
+ public function getPageSource ();
113
69
114
70
/**
115
71
* Get the title of the current page.
116
72
*
117
73
* @return string The title of the current page.
118
74
*/
119
- public function getTitle () {
120
- return $ this ->executor ->execute ('getTitle ' );
121
- }
75
+ public function getTitle ();
122
76
123
77
/**
124
78
* Return an opaque handle to this window that uniquely identifies it within
125
79
* this driver instance.
126
80
*
127
81
* @return string The current window handle.
128
82
*/
129
- public function getWindowHandle () {
130
- return $ this ->executor ->execute ('getCurrentWindowHandle ' , array ());
131
- }
83
+ public function getWindowHandle ();
132
84
133
85
/**
134
86
* Get all window handles available to the current session.
135
87
*
136
88
* @return array An array of string containing all available window handles.
137
89
*/
138
- public function getWindowHandles () {
139
- return $ this ->executor ->execute ('getWindowHandles ' , array ());
140
- }
90
+ public function getWindowHandles ();
141
91
142
92
/**
143
93
* Quits this driver, closing every associated window.
144
94
*
145
95
* @return void
146
96
*/
147
- public function quit () {
148
- $ this ->executor ->execute ('quit ' );
149
- $ this ->executor = null ;
150
- }
97
+ public function quit ();
151
98
152
99
/**
153
100
* Inject a snippet of JavaScript into the page for execution in the context
@@ -158,51 +105,15 @@ public function quit() {
158
105
* @param array $arguments The arguments of the script.
159
106
* @return mixed The return value of the script.
160
107
*/
161
- public function executeScript ($ script , array $ arguments = array ()) {
162
- $ script = str_replace ('" ' , '\" ' , $ script );
163
- $ args = array ();
164
- foreach ($ arguments as $ arg ) {
165
- if ($ arg instanceof WebDriverElement) {
166
- array_push ($ args , array ('ELEMENT ' => $ arg ->getID ()));
167
- } else {
168
- // TODO: Handle the case where arg is a collection
169
- if (is_array ($ arg )) {
170
- throw new Exception (
171
- "executeScript with collection paramatar is unimplemented "
172
- );
173
- }
174
- array_push ($ args , $ arg );
175
- }
176
- }
177
-
178
- $ params = array ('script ' => $ script , 'args ' => $ args );
179
- $ response = $ this ->executor ->execute ('executeScript ' , $ params );
180
-
181
- if (is_array ($ response )) {
182
- // TODO: Handle this
183
- throw new Exception (
184
- "executeScript with collection response is unimplemented "
185
- );
186
- } else {
187
- return $ response ;
188
- }
189
- }
108
+ public function executeScript ($ script , array $ arguments = array ());
190
109
191
110
/**
192
111
* Take a screenshot of the current page.
193
112
*
194
113
* @param $save_as The path of the screenshot to be saved.
195
114
* @return string The screenshot in PNG format.
196
115
*/
197
- public function takeScreenshot ($ save_as = null ) {
198
- $ screenshot = base64_decode (
199
- $ this ->executor ->execute ('takeScreenshot ' )
200
- );
201
- if ($ save_as ) {
202
- file_put_contents ($ save_as , $ screenshot );
203
- }
204
- return $ screenshot ;
205
- }
116
+ public function takeScreenshot ($ save_as = null );
206
117
207
118
/**
208
119
* Construct a new WebDriverWait by the current WebDriver instance.
@@ -216,21 +127,15 @@ public function takeScreenshot($save_as = null) {
216
127
*/
217
128
public function wait (
218
129
$ timeout_in_second = 30 ,
219
- $ interval_in_millisecond = 250 ) {
220
- return new WebDriverWait (
221
- $ this , $ timeout_in_second , $ interval_in_millisecond
222
- );
223
- }
130
+ $ interval_in_millisecond = 250 );
224
131
225
132
/**
226
133
* An abstraction for managing stuff you would do in a browser menu. For
227
134
* example, adding and deleting cookies.
228
135
*
229
136
* @return WebDriverOptions
230
137
*/
231
- public function manage () {
232
- return new WebDriverOptions ($ this ->executor );
233
- }
138
+ public function manage ();
234
139
235
140
/**
236
141
* An abstraction allowing the driver to access the browser's history and to
@@ -239,28 +144,13 @@ public function manage() {
239
144
* @return WebDriverNavigation
240
145
* @see WebDriverNavigation
241
146
*/
242
- public function navigate () {
243
- return new WebDriverNavigation ($ this ->executor );
244
- }
147
+ public function navigate ();
245
148
246
149
/**
247
150
* Switch to a different window or frame.
248
151
*
249
152
* @return WebDriverTargetLocator
250
153
* @see WebDriverTargetLocator
251
154
*/
252
- public function switchTo () {
253
- return new WebDriverTargetLocator ($ this ->executor , $ this );
254
- }
255
-
256
- /**
257
- * Return the WebDriverElement with the given id.
258
- *
259
- * @param string $id The id of the element to be created.
260
- * @return WebDriverElement
261
- */
262
- private function newElement ($ id ) {
263
- return new WebDriverElement ($ this ->executor , $ id );
264
- }
265
-
155
+ public function switchTo ();
266
156
}
0 commit comments