3 namespace Drupal\DrupalExtension\Context;
5 use Behat\MinkExtension\Context\RawMinkContext;
8 * Extensions to the Mink Extension.
10 class MarkupContext extends RawMinkContext {
13 * Return a region from the current page.
16 * If region cannot be found.
18 * @param string $region
19 * The machine name of the region to return.
21 * @return \Behat\Mink\Element\NodeElement
23 * @todo this should be a trait when PHP 5.3 support is dropped.
25 public function getRegion($region) {
26 $session = $this->getSession();
27 $regionObj = $session->getPage()->find('region', $region);
29 throw new \Exception(sprintf('No region "%s" found on the page %s.', $region, $session->getCurrentUrl()));
36 * Checks if a button with id|name|title|alt|value exists in a region
38 * @Then I should see the button :button in the :region( region)
39 * @Then I should see the :button button in the :region( region)
42 * string The id|name|title|alt|value of the button
44 * string The region in which the button should be found
47 * If region or button within it cannot be found.
49 public function assertRegionButton($button, $region) {
50 $regionObj = $this->getRegion($region);
52 $buttonObj = $regionObj->findButton($button);
53 if (empty($buttonObj)) {
54 throw new \Exception(sprintf("The button '%s' was not found in the region '%s' on the page %s", $button, $region, $this->getSession()->getCurrentUrl()));
59 * @Then I( should) see the :tag element in the :region( region)
61 public function assertRegionElement($tag, $region) {
62 $regionObj = $this->getRegion($region);
63 $elements = $regionObj->findAll('css', $tag);
64 if (!empty($elements)) {
67 throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
71 * @Then I( should) not see the :tag element in the :region( region)
73 public function assertNotRegionElement($tag, $region) {
74 $regionObj = $this->getRegion($region);
75 $result = $regionObj->findAll('css', $tag);
76 if (!empty($result)) {
77 throw new \Exception(sprintf('The element "%s" was found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
82 * @Then I( should) not see :text in the :tag element in the :region( region)
84 public function assertNotRegionElementText($text, $tag, $region) {
85 $regionObj = $this->getRegion($region);
86 $results = $regionObj->findAll('css', $tag);
87 if (!empty($results)) {
88 foreach ($results as $result) {
89 if ($result->getText() == $text) {
90 throw new \Exception(sprintf('The text "%s" was found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl()));
97 * @Then I( should) see the :tag element with the :attribute attribute set to :value in the :region( region)
99 public function assertRegionElementAttribute($tag, $attribute, $value, $region) {
100 $regionObj = $this->getRegion($region);
101 $elements = $regionObj->findAll('css', $tag);
102 if (empty($elements)) {
103 throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
105 if (!empty($attribute)) {
107 foreach ($elements as $element) {
108 $attr = $element->getAttribute($attribute);
111 if (strpos($attr, "$value") === FALSE) {
112 throw new \Exception(sprintf('The "%s" attribute does not equal "%s" on the element "%s" in the "%s" region on the page %s', $attribute, $value, $tag, $region, $this->getSession()->getCurrentUrl()));
118 throw new \Exception(sprintf('The "%s" attribute is not present on the element "%s" in the "%s" region on the page %s', $attribute, $tag, $region, $this->getSession()->getCurrentUrl()));
124 * @Then I( should) see :text in the :tag element with the :attribute attribute set to :value in the :region( region)
126 public function assertRegionElementTextAttribute($text, $tag, $attribute, $value, $region) {
127 $regionObj = $this->getRegion($region);
128 $elements = $regionObj->findAll('css', $tag);
129 if (empty($elements)) {
130 throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
134 foreach ($elements as $element) {
135 if ($element->getText() == $text) {
141 throw new \Exception(sprintf('The text "%s" was not found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl()));
144 if (!empty($attribute)) {
145 $attr = $element->getAttribute($attribute);
147 throw new \Exception(sprintf('The "%s" attribute is not present on the element "%s" in the "%s" region on the page %s', $attribute, $tag, $region, $this->getSession()->getCurrentUrl()));
149 if (strpos($attr, "$value") === FALSE) {
150 throw new \Exception(sprintf('The "%s" attribute does not equal "%s" on the element "%s" in the "%s" region on the page %s', $attribute, $value, $tag, $region, $this->getSession()->getCurrentUrl()));
156 * @Then I( should) see :text in the :tag element with the :property CSS property set to :value in the :region( region)
158 public function assertRegionElementTextCss($text, $tag, $property, $value, $region) {
159 $regionObj = $this->getRegion($region);
160 $elements = $regionObj->findAll('css', $tag);
161 if (empty($elements)) {
162 throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
166 foreach ($elements as $element) {
167 if ($element->getText() == $text) {
173 throw new \Exception(sprintf('The text "%s" was not found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl()));
177 if (!empty($property)) {
178 $style = $element->getAttribute('style');
179 $rules = explode(";", $style);
180 foreach ($rules as $rule) {
181 if (strpos($rule, $property) !== FALSE) {
182 if (strpos($rule, $value) === FALSE) {
183 throw new \Exception(sprintf('The "%s" style property does not equal "%s" on the element "%s" in the "%s" region on the page %s', $property, $value, $tag, $region, $this->getSession()->getCurrentUrl()));
190 throw new \Exception(sprintf('The "%s" style property was not found in the "%s" element in the "%s" region on the page %s', $property, $tag, $region, $this->getSession()->getCurrentUrl()));