Skip to content

Commit 12e4a94

Browse files
committed
Implemented EventFiringWebDriverNavigation to use the navigation events.
Where applicable, using the underlying element item to find elements. Abstract class EventFiringObject added to encapsulate some of the generic methods and dispatcher.
1 parent ea8c815 commit 12e4a94

File tree

5 files changed

+143
-155
lines changed

5 files changed

+143
-155
lines changed

lib/EventFiringObject.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
abstract class EventFiringObject {
4+
5+
/**
6+
* @var WebDriverDispatcher
7+
*/
8+
protected $_dispatcher;
9+
10+
public function __call($method, array $arguments = array()) {
11+
12+
try {
13+
14+
return call_user_func_array([$this, $method], $arguments);
15+
16+
} catch (WebDriverException $exception) {
17+
18+
$this->_dispatch('onException', $exception, $this);
19+
throw $exception;
20+
21+
}
22+
23+
}
24+
25+
/**
26+
* @return WebDriverDispatcher
27+
*/
28+
public function getDispatcher() {
29+
return $this->_dispatcher;
30+
}
31+
32+
/**
33+
* @param $method
34+
*/
35+
protected function _dispatch($method) {
36+
37+
$arguments = func_get_args();
38+
unset($arguments[0]);
39+
$arguments[] = $this;
40+
41+
if($this->_dispatcher)
42+
$this->_dispatcher->dispatch($method, $arguments);
43+
44+
}
45+
46+
}

lib/EventFiringWebDriver.php

Lines changed: 9 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
<?php
22

3-
class EventFiringWebDriver {
3+
class EventFiringWebDriver extends EventFiringObject {
44

55
/**
66
* @var WebDriver
77
*/
88
protected $_webdriver;
99

10-
/**
11-
* @var WebDriverDispatcher
12-
*/
13-
protected $_dispatcher;
14-
1510
/**
1611
* @param WebDriver $webdriver
1712
* @param WebDriverDispatcher $dispatcher
1813
*/
1914
public function __construct(WebDriver $webdriver, WebDriverDispatcher $dispatcher = null) {
2015

21-
$this->_dispatcher = $dispatcher ? $dispatcher : new WebDriverDispatcher();
16+
$this->_dispatcher = $dispatcher ? $dispatcher : new WebDriverDispatcher;
2217
if(!$this->_dispatcher->getDefaultDriver())
2318
$this->_dispatcher->setDefaultDriver($this);
2419

@@ -28,28 +23,6 @@ public function __construct(WebDriver $webdriver, WebDriverDispatcher $dispatche
2823

2924
}
3025

31-
public function __call($method, array $arguments = array()) {
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-
5326
/**
5427
* @return WebDriver
5528
*/
@@ -58,26 +31,11 @@ public function getWebDriver() {
5831
}
5932

6033
/**
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.
34+
* @param WebDriverElement $element
7735
* @return EventFiringWebElement
7836
*/
79-
protected function newElement($id) {
80-
return new EventFiringWebElement($this->_webdriver->getExecutor(), $id, $this->getDispatcher());
37+
protected function newElement(WebDriverElement $element) {
38+
return new EventFiringWebElement($element, $this->getDispatcher());
8139
}
8240

8341
/**
@@ -102,14 +60,9 @@ protected function findElements(WebDriverBy $by) {
10260

10361
$this->_dispatch('beforeFindBy', $by);
10462

105-
$raw_elements = $this->_webdriver->getExecutor()->execute('findElements', [
106-
'using' => $by->getMechanism(),
107-
'value' => $by->getValue()
108-
]);
109-
11063
$elements = array();
111-
foreach ($raw_elements as $raw_element)
112-
$elements[] = $this->newElement($raw_element['ELEMENT']);
64+
foreach($this->_webdriver->findElements($by) as $element)
65+
$elements[] = $this->newElement($element);
11366

11467
$this->_dispatch('afterFindBy', $by, $elements);
11568

@@ -124,13 +77,7 @@ protected function findElements(WebDriverBy $by) {
12477
protected function findElement(WebDriverBy $by) {
12578

12679
$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-
80+
$element = $this->newElement($this->_webdriver->findElement($by));
13481
$this->_dispatch('afterFindBy', $by, $element);
13582

13683
return $element;
@@ -152,34 +99,6 @@ protected function executeScript($script, array $arguments = array()) {
15299

153100
}
154101

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-
183102
// Implement the following from WebDriver as protected methods so __call catches exceptions
184103

185104
/**
@@ -261,7 +180,7 @@ protected function manage() {
261180
* @return WebDriverNavigation
262181
*/
263182
protected function navigate() {
264-
return $this->_webdriver->navigate();
183+
return new EventFiringWebDriverNavigation($this->_webdriver->navigate(), $this->getDispatcher());
265184
}
266185

267186
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
class EventFiringWebDriverNavigation extends EventFiringObject {
4+
5+
/**
6+
* @var WebDriverNavigation
7+
*/
8+
protected $_navigator;
9+
10+
/**
11+
* @param WebDriverNavigation $navigator
12+
* @param WebDriverDispatcher $dispatcher
13+
*/
14+
public function __construct(WebDriverNavigation $navigator, WebDriverDispatcher $dispatcher = null) {
15+
16+
$this->_navigator = $navigator;
17+
$this->_dispatcher = $dispatcher;
18+
return $this;
19+
20+
}
21+
22+
/**
23+
* @return WebDriverNavigation
24+
*/
25+
public function getNavigator() {
26+
return $this->_navigator;
27+
}
28+
29+
// Implement the following from WebDriverNavigation as protected methods so __call catches exceptions
30+
31+
/**
32+
* @return $this
33+
*/
34+
protected function back() {
35+
36+
$this->_dispatch('beforeNavigateBack', $this->getDispatcher()->getDefaultDriver());
37+
$this->_navigator->back();
38+
$this->_dispatch('afterNavigateBack', $this->getDispatcher()->getDefaultDriver());
39+
return $this;
40+
41+
}
42+
43+
/**
44+
* @return $this
45+
*/
46+
protected function forward() {
47+
48+
$this->_dispatch('beforeNavigateForward', $this->getDispatcher()->getDefaultDriver());
49+
$this->_navigator->forward();
50+
$this->_dispatch('afterNavigateForward', $this->getDispatcher()->getDefaultDriver());
51+
return $this;
52+
53+
}
54+
55+
/**
56+
* @return $this
57+
*/
58+
protected function refresh() {
59+
$this->_navigator->refresh();
60+
return $this;
61+
}
62+
63+
/**
64+
* @param $url
65+
* @return $this
66+
*/
67+
protected function to($url) {
68+
$this->_navigator->to($url);
69+
return $this;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)