Skip to content

Instantly share code, notes, and snippets.

@shanly-suepaul
Created February 4, 2014 20:42
Show Gist options
  • Save shanly-suepaul/8811941 to your computer and use it in GitHub Desktop.
Save shanly-suepaul/8811941 to your computer and use it in GitHub Desktop.
Here are two step definitions you might find useful: 1) These two functions override the default "click" functionality to make them easier to use with the Drupal admin menu. (Ask me for more info). It also allows the page to finish loading in Safari/IE (which have worse support in Selenium than other browsers) 2) I break - this is already in Dru…
<?php
/**
* Add a custom named selector for links. The selector will not match strings
* that contain the search but are larger than the search.
* @BeforeScenario
*/
public function addLinkStrictNamedSelector($event)
{
$selectors = $this->getSession()->getSelectorsHandler();
$selectors->getSelector('named')->registerNamedXpath('link_strict', <<<XPATH
.//a[./@href][(((./@id = %locator% or normalize-space(string(.)) = %locator%) or contains(./@title, %locator%) or contains(./@rel, %locator%)) or .//img[contains(./@alt, %locator%)])] | .//*[./@role = 'link'][((./@id = %locator% or contains(./@value, %locator%)) or contains(./@title, %locator%) or normalize-space(string(.)) = %locator%)]
XPATH
);
}
/**
* OVERRIDES FROM MINKCONTEXT.
*/
/*
* Clicks link with specified id|title|alt|text.
*
* overrides /^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/
*/
public function clickLink($link) {
// Get the link element and click it.
$link = $this->fixStepArgument($link);
$page = $this->getSession()->getPage();
$linkElement = $page->find('named', array(
'link_strict', $this->getSession()->getSelectorsHandler()->xpathLiteral($link)
));
if (NULL === $linkElement) {
throw new ElementNotFoundException(
$this->getSession(), 'link', 'id|title|alt|text', $link
);
}
$linkElement->click();
// Allow the page finish loading in some browsers.
$this->getSession()->wait(3000, "document.readyState == 'complete';");
}
/**
* Pauses the scenario until the user presses a key. Useful when debugging a scenario.
*
* @Then /^(?:|I )break$/
*/
public function iPutABreakpoint() {
fwrite(STDOUT, "\033[s \033[93m[Breakpoint] Press \033[1;93m[RETURN]\033[0;93m to continue...\033[0m");
while (fgets(STDIN, 1024) == '') {}
fwrite(STDOUT, "\033[u");
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment