Improved region support, added "see text in region" step

This commit is contained in:
Ingo Schommer 2014-04-09 10:31:41 +12:00
parent 6a7df2f702
commit 2ec760cb1d
2 changed files with 59 additions and 9 deletions

View File

@ -563,14 +563,19 @@ JS;
}
/**
* Clicks a link via a configuarable css selector in the behat.yml configuration
* Example: Given I follow "Select" in the "" region
* Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute,
* or a named region mapped to a CSS selector via Behat configuration).
*
* @Given /^I (?:follow|click) "(?P<link>[^"]*)" in the "(?P<region>[^"]*)"(?:| region)$/
* Example: Given I follow "Select" in the "header .login-form" region
* Example: Given I follow "Select" in the "My Login Form" region
*
* @Given /^I (?:follow|click) "(?P<link>[^"]*)" in the "(?P<region>[^"]*)" region$/
*/
public function iFollowInTheRegion($link, $region) {
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj);
$linkObj = $regionObj->findLink($link);
if (empty($linkObj)) {
throw new \Exception(sprintf('The link "%s" was not found in the region "%s" on the page %s', $link, $region, $this->getSession()->getCurrentUrl()));
@ -579,4 +584,37 @@ JS;
$linkObj->click();
}
/**
* Asserts text in a specific region (an element identified by a CSS selector, a "data-title" attribute,
* or a named region mapped to a CSS selector via Behat configuration).
* Supports regular expressions in text value.
*
* Example: Given I should see "My Text" in the "header .login-form" region
* Example: Given I should not see "My Text" in the "My Login Form" region
*
* @Given /^I should (?P<negate>(?:(not |)))see "(?P<text>[^"]*)" in the "(?P<region>[^"]*)" region$/
*/
public function iSeeTextInRegion($negate, $text, $region) {
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj);
$actual = $regionObj->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
$regex = '/'.preg_quote($text, '/').'/ui';
if(trim($negate)) {
if (preg_match($regex, $actual)) {
$message = sprintf('The text "%s" was found in the text of the "%s" region.', $text, $region);
throw new \Exception($message, $this->session);
}
} else {
if (!preg_match($regex, $actual)) {
$message = sprintf('The text "%s" was not found anywhere in the text of the "%s" region.', $text, $region);
throw new \Exception($message, $this->session);
}
}
}
}

View File

@ -129,22 +129,34 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
}
/**
* Returns MinkElement based off region defined in .yml file
* Returns MinkElement based off region defined in .yml file.
* Also supports direct CSS selectors and regions identified by a "data-title" attribute.
*
* @param String $region Region name or CSS selector
* @return MinkElement|null
*/
public function getRegionObj($region) {
$key = $this->regionMap[$region];
// Try to find regions directly by CSS selector
$regionObj = $this->getSession()->getPage()->find('css', $region);
if($regionObj) {
return $regionObj;
}
if(!$this->regionMap){
// Fall back to region identified by data-tilte
$regionObj = $this->getSession()->getPage()->find('css', '[data-title="' . $region . '"]');
if($regionObj) {
return $regionObj;
}
// Look for named region
if(!isset($this->regionMap[$region])) {
throw new \LogicException("Cannot find 'region_map' in the behat.yml");
}
$key = $this->regionMap[$region];
if(!$key) {
throw new \LogicException("Cannot find the specified region in the behat.yml");
}
$regionObj = $this->getSession()->getPage()->find('css', $key);
if(!$regionObj) {
throw new ElementNotFoundException("Cannot find the specified region on the page");
}