Skip to content

Commit

Permalink
Merge pull request #134 from jhedstrom/cleanup
Browse files Browse the repository at this point in the history
Cleanup

Signed-off-by: Jonathan Hedstrom <jhedstrom@gmail.com>
  • Loading branch information
jhedstrom committed Feb 2, 2015
1 parent ef08a8a commit bdddcbb
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 90 deletions.
90 changes: 2 additions & 88 deletions src/Drupal/DrupalExtension/Context/MarkupContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class MarkupContext extends RawMinkContext {
* The machine name of the region to return.
*
* @return \Behat\Mink\Element\NodeElement
*
* @todo this should be a trait when PHP 5.3 support is dropped.
*/
public function getRegion($region) {
$session = $this->getSession();
Expand Down Expand Up @@ -53,32 +55,6 @@ public function assertRegionButton($button, $region) {
}
}

/**
* Find a heading in a specific region.
*
* @Then I should see the heading :heading in the :region( region)
* @Then I should see the :heading heading in the :region( region)
*
* @throws \Exception
* If region or header within it cannot be found.
*/
public function assertRegionHeading($heading, $region) {
$regionObj = $this->getRegion($region);

foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $tag) {
$elements = $regionObj->findAll('css', $tag);
if (!empty($elements)) {
foreach ($elements as $element) {
if (trim($element->getText()) === $heading) {
return;
}
}
}
}

throw new \Exception(sprintf('The heading "%s" was not found in the "%s" region on the page %s', $heading, $region, $this->getSession()->getCurrentUrl()));
}

/**
* @Then I( should) see the :tag element in the :region( region)
*/
Expand Down Expand Up @@ -117,68 +93,6 @@ public function assertNotRegionElementText($text, $tag, $region) {
}
}

/**
* @Then I should see the link :link in the :region( region)
*
* @throws \Exception
* If region or link within it cannot be found.
*/
public function assertLinkRegion($link, $region) {
$regionObj = $this->getRegion($region);

$result = $regionObj->findLink($link);
if (empty($result)) {
throw new \Exception(sprintf('No link to "%s" in the "%s" region on the page %s', $link, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I should not see the link :link in the :region( region)
*
* @throws \Exception
* If region or link within it cannot be found.
*/
public function assertNotLinkRegion($link, $region) {
$regionObj = $this->getRegion($region);

$result = $regionObj->findLink($link);
if (!empty($result)) {
throw new \Exception(sprintf('Link to "%s" in the "%s" region on the page %s', $link, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I should see( the text) :text in the :region( region)
*
* @throws \Exception
* If region or text within it cannot be found.
*/
public function assertRegionText($text, $region) {
$regionObj = $this->getRegion($region);

// Find the text within the region
$regionText = $regionObj->getText();
if (strpos($regionText, $text) === FALSE) {
throw new \Exception(sprintf("The text '%s' was not found in the region '%s' on the page %s", $text, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I should not see( the text) :text in the :region( region)
*
* @throws \Exception
* If region or text within it cannot be found.
*/
public function assertNotRegionText($text, $region) {
$regionObj = $this->getRegion($region);

// Find the text within the region.
$regionText = $regionObj->getText();
if (strpos($regionText, $text) !== FALSE) {
throw new \Exception(sprintf('The text "%s" was found in the region "%s" on the page %s', $text, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I( should) see the :tag element with the :attribute attribute set to :value in the :region( region)
*/
Expand Down
99 changes: 97 additions & 2 deletions src/Drupal/DrupalExtension/Context/MinkContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
namespace Drupal\DrupalExtension\Context;

use Behat\Behat\Context\TranslatableContext;

use Behat\Mink\Exception\UnsupportedDriverActionException;

use Behat\MinkExtension\Context\MinkContext as MinkExtension;

/**
* Extensions to the Mink Extension.
*/
class MinkContext extends MinkExtension implements TranslatableContext {

/**
* Returns list of definition translation resources paths.
*
* @return array
*/
public static function getTranslationResources() {
return glob(__DIR__ . '/../../../../i18n/*.xliff');
}

/**
* Return a region from the current page.
*
Expand Down Expand Up @@ -357,6 +364,94 @@ public function regionFillField($field, $value, $region) {
$regionObj->fillField($field, $value);
}

/**
* Find a heading in a specific region.
*
* @Then I should see the heading :heading in the :region( region)
* @Then I should see the :heading heading in the :region( region)
*
* @throws \Exception
* If region or header within it cannot be found.
*/
public function assertRegionHeading($heading, $region) {
$regionObj = $this->getRegion($region);

foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $tag) {
$elements = $regionObj->findAll('css', $tag);
if (!empty($elements)) {
foreach ($elements as $element) {
if (trim($element->getText()) === $heading) {
return;
}
}
}
}

throw new \Exception(sprintf('The heading "%s" was not found in the "%s" region on the page %s', $heading, $region, $this->getSession()->getCurrentUrl()));
}

/**
* @Then I should see the link :link in the :region( region)
*
* @throws \Exception
* If region or link within it cannot be found.
*/
public function assertLinkRegion($link, $region) {
$regionObj = $this->getRegion($region);

$result = $regionObj->findLink($link);
if (empty($result)) {
throw new \Exception(sprintf('No link to "%s" in the "%s" region on the page %s', $link, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I should not see the link :link in the :region( region)
*
* @throws \Exception
* If region or link within it cannot be found.
*/
public function assertNotLinkRegion($link, $region) {
$regionObj = $this->getRegion($region);

$result = $regionObj->findLink($link);
if (!empty($result)) {
throw new \Exception(sprintf('Link to "%s" in the "%s" region on the page %s', $link, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I should see( the text) :text in the :region( region)
*
* @throws \Exception
* If region or text within it cannot be found.
*/
public function assertRegionText($text, $region) {
$regionObj = $this->getRegion($region);

// Find the text within the region
$regionText = $regionObj->getText();
if (strpos($regionText, $text) === FALSE) {
throw new \Exception(sprintf("The text '%s' was not found in the region '%s' on the page %s", $text, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I should not see( the text) :text in the :region( region)
*
* @throws \Exception
* If region or text within it cannot be found.
*/
public function assertNotRegionText($text, $region) {
$regionObj = $this->getRegion($region);

// Find the text within the region.
$regionText = $regionObj->getText();
if (strpos($regionText, $text) !== FALSE) {
throw new \Exception(sprintf('The text "%s" was found in the region "%s" on the page %s', $text, $region, $this->getSession()->getCurrentUrl()));
}
}

/**
* @Then I (should )see the text :text
*/
Expand Down

0 comments on commit bdddcbb

Please sign in to comment.