Complex classes like AbstractTestCase 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 AbstractTestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
||
| 22 | { |
||
| 23 | use ElementTrait; |
||
| 24 | use ElementsTrait; |
||
| 25 | use NotifyTrait; |
||
| 26 | use ScreenshotTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Selenium Web Driver host URI |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected static $webDriverHost = 'http://localhost:%s/wd/hub'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Selenium Web Driver port |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected static $webDriverPort = '4444'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected static $browser = 'firefox'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $browserBin; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $uri; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var PHPWebDriver_WebDriver |
||
| 59 | */ |
||
| 60 | protected $webDriver; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \PHPWebDriver_WebDriverSession |
||
| 64 | */ |
||
| 65 | protected $session; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Resolves URI to open session |
||
| 69 | */ |
||
| 70 | protected function setUp() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | protected function tearDown() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param Exception $exc |
||
| 91 | * @throws Exception |
||
| 92 | */ |
||
| 93 | protected function onNotSuccessfulTest(Exception $exc) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | protected function getBrowser() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | protected function getBrowserBin() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $bin |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | protected function setBrowserBin($bin) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return \PHPWebDriver_WebDriverSession |
||
| 128 | */ |
||
| 129 | public function getSession() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | protected function getSessionId() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | protected function getServerUrl() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get raw source from URL |
||
| 153 | * |
||
| 154 | * @param string $url |
||
| 155 | * @param null $sessId |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | protected function source($url, $sessId = null) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Resolve Selenium WebDriver host |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | protected function resolveHost() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Resolve Selenium WebDriver port |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | protected function resolvePort() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Resolve test session browser |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | protected function resolveBrowser() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Resolve test session capabilities |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | protected function resolveCapabilities() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Resolve test target URI |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | protected function resolveUri() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param float $sec |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | protected function sleep($sec = 2.0) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Opens URI and asserts not error |
||
| 261 | * |
||
| 262 | * @param string $path |
||
| 263 | * @param string $caption |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | protected function openOk($path = '', $caption = 'Home') |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Assert that page is without errors |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | protected function assertNotError() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function is404() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Clicks on a link |
||
| 310 | * |
||
| 311 | * @param string $linkText |
||
| 312 | * @param callable $callback |
||
| 313 | * @return $this |
||
| 314 | */ |
||
| 315 | protected function clickLink($linkText, callable $callback = null) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Clicks on ajax-link |
||
| 326 | * |
||
| 327 | * @param string $linkText |
||
| 328 | * @param callable $callback |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | protected function clickAjaxLink($linkText, callable $callback = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $class |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | protected function clickByClass($class) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $name |
||
| 352 | * @param string $value |
||
| 353 | * @return $this |
||
| 354 | * @deprecated use getSelect() |
||
| 355 | */ |
||
| 356 | protected function clickSelect($name, $value) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return WebDriver\Select |
||
| 365 | */ |
||
| 366 | protected function getSelect($name) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Enters the input value |
||
| 373 | * |
||
| 374 | * @param string|PHPWebDriver_WebDriverElement $name |
||
| 375 | * @param string $value |
||
| 376 | * @param callable|false|null $callback |
||
| 377 | * @return $this |
||
| 378 | */ |
||
| 379 | protected function enterInput($name, $value, $callback = null) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Submit the input value |
||
| 402 | * |
||
| 403 | * @param string|PHPWebDriver_WebDriverElement $name |
||
| 404 | * @param string $value |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | protected function submitInput($name, $value) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Assert that input value is same than expected |
||
| 417 | * |
||
| 418 | * @param string|PHPWebDriver_WebDriverElement $name |
||
| 419 | * @param string $expectedValue |
||
| 420 | * @param callable $callback |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | public function assertInput($name, $expectedValue, callable $callback = null) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Focus a browser window |
||
| 433 | * |
||
| 434 | * @param int $index |
||
| 435 | * @return $this |
||
| 436 | */ |
||
| 437 | protected function focusWindow($index) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Close a browser window |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | protected function closeWindow() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Wait for something, then do something else |
||
| 459 | * |
||
| 460 | * @param callable $action |
||
| 461 | * @param callable $callback |
||
| 462 | * @return $this |
||
| 463 | */ |
||
| 464 | protected function waitFor(callable $action, callable $callback = null) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Ajax wait |
||
| 473 | * |
||
| 474 | * Depends on jQuery. |
||
| 475 | * |
||
| 476 | * @param float $delay Seconds |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | protected function waitForAjax($delay = .1) |
||
| 490 | } |
||
| 491 |