mirror of
https://github.com/silverstripe/silverstripe-behat-extension
synced 2024-10-22 15:05:32 +00:00
Improved region support, added "see text in region" step
This commit is contained in:
parent
6a7df2f702
commit
2ec760cb1d
@ -563,14 +563,19 @@ JS;
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clicks a link via a configuarable css selector in the behat.yml configuration
|
* Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute,
|
||||||
* Example: Given I follow "Select" in the "" region
|
* 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) {
|
public function iFollowInTheRegion($link, $region) {
|
||||||
$context = $this->getMainContext();
|
$context = $this->getMainContext();
|
||||||
$regionObj = $context->getRegionObj($region);
|
$regionObj = $context->getRegionObj($region);
|
||||||
|
assertNotNull($regionObj);
|
||||||
|
|
||||||
$linkObj = $regionObj->findLink($link);
|
$linkObj = $regionObj->findLink($link);
|
||||||
if (empty($linkObj)) {
|
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()));
|
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();
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
* @return MinkElement|null
|
||||||
*/
|
*/
|
||||||
public function getRegionObj($region) {
|
public function getRegionObj($region) {
|
||||||
$key = $this->regionMap[$region];
|
// Try to find regions directly by CSS selector
|
||||||
|
$regionObj = $this->getSession()->getPage()->find('css', $region);
|
||||||
if(!$this->regionMap){
|
if($regionObj) {
|
||||||
throw new \LogicException("Cannot find 'region_map' in the behat.yml");
|
return $regionObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
if(!$key) {
|
||||||
throw new \LogicException("Cannot find the specified region in the behat.yml");
|
throw new \LogicException("Cannot find the specified region in the behat.yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
$regionObj = $this->getSession()->getPage()->find('css', $key);
|
$regionObj = $this->getSession()->getPage()->find('css', $key);
|
||||||
|
|
||||||
if(!$regionObj) {
|
if(!$regionObj) {
|
||||||
throw new ElementNotFoundException("Cannot find the specified region on the page");
|
throw new ElementNotFoundException("Cannot find the specified region on the page");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user