From 2ec760cb1d1d7a844aae88e73b527b305cd3c27f Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 9 Apr 2014 10:31:41 +1200 Subject: [PATCH] Improved region support, added "see text in region" step --- .../BehatExtension/Context/BasicContext.php | 44 +++++++++++++++++-- .../Context/SilverStripeContext.php | 24 +++++++--- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/SilverStripe/BehatExtension/Context/BasicContext.php b/src/SilverStripe/BehatExtension/Context/BasicContext.php index 543255f..7764f43 100644 --- a/src/SilverStripe/BehatExtension/Context/BasicContext.php +++ b/src/SilverStripe/BehatExtension/Context/BasicContext.php @@ -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[^"]*)" in the "(?P[^"]*)"(?:| 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[^"]*)" in the "(?P[^"]*)" 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(?:(not |)))see "(?P[^"]*)" in the "(?P[^"]*)" 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); + } + } + + } + } diff --git a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php index a54fd5c..d58e913 100644 --- a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php +++ b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php @@ -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"); }