From 90761f1a8cb540a99539b2c35083d8e79ce56972 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Thu, 27 Mar 2014 18:44:29 +0000 Subject: [PATCH] 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). --- lib/tests/behat/behat_general.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index bae9c66abca..bc9432e7093 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -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 of "(?P(?:[^"]|\\")*)" "(?P[^"]*)" should contain "(?P(?:[^"]|\\")*)"$/ + * @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()); + } + } }