1
+ <?php
2
+
3
+ class EventFiringWebDriver {
4
+
5
+ /**
6
+ * @var WebDriver
7
+ */
8
+ protected $ _webdriver ;
9
+
10
+ /**
11
+ * @var WebDriverDispatcher
12
+ */
13
+ protected $ _dispatcher ;
14
+
15
+ /**
16
+ * @param WebDriver $webdriver
17
+ * @param WebDriverDispatcher $dispatcher
18
+ */
19
+ public function __construct (WebDriver $ webdriver , WebDriverDispatcher $ dispatcher = null ) {
20
+
21
+ $ this ->_dispatcher = $ dispatcher ? $ dispatcher : new WebDriverDispatcher ;
22
+ if (!$ this ->_dispatcher ->getDefaultDriver ())
23
+ $ this ->_dispatcher ->setDefaultDriver ($ this );
24
+
25
+ $ this ->_webdriver = $ webdriver ;
26
+
27
+ return $ this ;
28
+
29
+ }
30
+
31
+ /**
32
+ * @return WebDriverDispatcher
33
+ */
34
+ public function getDispatcher () {
35
+ return $ this ->_dispatcher ;
36
+ }
37
+
38
+ /**
39
+ * @param $method
40
+ */
41
+ protected function _dispatch ($ method ) {
42
+
43
+ if (!$ this ->_dispatcher )
44
+ return ;
45
+
46
+ $ arguments = func_get_args ();
47
+ unset($ arguments [0 ]);
48
+ $ this ->_dispatcher ->dispatch ($ method , $ arguments );
49
+
50
+ }
51
+
52
+ /**
53
+ * @return WebDriver
54
+ */
55
+ public function getWebDriver () {
56
+ return $ this ->_webdriver ;
57
+ }
58
+
59
+ /**
60
+ * @param WebDriverElement $element
61
+ * @return EventFiringWebElement
62
+ */
63
+ private function newElement (WebDriverElement $ element ) {
64
+ return new EventFiringWebElement ($ element , $ this ->getDispatcher ());
65
+ }
66
+
67
+ /**
68
+ * @param $url
69
+ * @return $this
70
+ * @throws WebDriverException
71
+ */
72
+ public function get ($ url ) {
73
+
74
+ $ this ->_dispatch ('beforeNavigateTo ' , $ url , $ this );
75
+ try {
76
+ $ this ->_webdriver ->get ($ url );
77
+ } catch (WebDriverException $ exception ) {
78
+
79
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
80
+ throw $ exception ;
81
+
82
+ }
83
+ $ this ->_dispatch ('afterNavigateTo ' , $ url , $ this );
84
+
85
+ return $ this ;
86
+
87
+ }
88
+
89
+ /**
90
+ * @param WebDriverBy $by
91
+ * @return array
92
+ * @throws WebDriverException
93
+ */
94
+ public function findElements (WebDriverBy $ by ) {
95
+
96
+ $ this ->_dispatch ('beforeFindBy ' , $ by , null , $ this );
97
+
98
+ try {
99
+
100
+ $ elements = array ();
101
+ foreach ($ this ->_webdriver ->findElements ($ by ) as $ element )
102
+ $ elements [] = $ this ->newElement ($ element );
103
+
104
+ } catch (WebDriverException $ exception ) {
105
+
106
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
107
+ throw $ exception ;
108
+
109
+ }
110
+
111
+ $ this ->_dispatch ('afterFindBy ' , $ by , null , $ this );
112
+
113
+ return $ elements ;
114
+
115
+ }
116
+
117
+ /**
118
+ * @param WebDriverBy $by
119
+ * @return EventFiringWebElement
120
+ * @throws WebDriverException
121
+ */
122
+ public function findElement (WebDriverBy $ by ) {
123
+
124
+ $ this ->_dispatch ('beforeFindBy ' , $ by , null , $ this );
125
+ try {
126
+ $ element = $ this ->newElement ($ this ->_webdriver ->findElement ($ by ));
127
+ } catch (WebDriverException $ exception ) {
128
+
129
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
130
+ throw $ exception ;
131
+
132
+ }
133
+ $ this ->_dispatch ('afterFindBy ' , $ by , null , $ this );
134
+
135
+ return $ element ;
136
+
137
+ }
138
+
139
+ /**
140
+ * @param $script
141
+ * @param array $arguments
142
+ * @return mixed
143
+ * @throws WebDriverException
144
+ */
145
+ public function executeScript ($ script , array $ arguments = array ()) {
146
+
147
+ $ this ->_dispatch ('beforeScript ' , $ script , $ this );
148
+ try {
149
+ $ result = $ this ->_webdriver ->executeScript ($ script , $ arguments );
150
+ } catch (WebDriverException $ exception ) {
151
+
152
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
153
+ throw $ exception ;
154
+
155
+ }
156
+ $ this ->_dispatch ('afterScript ' , $ script , $ this );
157
+
158
+ return $ result ;
159
+
160
+ }
161
+
162
+ /**
163
+ * @return $this
164
+ * @throws WebDriverException
165
+ */
166
+ public function close () {
167
+ try {
168
+ $ this ->_webdriver ->close ();
169
+ return $ this ;
170
+ } catch (WebDriverException $ exception ) {
171
+
172
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
173
+ throw $ exception ;
174
+
175
+ }
176
+ }
177
+
178
+ /**
179
+ * @return string
180
+ * @throws WebDriverException
181
+ */
182
+ public function getCurrentURL () {
183
+ try {
184
+ return $ this ->_webdriver ->getCurrentURL ();
185
+ } catch (WebDriverException $ exception ) {
186
+
187
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
188
+ throw $ exception ;
189
+
190
+ }
191
+ }
192
+
193
+ /**
194
+ * @return string
195
+ * @throws WebDriverException
196
+ */
197
+ public function getPageSource () {
198
+ try {
199
+ return $ this ->_webdriver ->getPageSource ();
200
+ } catch (WebDriverException $ exception ) {
201
+
202
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
203
+ throw $ exception ;
204
+
205
+ }
206
+ }
207
+
208
+ /**
209
+ * @return string
210
+ * @throws WebDriverException
211
+ */
212
+ public function getTitle () {
213
+ try {
214
+ return $ this ->_webdriver ->getTitle ();
215
+ } catch (WebDriverException $ exception ) {
216
+
217
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
218
+ throw $ exception ;
219
+
220
+ }
221
+ }
222
+
223
+ /**
224
+ * @return string
225
+ * @throws WebDriverException
226
+ */
227
+ public function getWindowHandle () {
228
+ try {
229
+ return $ this ->_webdriver ->getWindowHandle ();
230
+ } catch (WebDriverException $ exception ) {
231
+
232
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
233
+ throw $ exception ;
234
+
235
+ }
236
+ }
237
+
238
+ /**
239
+ * @return array
240
+ * @throws WebDriverException
241
+ */
242
+ public function getWindowHandles () {
243
+ try {
244
+ return $ this ->_webdriver ->getWindowHandles ();
245
+ } catch (WebDriverException $ exception ) {
246
+
247
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
248
+ throw $ exception ;
249
+
250
+ }
251
+ }
252
+
253
+ /**
254
+ * @throws WebDriverException
255
+ */
256
+ public function quit () {
257
+ try {
258
+ $ this ->_webdriver ->quit ();
259
+ } catch (WebDriverException $ exception ) {
260
+
261
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
262
+ throw $ exception ;
263
+
264
+ }
265
+ }
266
+
267
+ /**
268
+ * @param null $save_as
269
+ * @return string
270
+ * @throws WebDriverException
271
+ */
272
+ public function takeScreenshot ($ save_as = null ) {
273
+ try {
274
+ return $ this ->_webdriver ->takeScreenshot ($ save_as );
275
+ } catch (WebDriverException $ exception ) {
276
+
277
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
278
+ throw $ exception ;
279
+
280
+ }
281
+ }
282
+
283
+ /**
284
+ * @param int $timeout_in_second
285
+ * @param int $interval_in_millisecond
286
+ * @return WebDriverWait
287
+ * @throws WebDriverException
288
+ */
289
+ public function wait ($ timeout_in_second = 30 , $ interval_in_millisecond = 250 ) {
290
+ try {
291
+ return $ this ->_webdriver ->wait ($ timeout_in_second , $ interval_in_millisecond );
292
+ } catch (WebDriverException $ exception ) {
293
+
294
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
295
+ throw $ exception ;
296
+
297
+ }
298
+ }
299
+
300
+ /**
301
+ * @return WebDriverOptions
302
+ * @throws WebDriverException
303
+ */
304
+ public function manage () {
305
+ try {
306
+ return $ this ->_webdriver ->manage ();
307
+ } catch (WebDriverException $ exception ) {
308
+
309
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
310
+ throw $ exception ;
311
+
312
+ }
313
+ }
314
+
315
+ /**
316
+ * @return EventFiringWebDriverNavigation
317
+ * @throws WebDriverException
318
+ */
319
+ public function navigate () {
320
+ try {
321
+ return new EventFiringWebDriverNavigation ($ this ->_webdriver ->navigate (), $ this ->getDispatcher ());
322
+ } catch (WebDriverException $ exception ) {
323
+
324
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
325
+ throw $ exception ;
326
+
327
+ }
328
+ }
329
+
330
+ /**
331
+ * @return WebDriverTargetLocator
332
+ * @throws WebDriverException
333
+ */
334
+ public function switchTo () {
335
+ try {
336
+ return $ this ->_webdriver ->switchTo ();
337
+ } catch (WebDriverException $ exception ) {
338
+
339
+ $ this ->_dispatch ('onException ' , $ exception , $ this );
340
+ throw $ exception ;
341
+
342
+ }
343
+ }
344
+
345
+ }
0 commit comments