Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BasicContext 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 BasicContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class BasicContext extends BehatContext |
||
| 31 | { |
||
| 32 | protected $context; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Date format in date() syntax |
||
| 36 | * @var String |
||
| 37 | */ |
||
| 38 | protected $dateFormat = 'Y-m-d'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Time format in date() syntax |
||
| 42 | * @var String |
||
| 43 | */ |
||
| 44 | protected $timeFormat = 'H:i:s'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Date/time format in date() syntax |
||
| 48 | * @var String |
||
| 49 | */ |
||
| 50 | protected $datetimeFormat = 'Y-m-d H:i:s'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initializes context. |
||
| 54 | * Every scenario gets it's own context object. |
||
| 55 | * |
||
| 56 | * @param array $parameters context parameters (set them up through behat.yml) |
||
| 57 | */ |
||
| 58 | public function __construct(array $parameters) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get Mink session from MinkContext |
||
| 65 | * |
||
| 66 | * @return \Behat\Mink\Session |
||
| 67 | */ |
||
| 68 | public function getSession($name = null) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @AfterStep ~@modal |
||
| 74 | * |
||
| 75 | * Excluding scenarios with @modal tag is required, |
||
| 76 | * because modal dialogs stop any JS interaction |
||
| 77 | */ |
||
| 78 | public function appendErrorHandlerBeforeStep(StepEvent $event) { |
||
| 79 | try{ |
||
| 80 | $javascript = <<<JS |
||
| 81 | window.onerror = function(message, file, line, column, error) { |
||
| 82 | var body = document.getElementsByTagName('body')[0]; |
||
| 83 | var msg = message + " in " + file + ":" + line + ":" + column; |
||
| 84 | if(error !== undefined && error.stack !== undefined) { |
||
| 85 | msg += "\\nSTACKTRACE:\\n" + error.stack; |
||
| 86 | } |
||
| 87 | body.setAttribute('data-jserrors', '[captured JavaScript error] ' + msg); |
||
| 88 | } |
||
| 89 | if ('undefined' !== typeof window.jQuery) { |
||
| 90 | window.jQuery('body').ajaxError(function(event, jqxhr, settings, exception) { |
||
| 91 | if ('abort' === exception) return; |
||
| 92 | window.onerror(event.type + ': ' + settings.type + ' ' + settings.url + ' ' + exception + ' ' + jqxhr.responseText); |
||
| 93 | }); |
||
| 94 | } |
||
| 95 | JS; |
||
| 96 | |||
| 97 | $this->getSession()->executeScript($javascript); |
||
| 98 | }catch(\WebDriver\Exception $e){ |
||
| 99 | $this->logException($e); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @AfterStep ~@modal |
||
| 105 | * |
||
| 106 | * Excluding scenarios with @modal tag is required, |
||
| 107 | * because modal dialogs stop any JS interaction |
||
| 108 | */ |
||
| 109 | public function readErrorHandlerAfterStep(StepEvent $event) { |
||
| 110 | try{ |
||
| 111 | $page = $this->getSession()->getPage(); |
||
| 112 | |||
| 113 | $jserrors = $page->find('xpath', '//body[@data-jserrors]'); |
||
| 114 | if (null !== $jserrors) { |
||
| 115 | $this->takeScreenshot($event); |
||
| 116 | file_put_contents('php://stderr', $jserrors->getAttribute('data-jserrors') . PHP_EOL); |
||
| 117 | } |
||
| 118 | |||
| 119 | $javascript = <<<JS |
||
| 120 | if ('undefined' !== typeof window.jQuery) { |
||
| 121 | window.jQuery(document).ready(function() { |
||
| 122 | window.jQuery('body').removeAttr('data-jserrors'); |
||
| 123 | }); |
||
| 124 | } |
||
| 125 | JS; |
||
| 126 | |||
| 127 | $this->getSession()->executeScript($javascript); |
||
| 128 | }catch(\WebDriver\Exception $e){ |
||
| 129 | $this->logException($e); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Hook into jQuery ajaxStart, ajaxSuccess and ajaxComplete events. |
||
| 135 | * Prepare __ajaxStatus() functions and attach them to these handlers. |
||
| 136 | * Event handlers are removed after one run. |
||
| 137 | * |
||
| 138 | * @BeforeStep |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function handleAjaxBeforeStep(StepEvent $event) { |
|
| 141 | try{ |
||
| 142 | $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); |
||
| 143 | $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); |
||
| 144 | |||
| 145 | if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | $javascript = <<<JS |
||
| 150 | if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.on) { |
||
| 151 | window.jQuery(document).on('ajaxStart.ss.test.behaviour', function(){ |
||
| 152 | window.__ajaxStatus = function() { |
||
| 153 | return 'waiting'; |
||
| 154 | }; |
||
| 155 | }); |
||
| 156 | window.jQuery(document).on('ajaxComplete.ss.test.behaviour', function(e, jqXHR){ |
||
| 157 | if (null === jqXHR.getResponseHeader('X-ControllerURL')) { |
||
| 158 | window.__ajaxStatus = function() { |
||
| 159 | return 'no ajax'; |
||
| 160 | }; |
||
| 161 | } |
||
| 162 | }); |
||
| 163 | window.jQuery(document).on('ajaxSuccess.ss.test.behaviour', function(e, jqXHR){ |
||
| 164 | if (null === jqXHR.getResponseHeader('X-ControllerURL')) { |
||
| 165 | window.__ajaxStatus = function() { |
||
| 166 | return 'success'; |
||
| 167 | }; |
||
| 168 | } |
||
| 169 | }); |
||
| 170 | } |
||
| 171 | JS; |
||
| 172 | $this->getSession()->wait(500); // give browser a chance to process and render response |
||
| 173 | $this->getSession()->executeScript($javascript); |
||
| 174 | }catch(\WebDriver\Exception $e){ |
||
| 175 | $this->logException($e); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Wait for the __ajaxStatus()to return anything but 'waiting'. |
||
| 181 | * Don't wait longer than 5 seconds. |
||
| 182 | * |
||
| 183 | * Don't unregister handler if we're dealing with modal windows |
||
| 184 | * |
||
| 185 | * @AfterStep ~@modal |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function handleAjaxAfterStep(StepEvent $event) { |
|
| 188 | try{ |
||
| 189 | $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps(); |
||
| 190 | $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps)); |
||
| 191 | |||
| 192 | if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) { |
||
| 193 | return; |
||
| 194 | } |
||
| 195 | |||
| 196 | $this->handleAjaxTimeout(); |
||
| 197 | |||
| 198 | $javascript = <<<JS |
||
| 199 | if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.off) { |
||
| 200 | window.jQuery(document).off('ajaxStart.ss.test.behaviour'); |
||
| 201 | window.jQuery(document).off('ajaxComplete.ss.test.behaviour'); |
||
| 202 | window.jQuery(document).off('ajaxSuccess.ss.test.behaviour'); |
||
| 203 | } |
||
| 204 | JS; |
||
| 205 | $this->getSession()->executeScript($javascript); |
||
| 206 | }catch(\WebDriver\Exception $e){ |
||
| 207 | $this->logException($e); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | public function handleAjaxTimeout() { |
||
| 212 | $timeoutMs = $this->getMainContext()->getAjaxTimeout(); |
||
| 213 | |||
| 214 | // Wait for an ajax request to complete, but only for a maximum of 5 seconds to avoid deadlocks |
||
| 215 | $this->getSession()->wait($timeoutMs, |
||
| 216 | "(typeof window.__ajaxStatus !== 'undefined' ? window.__ajaxStatus() : 'no ajax') !== 'waiting'" |
||
| 217 | ); |
||
| 218 | |||
| 219 | // wait additional 100ms to allow DOM to update |
||
| 220 | $this->getSession()->wait(100); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Take screenshot when step fails. |
||
| 225 | * Works only with Selenium2Driver. |
||
| 226 | * |
||
| 227 | * @AfterStep |
||
| 228 | */ |
||
| 229 | public function takeScreenshotAfterFailedStep(StepEvent $event) { |
||
| 230 | if (4 === $event->getResult()) { |
||
| 231 | try{ |
||
| 232 | $this->takeScreenshot($event); |
||
| 233 | }catch(\WebDriver\Exception $e){ |
||
| 234 | $this->logException($e); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Close modal dialog if test scenario fails on CMS page |
||
| 241 | * |
||
| 242 | * @AfterScenario |
||
| 243 | */ |
||
| 244 | public function closeModalDialog(ScenarioEvent $event) { |
||
| 245 | try{ |
||
| 246 | // Only for failed tests on CMS page |
||
| 247 | if (4 === $event->getResult()) { |
||
| 248 | $cmsElement = $this->getSession()->getPage()->find('css', '.cms'); |
||
| 249 | if($cmsElement) { |
||
| 250 | try { |
||
| 251 | // Navigate away triggered by reloading the page |
||
| 252 | $this->getSession()->reload(); |
||
| 253 | $this->getSession()->getDriver()->getWebDriverSession()->accept_alert(); |
||
| 254 | } catch(\WebDriver\Exception $e) { |
||
| 255 | // no-op, alert might not be present |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | }catch(\WebDriver\Exception $e){ |
||
| 260 | $this->logException($e); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Delete any created files and folders from assets directory |
||
| 266 | * |
||
| 267 | * @AfterScenario @assets |
||
| 268 | */ |
||
| 269 | public function cleanAssetsAfterScenario(ScenarioEvent $event) { |
||
| 270 | foreach(\File::get() as $file) { |
||
| 271 | if(file_exists($file->getFullPath())) $file->delete(); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | public function takeScreenshot(StepEvent $event) { |
||
| 276 | $driver = $this->getSession()->getDriver(); |
||
| 277 | // quit silently when unsupported |
||
| 278 | if (!($driver instanceof Selenium2Driver)) { |
||
| 279 | return; |
||
| 280 | } |
||
| 281 | |||
| 282 | $parent = $event->getLogicalParent(); |
||
| 283 | $feature = $parent->getFeature(); |
||
| 284 | $step = $event->getStep(); |
||
| 285 | $screenshotPath = null; |
||
| 286 | |||
| 287 | $path = $this->getMainContext()->getScreenshotPath(); |
||
| 288 | if(!$path) return; // quit silently when path is not set |
||
| 289 | |||
| 290 | \Filesystem::makeFolder($path); |
||
| 291 | $path = realpath($path); |
||
| 292 | |||
| 293 | View Code Duplication | if (!file_exists($path)) { |
|
| 294 | file_put_contents('php://stderr', sprintf('"%s" is not valid directory and failed to create it' . PHP_EOL, $path)); |
||
| 295 | return; |
||
| 296 | } |
||
| 297 | |||
| 298 | View Code Duplication | if (file_exists($path) && !is_dir($path)) { |
|
| 299 | file_put_contents('php://stderr', sprintf('"%s" is not valid directory' . PHP_EOL, $path)); |
||
| 300 | return; |
||
| 301 | } |
||
| 302 | View Code Duplication | if (file_exists($path) && !is_writable($path)) { |
|
| 303 | file_put_contents('php://stderr', sprintf('"%s" directory is not writable' . PHP_EOL, $path)); |
||
| 304 | return; |
||
| 305 | } |
||
| 306 | |||
| 307 | $path = sprintf('%s/%s_%d.png', $path, basename($feature->getFile()), $step->getLine()); |
||
| 308 | $screenshot = $driver->getWebDriverSession()->screenshot(); |
||
| 309 | file_put_contents($path, base64_decode($screenshot)); |
||
| 310 | file_put_contents('php://stderr', sprintf('Saving screenshot into %s' . PHP_EOL, $path)); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @Then /^I should be redirected to "([^"]+)"/ |
||
| 315 | */ |
||
| 316 | public function stepIShouldBeRedirectedTo($url) { |
||
| 317 | if ($this->getMainContext()->canIntercept()) { |
||
| 318 | $client = $this->getSession()->getDriver()->getClient(); |
||
| 319 | $client->followRedirects(true); |
||
| 320 | $client->followRedirect(); |
||
| 321 | |||
| 322 | $url = $this->getMainContext()->joinUrlParts($this->context['base_url'], $url); |
||
| 323 | |||
| 324 | assertTrue($this->getMainContext()->isCurrentUrlSimilarTo($url), sprintf('Current URL is not %s', $url)); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @Given /^the page can't be found/ |
||
| 330 | */ |
||
| 331 | public function stepPageCantBeFound() { |
||
| 332 | $page = $this->getSession()->getPage(); |
||
| 333 | assertTrue( |
||
| 334 | // Content from ErrorPage default record |
||
| 335 | $page->hasContent('Page not found') |
||
| 336 | // Generic ModelAsController message |
||
| 337 | || $page->hasContent('The requested page could not be found') |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @Given /^I wait (?:for )?([\d\.]+) second(?:s?)$/ |
||
| 343 | */ |
||
| 344 | public function stepIWaitFor($secs) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @Given /^I press the "([^"]*)" button$/ |
||
| 350 | */ |
||
| 351 | public function stepIPressTheButton($button) { |
||
| 352 | $page = $this->getSession()->getPage(); |
||
| 353 | $els = $page->findAll('named', array('link_or_button', "'$button'")); |
||
| 354 | $matchedEl = null; |
||
| 355 | foreach($els as $el) { |
||
| 356 | if($el->isVisible()) $matchedEl = $el; |
||
| 357 | } |
||
| 358 | assertNotNull($matchedEl, sprintf('%s button not found', $button)); |
||
| 359 | $matchedEl->click(); |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 364 | * Example1: I press the "Remove current combo" button, confirming the dialog |
||
| 365 | * Example2: I follow the "Remove current combo" link, confirming the dialog |
||
| 366 | * |
||
| 367 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), confirming the dialog$/ |
||
| 368 | */ |
||
| 369 | public function stepIPressTheButtonConfirmingTheDialog($button) { |
||
| 370 | $this->stepIPressTheButton($button); |
||
| 371 | $this->iConfirmTheDialog(); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 376 | * Example: I follow the "Remove current combo" link, dismissing the dialog |
||
| 377 | * |
||
| 378 | * @Given /^I (?:press|follow) the "([^"]*)" (?:button|link), dismissing the dialog$/ |
||
| 379 | */ |
||
| 380 | public function stepIPressTheButtonDismissingTheDialog($button) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element$/ |
||
| 387 | */ |
||
| 388 | public function iClickInTheElement($clickType, $text, $selector) { |
||
| 389 | $clickTypeMap = array( |
||
| 390 | "double click" => "doubleclick", |
||
| 391 | "click" => "click" |
||
| 392 | ); |
||
| 393 | $page = $this->getSession()->getPage(); |
||
| 394 | $parentElement = $page->find('css', $selector); |
||
| 395 | assertNotNull($parentElement, sprintf('"%s" element not found', $selector)); |
||
| 396 | $element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text)); |
||
| 397 | assertNotNull($element, sprintf('"%s" not found', $text)); |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 404 | * Example: I click "Delete" in the ".actions" element, confirming the dialog |
||
| 405 | * |
||
| 406 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, confirming the dialog$/ |
||
| 407 | */ |
||
| 408 | public function iClickInTheElementConfirmingTheDialog($clickType, $text, $selector) { |
||
| 412 | /** |
||
| 413 | * Needs to be in single command to avoid "unexpected alert open" errors in Selenium. |
||
| 414 | * Example: I click "Delete" in the ".actions" element, dismissing the dialog |
||
| 415 | * |
||
| 416 | * @Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, dismissing the dialog$/ |
||
| 417 | */ |
||
| 418 | public function iClickInTheElementDismissingTheDialog($clickType, $text, $selector) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @Given /^I type "([^"]*)" into the dialog$/ |
||
| 425 | */ |
||
| 426 | public function iTypeIntoTheDialog($data) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @Given /^I confirm the dialog$/ |
||
| 435 | */ |
||
| 436 | public function iConfirmTheDialog() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @Given /^I dismiss the dialog$/ |
||
| 443 | */ |
||
| 444 | public function iDismissTheDialog() { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @Given /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)" with HTML5$/ |
||
| 451 | */ |
||
| 452 | public function iAttachTheFileTo($field, $path) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Select an individual input from within a group, matched by the top-most label. |
||
| 469 | * |
||
| 470 | * @Given /^I select "([^"]*)" from "([^"]*)" input group$/ |
||
| 471 | */ |
||
| 472 | public function iSelectFromInputGroup($value, $labelText) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Pauses the scenario until the user presses a key. Useful when debugging a scenario. |
||
| 498 | * |
||
| 499 | * @Then /^(?:|I )put a breakpoint$/ |
||
| 500 | */ |
||
| 501 | public function iPutABreakpoint() { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Transforms relative time statements compatible with strtotime(). |
||
| 511 | * Example: "time of 1 hour ago" might return "22:00:00" if its currently "23:00:00". |
||
| 512 | * Customize through {@link setTimeFormat()}. |
||
| 513 | * |
||
| 514 | * @Transform /^(?:(the|a)) time of (?<val>.*)$/ |
||
| 515 | */ |
||
| 516 | View Code Duplication | public function castRelativeToAbsoluteTime($prefix, $val) { |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Transforms relative date and time statements compatible with strtotime(). |
||
| 529 | * Example: "datetime of 2 days ago" might return "2013-10-10 22:00:00" if its currently |
||
| 530 | * the 12th of October 2013. Customize through {@link setDatetimeFormat()}. |
||
| 531 | * |
||
| 532 | * @Transform /^(?:(the|a)) datetime of (?<val>.*)$/ |
||
| 533 | */ |
||
| 534 | View Code Duplication | public function castRelativeToAbsoluteDatetime($prefix, $val) { |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Transforms relative date statements compatible with strtotime(). |
||
| 547 | * Example: "date 2 days ago" might return "2013-10-10" if its currently |
||
| 548 | * the 12th of October 2013. Customize through {@link setDateFormat()}. |
||
| 549 | * |
||
| 550 | * @Transform /^(?:(the|a)) date of (?<val>.*)$/ |
||
| 551 | */ |
||
| 552 | View Code Duplication | public function castRelativeToAbsoluteDate($prefix, $val) { |
|
| 562 | |||
| 563 | public function getDateFormat() { |
||
| 566 | |||
| 567 | public function setDateFormat($format) { |
||
| 570 | |||
| 571 | public function getTimeFormat() { |
||
| 574 | |||
| 575 | public function setTimeFormat($format) { |
||
| 578 | |||
| 579 | public function getDatetimeFormat() { |
||
| 582 | |||
| 583 | public function setDatetimeFormat($format) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Checks that field with specified in|name|label|value is disabled. |
||
| 589 | * Example: Then the field "Email" should be disabled |
||
| 590 | * Example: Then the "Email" field should be disabled |
||
| 591 | * |
||
| 592 | * @Then /^the "(?P<name>(?:[^"]|\\")*)" (?P<type>(?:(field|button))) should (?P<negate>(?:(not |)))be disabled/ |
||
| 593 | * @Then /^the (?P<type>(?:(field|button))) "(?P<name>(?:[^"]|\\")*)" should (?P<negate>(?:(not |)))be disabled/ |
||
| 594 | */ |
||
| 595 | public function stepFieldShouldBeDisabled($name, $type, $negate) { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Checks that checkbox with specified in|name|label|value is enabled. |
||
| 617 | * Example: Then the field "Email" should be enabled |
||
| 618 | * Example: Then the "Email" field should be enabled |
||
| 619 | * |
||
| 620 | * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should be enabled/ |
||
| 621 | * @Then /^the field "(?P<field>(?:[^"]|\\")*)" should be enabled/ |
||
| 622 | */ |
||
| 623 | public function stepFieldShouldBeEnabled($field) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute, |
||
| 635 | * or a named region mapped to a CSS selector via Behat configuration). |
||
| 636 | * |
||
| 637 | * Example: Given I follow "Select" in the "header .login-form" region |
||
| 638 | * Example: Given I follow "Select" in the "My Login Form" region |
||
| 639 | * |
||
| 640 | * @Given /^I (?:follow|click) "(?P<link>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
| 641 | */ |
||
| 642 | View Code Duplication | public function iFollowInTheRegion($link, $region) { |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Fills in a field in a specfic region similar to (@see iFollowInTheRegion or @see iSeeTextInRegion) |
||
| 658 | * |
||
| 659 | * Example: Given I fill in "Hello" with "World" |
||
| 660 | * |
||
| 661 | * @Given /^I fill in "(?P<field>[^"]*)" with "(?P<value>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
| 662 | */ |
||
| 663 | View Code Duplication | public function iFillinTheRegion($field, $value, $region){ |
|
| 676 | |||
| 677 | |||
| 678 | /** |
||
| 679 | * Asserts text in a specific region (an element identified by a CSS selector, a "data-title" attribute, |
||
| 680 | * or a named region mapped to a CSS selector via Behat configuration). |
||
| 681 | * Supports regular expressions in text value. |
||
| 682 | * |
||
| 683 | * Example: Given I should see "My Text" in the "header .login-form" region |
||
| 684 | * Example: Given I should not see "My Text" in the "My Login Form" region |
||
| 685 | * |
||
| 686 | * @Given /^I should (?P<negate>(?:(not |)))see "(?P<text>[^"]*)" in the "(?P<region>[^"]*)" region$/ |
||
| 687 | */ |
||
| 688 | public function iSeeTextInRegion($negate, $text, $region) { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Selects the specified radio button |
||
| 725 | * |
||
| 726 | * @Given /^I select the "([^"]*)" radio button$/ |
||
| 727 | */ |
||
| 728 | public function iSelectTheRadioButton($radioLabel) { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @Then /^the "([^"]*)" table should contain "([^"]*)"$/ |
||
| 739 | */ |
||
| 740 | public function theTableShouldContain($selector, $text) { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @Then /^the "([^"]*)" table should not contain "([^"]*)"$/ |
||
| 749 | */ |
||
| 750 | public function theTableShouldNotContain($selector, $text) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * @Given /^I click on "([^"]*)" in the "([^"]*)" table$/ |
||
| 759 | */ |
||
| 760 | public function iClickOnInTheTable($text, $selector) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Finds the first visible table by various factors: |
||
| 770 | * - table[id] |
||
| 771 | * - table[title] |
||
| 772 | * - table *[class=title] |
||
| 773 | * - fieldset[data-name] table |
||
| 774 | * - table caption |
||
| 775 | * |
||
| 776 | * @return Behat\Mink\Element\NodeElement |
||
| 777 | */ |
||
| 778 | protected function getTable($selector) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Checks the order of two texts. |
||
| 815 | * Assumptions: the two texts appear in their conjunct parent element once |
||
| 816 | * @Then /^I should see the text "(?P<textBefore>(?:[^"]|\\")*)" (before|after) the text "(?P<textAfter>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/ |
||
| 817 | */ |
||
| 818 | public function theTextBeforeAfter($textBefore, $order, $textAfter, $element) { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Wait until a certain amount of seconds till I see an element identified by a CSS selector. |
||
| 838 | * |
||
| 839 | * Example: Given I wait for 10 seconds until I see the ".css_element" element |
||
| 840 | * |
||
| 841 | * @Given /^I wait for (\d+) seconds until I see the "([^"]*)" element$/ |
||
| 842 | **/ |
||
| 843 | View Code Duplication | public function iWaitXUntilISee($wait, $selector) { |
|
| 856 | |||
| 857 | /** |
||
| 858 | * Wait until a particular element is visible, using a CSS selector. Useful for content loaded via AJAX, or only |
||
| 859 | * populated after JS execution. |
||
| 860 | * |
||
| 861 | * Example: Given I wait until I see the "header .login-form" element |
||
| 862 | * |
||
| 863 | * @Given /^I wait until I see the "([^"]*)" element$/ |
||
| 864 | */ |
||
| 865 | View Code Duplication | public function iWaitUntilISee($selector) { |
|
| 876 | |||
| 877 | /** |
||
| 878 | * Wait until a particular string is found on the page. Useful for content loaded via AJAX, or only populated after |
||
| 879 | * JS execution. |
||
| 880 | * |
||
| 881 | * Example: Given I wait until I see the text "Welcome back, John!" |
||
| 882 | * |
||
| 883 | * @Given /^I wait until I see the text "([^"]*)"$/ |
||
| 884 | */ |
||
| 885 | public function iWaitUntilISeeText($text){ |
||
| 901 | |||
| 902 | /** |
||
| 903 | * @Given /^I scroll to the bottom$/ |
||
| 904 | */ |
||
| 905 | public function iScrollToBottom() { |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @Given /^I scroll to the top$/ |
||
| 912 | */ |
||
| 913 | public function iScrollToTop() { |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Scroll to a certain element by label. |
||
| 919 | * Requires an "id" attribute to uniquely identify the element in the document. |
||
| 920 | * |
||
| 921 | * Example: Given I scroll to the "Submit" button |
||
| 922 | * Example: Given I scroll to the "My Date" field |
||
| 923 | * |
||
| 924 | * @Given /^I scroll to the "([^"]*)" (field|link|button)$/ |
||
| 925 | */ |
||
| 926 | public function iScrollToField($locator, $type) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Scroll to a certain element by CSS selector. |
||
| 942 | * Requires an "id" attribute to uniquely identify the element in the document. |
||
| 943 | * |
||
| 944 | * Example: Given I scroll to the ".css_element" element |
||
| 945 | * |
||
| 946 | * @Given /^I scroll to the "(?P<locator>(?:[^"]|\\")*)" element$/ |
||
| 947 | */ |
||
| 948 | public function iScrollToElement($locator) { |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Continuously poll the dom until callback returns true, code copied from |
||
| 963 | * (@link http://docs.behat.org/cookbook/using_spin_functions.html) |
||
| 964 | * If not found within a given wait period, timeout and throw error |
||
| 965 | * |
||
| 966 | * @param callback $lambda The function to run continuously |
||
| 967 | * @param integer $wait Timeout in seconds |
||
| 968 | * @return bool Returns true if the lambda returns successfully |
||
| 969 | * @throws \Exception Thrown if the wait threshold is exceeded without the lambda successfully returning |
||
| 970 | */ |
||
| 971 | public function spin($lambda, $wait = 60) { |
||
| 992 | |||
| 993 | |||
| 994 | |||
| 995 | /** |
||
| 996 | * We have to catch exceptions and log somehow else otherwise behat falls over |
||
| 997 | */ |
||
| 998 | protected function logException($e){ |
||
| 1001 | |||
| 1002 | } |
||
| 1003 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: