From 5458ab3e2acb27b7473352757b4a0ff1fc5b6d64 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 27 Sep 2013 10:51:42 +0800 Subject: [PATCH 1/2] MDL-42013 behat: Fixing problem with should not see Using 'should see' outputs rather than an incorrect xpath query. Also our custom selectors are not properly used by WebAssert so switching to find xpath. --- lib/tests/behat/behat_general.php | 52 ++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index f898c2618ee..619e7399f2c 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -285,7 +285,7 @@ class behat_general extends behat_base { public function assert_page_contains_text($text) { $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); - $xpath = "/descendant::*[contains(., $xpathliteral)]"; + $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]"; // Wait until it finds the text, otherwise custom exception. try { @@ -304,45 +304,69 @@ class behat_general extends behat_base { */ public function assert_page_not_contains_text($text) { - $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); - $xpath = "/descendant::*[not(contains(., $xpathliteral))]"; - - // Wait until it finds the text, otherwise custom exception. + // Delegating the process to assert_page_contains_text. try { - $this->find('xpath', $xpath); - } catch (ElementNotFoundException $e) { - throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession()); + $this->assert_page_contains_text($text); + } catch (ExpectationException $e) { + // It should not appear, so this is good. + return; } + + // If the page contains the text this is failing. + throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession()); } /** * Checks, that element with specified CSS selector or XPath contains specified text. * * @Then /^I should see "(?P(?:[^"]|\\")*)" in the "(?P(?:[^"]|\\")*)" "(?P[^"]*)"$/ + * @throws ElementNotFoundException + * @throws ExpectationException * @param string $text * @param string $element Element we look in. * @param string $selectortype The type of element where we are looking in. */ public function assert_element_contains_text($text, $element, $selectortype) { - // Transforming from steps definitions selector/locator format to Mink format. - list($selector, $locator) = $this->transform_text_selector($selectortype, $element); - $this->assertSession()->elementTextContains($selector, $locator, $text); + // Getting the container where the text should be found. + $container = $this->get_selected_node($selectortype, $element); + + $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); + $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]"; + + // Wait until it finds the text inside the container, otherwise custom exception. + try { + $this->find('xpath', $xpath, false, $container); + } catch (ElementNotFoundException $e) { + throw new ExpectationException('"' . $text . '" text was not found in the ' . $element . ' element', $this->getSession()); + } + } /** * Checks, that element with specified CSS selector or XPath doesn't contain specified text. * * @Then /^I should not see "(?P(?:[^"]|\\")*)" in the "(?P(?:[^"]|\\")*)" "(?P[^"]*)"$/ + * @throws ElementNotFoundException + * @throws ExpectationException * @param string $text * @param string $element Element we look in. * @param string $selectortype The type of element where we are looking in. */ public function assert_element_not_contains_text($text, $element, $selectortype) { - // Transforming from steps definitions selector/locator format to mink format. - list($selector, $locator) = $this->transform_text_selector($selectortype, $element); - $this->assertSession()->elementTextNotContains($selector, $locator, $text); + // Delegating the process to assert_element_contains_text. + try { + $this->assert_element_contains_text($text, $element, $selectortype); + } catch (ExpectationException $e) { + // It should not appear, so this is good. + // We only catch ExpectationException as ElementNotFoundException + // will be thrown if the container does not exist. + return; + } + + // If the element contains the text this is failing. + throw new ExpectationException('"' . $text . '" text was found in the ' . $element . ' element', $this->getSession()); } /** From 8b30d05557bacc3f2591ca4409e3ab3c84fc1e77 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 30 Sep 2013 15:49:26 +0800 Subject: [PATCH 2/2] MDL-42013 behat: Fixing related tests We had no false positives because of this issue, but some checking have been restricted to specific DOM parts. --- admin/tool/behat/tests/behat/basic_actions.feature | 4 ++-- admin/tool/behat/tests/behat/manipulate_forms.feature | 2 +- mod/forum/tests/behat/edit_post_student.feature | 4 ++-- repository/tests/behat/delete_files.feature | 2 ++ 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/admin/tool/behat/tests/behat/basic_actions.feature b/admin/tool/behat/tests/behat/basic_actions.feature index 4ec803d8e21..364b54e6bf3 100644 --- a/admin/tool/behat/tests/behat/basic_actions.feature +++ b/admin/tool/behat/tests/behat/basic_actions.feature @@ -36,7 +36,7 @@ Feature: Page contents assertions And I log in as "admin" And I follow "Course 1" When I click on "Move this to the dock" "button" in the "Administration" "block" - Then I should not see "Question bank" + Then I should not see "Question bank" in the "region-pre" "region" And I click on "//div[@id='dock']/descendant::h2[normalize-space(.)='Administration']" "xpath_element" @javascript @@ -46,4 +46,4 @@ Feature: Page contents assertions | Course 1 | C1 | 0 | And I log in as "admin" When I click on "Move this to the dock" "button" in the "Administration" "block" - Then I should not see "Turn editing on" + Then I should not see "Turn editing on" in the "region-pre" "region" diff --git a/admin/tool/behat/tests/behat/manipulate_forms.feature b/admin/tool/behat/tests/behat/manipulate_forms.feature index 4bf47d0426d..2b9ed4620d2 100644 --- a/admin/tool/behat/tests/behat/manipulate_forms.feature +++ b/admin/tool/behat/tests/behat/manipulate_forms.feature @@ -32,5 +32,5 @@ Feature: Forms manipulation Then I should see "Close the quiz" And I should see "Group mode" And I should see "Grouping" - And I should not see "Show more..." + And I should not see "Show more..." in the "region-main-box" "region" And I should see "Show less..." diff --git a/mod/forum/tests/behat/edit_post_student.feature b/mod/forum/tests/behat/edit_post_student.feature index e4b2ae4a2ac..4b3afcd72a7 100644 --- a/mod/forum/tests/behat/edit_post_student.feature +++ b/mod/forum/tests/behat/edit_post_student.feature @@ -56,5 +56,5 @@ Feature: Students can edit or delete their forum posts within a set time limit Scenario: Time limit expires When I wait "70" seconds And I follow "Forum post subject" - Then I should not see "Edit" - And I should not see "Delete" + Then I should not see "Edit" in the "region-main" "region" + And I should not see "Delete" in the "region-main" "region" diff --git a/repository/tests/behat/delete_files.feature b/repository/tests/behat/delete_files.feature index a983abe3df7..c6bb5ff5be6 100644 --- a/repository/tests/behat/delete_files.feature +++ b/repository/tests/behat/delete_files.feature @@ -13,7 +13,9 @@ Feature: Delete files and folders from the file manager And I create "Delete me" folder in "Files" filepicker And I press "Save changes" When I delete "empty.txt" from "Files" filepicker + And I press "Save changes" Then I should not see "empty.txt" And I delete "Delete me" from "Files" filepicker + And I press "Save changes" And I should not see "Delete me" And I press "Cancel"