Complex classes like Driver_Selenium often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Driver_Selenium, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Driver_Selenium extends Driver { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Driver name |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public $name = 'selenium'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Array containing parameters to be added on the next request |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $_next_query = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Variable holding the current Driver_Selenium_Connection |
||
| 30 | * @var Driver_Selenium_Connection |
||
| 31 | */ |
||
| 32 | protected $_connection; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The base URL, to be prefixed on each request |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $_base_url = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * As selenium might be a bit slow to respond, we increase the default wait time. |
||
| 42 | * @var integer |
||
| 43 | */ |
||
| 44 | public $default_wait_time = 4000; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Getter / Setter of the base_url, that will be prefixed on each request |
||
| 48 | * @param string $base_url |
||
| 49 | * @return string|Driver_PHantomjs |
||
| 50 | */ |
||
| 51 | 4 | public function base_url($base_url = NULL) |
|
| 52 | { |
||
| 53 | 4 | if ($base_url !== NULL) |
|
| 54 | { |
||
| 55 | 1 | $this->_base_url = $base_url; |
|
| 56 | 1 | return $this; |
|
| 57 | } |
||
| 58 | 4 | return $this->_base_url; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Clear the connection by deleting all cookies |
||
| 63 | */ |
||
| 64 | 1 | public function clear() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Getter of the raw content html, this driver does not allow "setting" |
||
| 71 | * |
||
| 72 | * @param string $content |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 1 | public function content($content = NULL) |
|
| 76 | { |
||
| 77 | 1 | return $this->connection()->get('source'); |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Getter / Setter of the Driver_Selenium_Connection object. |
||
| 82 | * Use this to customize the connection, otherwise a default one on a random port will be used |
||
| 83 | * |
||
| 84 | * @param Driver_Selenium_Connection $connection |
||
| 85 | * @return Driver_Selenium_Connection|Driver_Selenium |
||
| 86 | */ |
||
| 87 | 12 | public function connection(Driver_Selenium_Connection $connection = NULL) |
|
| 88 | { |
||
| 89 | 12 | if ($connection !== NULL) |
|
| 90 | { |
||
| 91 | 1 | $this->_connection = $connection; |
|
| 92 | 1 | return $this; |
|
| 93 | } |
||
| 94 | |||
| 95 | 12 | if ( ! $this->_connection) |
|
| 96 | { |
||
| 97 | 1 | $this->_connection = new Driver_Selenium_Connection(); |
|
| 98 | 1 | $this->_connection->start(array('browserName' => 'firefox', 'acceptSslCerts' => FALSE)); |
|
| 99 | } |
||
| 100 | |||
| 101 | 12 | return $this->_connection; |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * NODE GETTERS |
||
| 106 | * ===================================== |
||
| 107 | */ |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the tag name of a Node with id. e.g. DIV, SPAN ... |
||
| 111 | * @param string $id |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | 2 | public function tag_name($id) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Get the attribute of a Node with id. If the attribute does not exist, returns NULL |
||
| 121 | * @param string $id |
||
| 122 | * @param string $name |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 2 | public function attribute($id, $name) |
|
| 126 | { |
||
| 127 | 2 | return $this->connection()->get("element/$id/attribute/$name"); |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Return the raw html of a Node with id, along with all of its children. |
||
| 132 | * @param string $id |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 1 | public function html($id) |
|
| 136 | { |
||
| 137 | 1 | if ($id === NULL) |
|
| 138 | return $this->content(); |
||
| 139 | |||
| 140 | 1 | return $this->execute($id, 'return arguments[0].outerHTML'); |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Return the text of a Node with id, with all the spaces collapsed, similar to browser rendering. |
||
| 145 | * @param string $id |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | 2 | public function text($id) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Return the value of a Node of a form element, e.g. INPUT, TEXTAREA or SELECT |
||
| 157 | * @param string $id |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | 2 | public function value($id) |
|
| 161 | { |
||
| 162 | 2 | if ($this->tag_name($id) == 'select' AND $this->attribute($id, 'multiple')) |
|
| 163 | { |
||
| 164 | 2 | $self = $this; |
|
| 165 | 2 | $values = array(); |
|
| 166 | 2 | foreach ($this->all('//option', $id) as $option_id) |
|
| 167 | { |
||
| 168 | 2 | if ($this->is_selected($option_id)) |
|
| 169 | { |
||
| 170 | 2 | $values []= $this->value($option_id); |
|
| 171 | } |
||
| 172 | } |
||
| 173 | 2 | return $values; |
|
| 174 | } |
||
| 175 | else |
||
| 176 | { |
||
| 177 | 2 | return $this->connection()->get("element/$id/value"); |
|
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Check if a Node with id is visible. |
||
| 183 | * @param string $id |
||
| 184 | * @return boolean |
||
| 185 | */ |
||
| 186 | 1 | public function is_visible($id) |
|
| 187 | { |
||
| 188 | 1 | return $this->connection()->get("element/$id/displayed"); |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Check if a Node with id of an option element is selected |
||
| 193 | * @param string $id |
||
| 194 | * @return boolean |
||
| 195 | */ |
||
| 196 | 2 | public function is_selected($id) |
|
| 197 | { |
||
| 198 | 2 | return $this->connection()->get("element/$id/selected"); |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Check if a Node with id of an input element (radio or checkbox) is checked |
||
| 203 | * @param string $id |
||
| 204 | * @return boolean |
||
| 205 | */ |
||
| 206 | 1 | public function is_checked($id) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Set the value of a Node with id of a form element |
||
| 213 | * @param string $id |
||
| 214 | * @param string $value |
||
| 215 | */ |
||
| 216 | 1 | public function set($id, $value) |
|
| 217 | { |
||
| 218 | 1 | $tag_name = $this->tag_name($id); |
|
| 219 | |||
| 220 | 1 | if ($tag_name == 'textarea') |
|
| 221 | { |
||
| 222 | 1 | $this->connection()->post("element/$id/clear", array()); |
|
| 223 | 1 | $this->connection()->post("element/$id/value", array('value' => str_split($value))); |
|
| 224 | } |
||
| 225 | 1 | elseif ($tag_name == 'input') |
|
| 226 | { |
||
| 227 | 1 | $type = $this->attribute($id, 'type'); |
|
| 228 | 1 | if ($type == 'checkbox' OR $type == 'radio') |
|
| 229 | { |
||
| 230 | 1 | $this->connection()->post("element/$id/click", array()); |
|
| 231 | } |
||
| 232 | else |
||
| 233 | { |
||
| 234 | 1 | if ($type !== 'file') |
|
| 235 | { |
||
| 236 | 1 | $this->connection()->post("element/$id/clear", array()); |
|
| 237 | } |
||
| 238 | 1 | $this->connection()->post("element/$id/value", array('value' => str_split($value))); |
|
| 239 | } |
||
| 240 | } |
||
| 241 | 1 | elseif ($tag_name == 'option') |
|
| 242 | { |
||
| 243 | 1 | $this->connection()->post("element/$id/click", array()); |
|
| 244 | } |
||
| 245 | 1 | } |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Set the option value that is selected of a Node of a select element |
||
| 249 | * @param string $id |
||
| 250 | * @param string $value |
||
| 251 | */ |
||
| 252 | 1 | public function select_option($id, $value) |
|
| 253 | { |
||
| 254 | 1 | $this->connection()->post("element/$id/click", array()); |
|
| 255 | 1 | } |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Confirm or cancel for the next confirmation dialog |
||
| 259 | * @param bool $confirm |
||
| 260 | */ |
||
| 261 | 2 | public function confirm($confirm) |
|
| 262 | { |
||
| 263 | 2 | if ($confirm) |
|
| 264 | { |
||
| 265 | 1 | $this->connection()->post('accept_alert', array()); |
|
| 266 | } |
||
| 267 | else |
||
| 268 | { |
||
| 269 | 2 | $this->connection()->post('dismiss_alert', array()); |
|
| 270 | } |
||
| 271 | 2 | } |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Get the text of the currently displayed alert / confirm /prompt dialog |
||
| 275 | * @param bool $confirm |
||
| 276 | */ |
||
| 277 | 2 | public function alert_text() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Click on a Node with id, triggering a link or form submit |
||
| 284 | * @param string $id |
||
| 285 | */ |
||
| 286 | 1 | public function click($id) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Go to a given url address, use next_query along with the provided query array |
||
| 293 | * @param string $uri |
||
| 294 | * @param array $query |
||
| 295 | */ |
||
| 296 | 3 | public function visit($uri, array $query = NULL) |
|
| 297 | { |
||
| 298 | 3 | $query = array_merge((array) $this->_next_query, (array) $query); |
|
| 299 | |||
| 300 | 3 | $this->_next_query = NULL; |
|
| 301 | |||
| 302 | // Check for implicit query string |
||
| 303 | 3 | if (strpos($uri, '?') !== FALSE) |
|
| 304 | { |
||
| 305 | $query = array_merge(self::extract_query_from_uri($uri), $query); |
||
| 306 | $uri = substr($uri, 0, strpos($uri, '?')); |
||
| 307 | } |
||
| 308 | |||
| 309 | 3 | $url = $this->base_url().$uri.($query ? '?'.http_build_query($query) : ''); |
|
| 310 | |||
| 311 | 3 | $this->connection()->post('url', array('url' => $url)); |
|
| 312 | 3 | } |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Get the current path (without host and protocol) |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 1 | public function current_path() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Get the current url |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 2 | public function current_url() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Find all ids of a given XPath |
||
| 336 | * @param string $xpath |
||
| 337 | * @param string $parent id of the parent node |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | 6 | public function all($xpath, $parent = NULL) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Setter for the next_query variable, to be added to the next visit's query |
||
| 349 | * @param array $query |
||
| 350 | */ |
||
| 351 | 1 | public function next_query(array $query) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Execute raw javascript. it will be executed in the context of a given node ('this' will point to the node) and the return of the script will be the return of this method |
||
| 359 | * @param string $id |
||
| 360 | * @param string $script |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | 3 | public function execute($id, $script) |
|
| 364 | { |
||
| 365 | 3 | return $this->connection()->post('execute', array( |
|
| 366 | 3 | 'script' => $script, |
|
| 367 | 3 | 'args' => $id ? array(array('ELEMENT' => $id)) : array() |
|
| 368 | )); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Check if a connection is active |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | 1 | public function is_page_active() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Move the mouse to a given element, or coordinates, or coordinates relative to a given element |
||
| 382 | * @param string $id |
||
| 383 | * @param integer $x |
||
| 384 | * @param integer $y |
||
| 385 | */ |
||
| 386 | 1 | public function move_to($id = NULL, $x = NULL, $y = NULL) |
|
| 387 | { |
||
| 388 | 1 | $this->connection()->post('moveto', array_filter(array( |
|
| 389 | 1 | 'element' => $id, |
|
| 390 | 1 | 'xoffset' => $x, |
|
| 391 | 1 | 'yoffset' => $y |
|
| 392 | 1 | ), function($param) |
|
| 393 | { |
||
| 394 | 1 | return ($param OR $param === 0); |
|
| 395 | 1 | })); |
|
| 396 | 1 | } |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Do a screenshot of the current page into a file |
||
| 400 | * @param string $file |
||
| 401 | */ |
||
| 402 | 1 | public function screenshot($file) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Return all the current cookies |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | 1 | public function cookies() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Set a cookie. Use parameters to set "expiry", "path", "domain" and "secure" |
||
| 420 | * @param string $name |
||
| 421 | * @param mixed $value |
||
| 422 | * @param array $parameters |
||
| 423 | */ |
||
| 424 | 1 | public function cookie($name, $value, array $parameters = array()) |
|
| 425 | { |
||
| 426 | 1 | $parameters = array_merge(array( |
|
| 427 | 1 | 'name' => $name, |
|
| 428 | 1 | 'value' => $value, |
|
| 429 | 1 | 'expiry' => time() + 86400, |
|
| 430 | ), $parameters); |
||
| 431 | |||
| 434 | } |
||
| 435 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: