Skip to content

Commit 5355783

Browse files
committed
Implementing EventFiringWebDriver and EventFiringWebElement.
1 parent becb8c1 commit 5355783

File tree

6 files changed

+681
-0
lines changed

6 files changed

+681
-0
lines changed

lib/EventFiringWebDriver.php

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
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+
public function __call($method, array $arguments = []) {
32+
33+
try {
34+
35+
return call_user_func_array([$this, $method], $arguments);
36+
37+
} catch (WebDriverException $exception) {
38+
39+
$this->_dispatch('onException', $exception, $this);
40+
throw $exception;
41+
42+
}
43+
44+
}
45+
46+
/**
47+
* @return WebDriverDispatcher
48+
*/
49+
public function getDispatcher() {
50+
return $this->_dispatcher;
51+
}
52+
53+
/**
54+
* @return WebDriver
55+
*/
56+
public function getWebDriver() {
57+
return $this->_webdriver;
58+
}
59+
60+
/**
61+
* @param $method
62+
*/
63+
protected function _dispatch($method) {
64+
65+
$arguments = func_get_args();
66+
unset($arguments[0]);
67+
$arguments[] = $this;
68+
69+
$this->getDispatcher()->dispatch($method, $arguments);
70+
71+
}
72+
73+
/**
74+
* Return the EventFiringWebElement with the given id.
75+
*
76+
* @param string $id The id of the element to be created.
77+
* @return EventFiringWebElement
78+
*/
79+
protected function newElement($id) {
80+
return new EventFiringWebElement($this->_webdriver->getExecutor(), $id, $this->getDispatcher());
81+
}
82+
83+
/**
84+
* @param string $url
85+
* @return EventFiringWebDriver $this
86+
*/
87+
protected function get($url) {
88+
89+
$this->_dispatch('beforeNavigateTo', $url);
90+
$this->_webdriver->get($url);
91+
$this->_dispatch('afterNavigateTo', $url);
92+
93+
return $this;
94+
95+
}
96+
97+
/**
98+
* @param WebDriverBy $by
99+
* @return array A list of EventFiringWebDriver, or empty
100+
*/
101+
protected function findElements(WebDriverBy $by) {
102+
103+
$this->_dispatch('beforeFindBy', $by);
104+
105+
$raw_elements = $this->_webdriver->getExecutor()->execute('findElements', [
106+
'using' => $by->getMechanism(),
107+
'value' => $by->getValue()
108+
]);
109+
110+
$elements = [];
111+
foreach ($raw_elements as $raw_element)
112+
$elements[] = $this->newElement($raw_element['ELEMENT']);
113+
114+
$this->_dispatch('afterFindBy', $by, $elements);
115+
116+
return $elements;
117+
118+
}
119+
120+
/**
121+
* @param WebDriverBy $by
122+
* @return EventFiringWebDriver
123+
*/
124+
protected function findElement(WebDriverBy $by) {
125+
126+
$this->_dispatch('beforeFindBy', $by);
127+
128+
$raw_element = $this->_webdriver->getExecutor()->execute('findElement', [
129+
'using' => $by->getMechanism(),
130+
'value' => $by->getValue()
131+
]);
132+
$element = $this->newElement($raw_element['ELEMENT']);
133+
134+
$this->_dispatch('afterFindBy', $by, $element);
135+
136+
return $element;
137+
138+
}
139+
140+
/**
141+
* @param string $script
142+
* @param array $arguments
143+
* @return mixed
144+
*/
145+
protected function executeScript($script, array $arguments = []) {
146+
147+
$this->_dispatch('beforeScript', $script);
148+
$result = $this->_webdriver->executeScript($script, $arguments);
149+
$this->_dispatch('afterScript', $script);
150+
151+
return $result;
152+
153+
}
154+
155+
/**
156+
* Not yet implemented by WebDriver
157+
*
158+
* @return EventFiringWebDriver
159+
*/
160+
// protected function back() {
161+
//
162+
// $this->_dispatch('beforeNavigateBack');
163+
// $this->_webdriver->back();
164+
// $this->_dispatch('afterNavigateBack');
165+
// return $this;
166+
//
167+
// }
168+
169+
/**
170+
* Not yet implemented by WebDriver
171+
*
172+
* @return EventFiringWebDriver
173+
*/
174+
// protected function forward() {
175+
//
176+
// $this->_dispatch('beforeNavigateForward');
177+
// $this->_webdriver->forward();
178+
// $this->_dispatch('afterNavigateForward');
179+
// return $this;
180+
//
181+
// }
182+
183+
// Implement the following from WebDriver as protected methods so __call catches exceptions
184+
185+
/**
186+
* @return EventFiringWebDriver
187+
*/
188+
protected function close() {
189+
$this->_webdriver->close();
190+
191+
return $this;
192+
}
193+
194+
/**
195+
* @return string
196+
*/
197+
protected function getCurrentURL() {
198+
return $this->_webdriver->getCurrentURL();
199+
}
200+
201+
/**
202+
* @return string
203+
*/
204+
protected function getPageSource() {
205+
return $this->_webdriver->getPageSource();
206+
}
207+
208+
/**
209+
* @return string
210+
*/
211+
protected function getTitle() {
212+
return $this->_webdriver->getTitle();
213+
}
214+
215+
/**
216+
* @return string
217+
*/
218+
protected function getWindowHandle() {
219+
return $this->_webdriver->getWindowHandle();
220+
}
221+
222+
/**
223+
* @return array
224+
*/
225+
protected function getWindowHandles() {
226+
return $this->_webdriver->getWindowHandles();
227+
}
228+
229+
/**
230+
* @return void
231+
*/
232+
protected function quit() {
233+
$this->_webdriver->quit();
234+
}
235+
236+
/**
237+
* @param string|null $save_as
238+
* @return string
239+
*/
240+
protected function takeScreenshot($save_as = null) {
241+
return $this->_webdriver->takeScreenshot($save_as);
242+
}
243+
244+
/**
245+
* @param int $timeout_in_second
246+
* @param int $interval_in_millisecond
247+
* @return WebDriverWait
248+
*/
249+
protected function wait($timeout_in_second = 30, $interval_in_millisecond = 250) {
250+
return $this->_webdriver->wait($timeout_in_second, $interval_in_millisecond);
251+
}
252+
253+
/**
254+
* @return WebDriverOptions
255+
*/
256+
protected function manage() {
257+
return $this->_webdriver->manage();
258+
}
259+
260+
/**
261+
* @return WebDriverNavigation
262+
*/
263+
protected function navigate() {
264+
return $this->_webdriver->navigate();
265+
}
266+
267+
/**
268+
* @return WebDriverTargetLocator
269+
*/
270+
protected function switchTo() {
271+
return $this->_webdriver->switchTo();
272+
}
273+
274+
}

0 commit comments

Comments
 (0)