From 80bf03d583c8290af390244608240cfff184e657 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 23 Jun 2020 11:37:20 +0800 Subject: [PATCH 1/2] MDL-70167 behat: Boolean attributes should either exist or not --- .../atto/tests/behat/disablecontrol.feature | 36 +++++++++---------- .../tests/behat/disablecontrol.feature | 4 +-- lib/tests/behat/behat_general.php | 33 +++++++++++++++++ 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/lib/editor/atto/tests/behat/disablecontrol.feature b/lib/editor/atto/tests/behat/disablecontrol.feature index 4e1d16a7134..893d241b3e5 100644 --- a/lib/editor/atto/tests/behat/disablecontrol.feature +++ b/lib/editor/atto/tests/behat/disablecontrol.feature @@ -19,27 +19,27 @@ Feature: Atto with enable/disable function. @javascript Scenario: Check disable Atto editor. When I set the field "mycontrol" to "Disable" - Then the "disabled" attribute of "button.atto_collapse_button" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_title_button" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_bold_button" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_italic_button" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_unorderedlist_button_insertUnorderedList" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_orderedlist_button_insertOrderedList" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_link_button" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_link_button_unlink" "css_element" should contain "disabled" - And the "disabled" attribute of "button.atto_image_button" "css_element" should contain "disabled" + Then the "disabled" attribute of "button.atto_collapse_button" "css_element" should be set + And the "disabled" attribute of "button.atto_title_button" "css_element" should be set + And the "disabled" attribute of "button.atto_bold_button" "css_element" should be set + And the "disabled" attribute of "button.atto_italic_button" "css_element" should be set + And the "disabled" attribute of "button.atto_unorderedlist_button_insertUnorderedList" "css_element" should be set + And the "disabled" attribute of "button.atto_orderedlist_button_insertOrderedList" "css_element" should be set + And the "disabled" attribute of "button.atto_link_button" "css_element" should be set + And the "disabled" attribute of "button.atto_link_button_unlink" "css_element" should be set + And the "disabled" attribute of "button.atto_image_button" "css_element" should be set And the "contenteditable" attribute of "div#id_myeditoreditable" "css_element" should contain "false" @javascript Scenario: Check enable Atto editor. When I set the field "mycontrol" to "Enable" - Then "button.atto_collapse_button[disabled]" "css_element" should not exist - And "button.atto_title_button[disabled]" "css_element" should not exist - And "button.atto_bold_button[disabled]" "css_element" should not exist - And "button.atto_italic_button[disabled]" "css_element" should not exist - And "button.atto_unorderedlist_button_insertUnorderedList[disabled]" "css_element" should not exist - And "button.atto_orderedlist_button_insertOrderedList[disabled]" "css_element" should not exist - And "button.atto_link_button[disabled]" "css_element" should not exist - And "button.atto_link_button_unlink[disabled]" "css_element" should not exist - And "button.atto_image_button[disabled]" "css_element" should not exist + Then the "disabled" attribute of "button.atto_collapse_button" "css_element" should not be set + And the "disabled" attribute of "button.atto_title_button" "css_element" should not be set + And the "disabled" attribute of "button.atto_bold_button" "css_element" should not be set + And the "disabled" attribute of "button.atto_italic_button" "css_element" should not be set + And the "disabled" attribute of "button.atto_unorderedlist_button_insertUnorderedList" "css_element" should not be set + And the "disabled" attribute of "button.atto_orderedlist_button_insertOrderedList" "css_element" should not be set + And the "disabled" attribute of "button.atto_link_button" "css_element" should not be set + And the "disabled" attribute of "button.atto_link_button_unlink" "css_element" should not be set + And the "disabled" attribute of "button.atto_image_button" "css_element" should not be set And the "contenteditable" attribute of "div#id_myeditoreditable" "css_element" should contain "true" diff --git a/lib/editor/textarea/tests/behat/disablecontrol.feature b/lib/editor/textarea/tests/behat/disablecontrol.feature index 0d823314b3c..9e482056a7f 100644 --- a/lib/editor/textarea/tests/behat/disablecontrol.feature +++ b/lib/editor/textarea/tests/behat/disablecontrol.feature @@ -23,9 +23,9 @@ Feature: Text area with enable/disable function. @javascript Scenario: Check disable Text area editor. When I set the field "mycontrol" to "Disable" - Then the "readonly" attribute of "textarea#id_myeditor" "css_element" should contain "readonly" + Then the "readonly" attribute of "textarea#id_myeditor" "css_element" should be set @javascript Scenario: Check enable Text area editor. When I set the field "mycontrol" to "Enable" - Then "textarea#id_myeditor[readonly]" "css_element" should not exist + Then the "readonly" attribute of "textarea#id_myeditor" "css_element" should not be set diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index dacbabc21c7..852e952a31e 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -1251,6 +1251,39 @@ EOF; $this->resize_window($windowsize, $windowviewport === 'viewport'); } + /** + * Checks whether there the specified attribute is set or not. + * + * @Then the :attribute attribute of :element :selectortype should be set + * @Then the :attribute attribute of :element :selectortype should :not be set + * + * @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 $not + */ + public function the_attribute_of_should_be_set($attribute, $element, $selectortype, $not = null) { + // Get the container node (exception if it doesn't exist). + $containernode = $this->get_selected_node($selectortype, $element); + $hasattribute = $containernode->hasAttribute($attribute); + + if ($not && $hasattribute) { + $value = $containernode->getAttribute($attribute); + // Should not be set but is. + throw new ExpectationException( + "The attribute \"{$attribute}\" should not be set but has a value of '{$value}'", + $this->getSession() + ); + } else if (!$not && !$hasattribute) { + // Should be set but is not. + throw new ExpectationException( + "The attribute \"{$attribute}\" should be set but is not", + $this->getSession() + ); + } + } + /** * Checks whether there is an attribute on the given element that contains the specified text. * From 6882d7399ba57014f8b433c771db45990b88d5c8 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 23 Nov 2020 09:00:13 +0800 Subject: [PATCH 2/2] MDL-70167 behat: Rewrite attribute check steps This commit updates the following steps to use the `the_attribute_of_should_be_set` step under the hood: - the [element] [type] should be disabled - the [element] [type] should be enabled - the [element] [type] should be readonly - the [element] [type] should not be readonly This reduces unnecssary code duplication. --- lib/tests/behat/behat_general.php | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 852e952a31e..ec1e3a42984 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -945,13 +945,7 @@ EOF; * @param string $selectortype The type of element where we are looking in. */ public function the_element_should_be_disabled($element, $selectortype) { - - // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement. - $node = $this->get_selected_node($selectortype, $element); - - if (!$node->hasAttribute('disabled')) { - throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession()); - } + $this->the_attribute_of_should_be_set("disabled", $element, $selectortype, false); } /** @@ -963,13 +957,7 @@ EOF; * @param string $selectortype The type of where we look */ public function the_element_should_be_enabled($element, $selectortype) { - - // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement. - $node = $this->get_selected_node($selectortype, $element); - - if ($node->hasAttribute('disabled')) { - throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession()); - } + $this->the_attribute_of_should_be_set("disabled", $element, $selectortype, true); } /** @@ -981,12 +969,7 @@ EOF; * @param string $selectortype The type of element where we are looking in. */ public function the_element_should_be_readonly($element, $selectortype) { - // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement. - $node = $this->get_selected_node($selectortype, $element); - - if (!$node->hasAttribute('readonly')) { - throw new ExpectationException('The element "' . $element . '" is not readonly', $this->getSession()); - } + $this->the_attribute_of_should_be_set("readonly", $element, $selectortype, false); } /** @@ -998,12 +981,7 @@ EOF; * @param string $selectortype The type of element where we are looking in. */ public function the_element_should_not_be_readonly($element, $selectortype) { - // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement. - $node = $this->get_selected_node($selectortype, $element); - - if ($node->hasAttribute('readonly')) { - throw new ExpectationException('The element "' . $element . '" is readonly', $this->getSession()); - } + $this->the_attribute_of_should_be_set("readonly", $element, $selectortype, true); } /**