Skip to content
Browse files

MDL-44891 Behat: Add step to check attribute

This commit creates a Behat step of the form:

Then the "title" attribute of "Toggle visibility" "button"
should contain "Show"

This is useful because unless I missed something there is currently
no step that checks attribute values; this seems like a simple
generic step. Behat is intended to check user-visible effects in
the resulting HTML, and sometimes these effects are implemented
using attribute values (two common examples are the alt= attribute,
which is visible to screenreader users, and the title= attribute,
which is visible to all users; there's also the src attribute of
images, and lots of other possibilities too).
  • Loading branch information...
1 parent 8911797 commit 90761f1a8cb540a99539b2c35083d8e79ce56972 @sammarshallou sammarshallou committed Mar 27, 2014
Showing with 24 additions and 0 deletions.
  1. +24 −0 lib/tests/behat/behat_general.php
View
24 lib/tests/behat/behat_general.php
@@ -951,4 +951,28 @@ public function should_not_exist_in_the($element, $selectortype, $containereleme
return;
}
}
+
+ /**
+ * Checks whether there is an attribute on the given element that contains the specified text.
+ *
+ * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should contain "(?P<text_string>(?:[^"]|\\")*)"$/
+ * @throws ExpectationException
+ * @param string $attribute Name of attribute
+ * @param string $element The locator of the specified selector
+ * @param string $selectortype The selector type
+ * @param string $text Expected substring
+ */
+ public function the_attribute_of_should_contain($attribute, $element, $selectortype, $text) {
+ // Get the container node (exception if it doesn't exist).
+ $containernode = $this->get_selected_node($selectortype, $element);
+ $value = $containernode->getAttribute($attribute);
+ if ($value == null) {
+ throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
+ $this->getSession());
+ } else if (strpos($value, $text) === false) {
+ throw new ExpectationException('The attribute "' . $attribute .
+ '" does not contain "' . $text . '" (actual value: "' . $value . '")',
+ $this->getSession());
+ }
+ }
}

0 comments on commit 90761f1

Please sign in to comment.
Something went wrong with that request. Please try again.