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).
This commit is contained in:
sam marshall 2014-03-27 18:44:29 +00:00
parent 89117976b8
commit 90761f1a8c

View file

@ -951,4 +951,28 @@ class behat_general extends behat_base {
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());
}
}
}