From fb99ef1ded70a7ae7d4f093044b891f07ce73c8d Mon Sep 17 00:00:00 2001
From: David Monllao <davidm@moodle.com>
Date: Thu, 12 Dec 2013 12:18:01 +0800
Subject: [PATCH] MDL-42625 behat: Fixing failures

* Expanding fieldsets before setting the course format
* Ensure editors are loaded moved to expand_all_fieldsets
* Return true rather than false when JS is disabled and
  non-allowed steps should continue without throwing an
  exception.
* Submit create user form after editing it
---
 admin/tests/behat/display_short_names.feature |  1 -
 course/tests/behat/behat_course.php           |  2 +-
 lib/tests/behat/behat_forms.php               |  6 +++---
 lib/tests/behat/behat_general.php             | 21 +++----------------
 lib/tests/behat/behat_navigation.php          |  4 ++--
 user/tests/behat/edituserpassword.feature     |  2 ++
 6 files changed, 11 insertions(+), 25 deletions(-)

diff --git a/admin/tests/behat/display_short_names.feature b/admin/tests/behat/display_short_names.feature
index 76cfda0d94b..b5d609644bf 100644
--- a/admin/tests/behat/display_short_names.feature
+++ b/admin/tests/behat/display_short_names.feature
@@ -16,7 +16,6 @@ Feature: Display extended course names
 
   Scenario: Courses list with extended course names
     Given I expand "Site administration" node
-    And I expand "Appearance" node
     And I click on "Courses" "link" in the "//div[@id='settingsnav']/descendant::li[contains(concat(' ', normalize-space(@class), ' '), ' type_setting ')][contains(., 'Appearance')]" "xpath_element"
     And I check "Display extended course names"
     When I press "Save changes"
diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php
index fcceee3e115..d31c1b966a5 100644
--- a/course/tests/behat/behat_course.php
+++ b/course/tests/behat/behat_course.php
@@ -101,7 +101,7 @@ class behat_course extends behat_base {
 
             // Adding a forced wait until editors are loaded as otherwise selenium sometimes tries clicks on the
             // format field when the editor is being rendered and the click misses the field coordinates.
-            $steps[] = new Given('I wait until the editors are loaded');
+            $steps[] = new Given('I expand all fieldsets');
             $steps[] = new Given('I select "' . $formatvalue . '" from "' . $formatfield . '"');
             $steps[] = new Given('I fill the moodle form with:', $table);
         } else {
diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php
index e16fda35bf0..5468b86ef66 100644
--- a/lib/tests/behat/behat_forms.php
+++ b/lib/tests/behat/behat_forms.php
@@ -69,9 +69,6 @@ class behat_forms extends behat_base {
      */
     public function i_fill_the_moodle_form_with(TableNode $data) {
 
-        // We ensure that all the editors are loaded and we can interact with them.
-        $this->ensure_editors_are_loaded();
-
         // Expand all fields in case we have.
         $this->expand_all_fields();
 
@@ -110,6 +107,9 @@ class behat_forms extends behat_base {
      */
     protected function expand_all_fields() {
 
+        // We ensure that all the editors are loaded and we can interact with them.
+        $this->ensure_editors_are_loaded();
+
         // behat_base::find() throws an exception if there are no elements, we should not fail a test because of this.
         try {
 
diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php
index 8d11e55d5d4..476650b1fa2 100644
--- a/lib/tests/behat/behat_general.php
+++ b/lib/tests/behat/behat_general.php
@@ -77,16 +77,16 @@ class behat_general extends behat_base {
         // moodle_page::$periodicrefreshdelay possible values.
         if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) {
             // We don't fail the scenario if no redirection with message is found to avoid race condition false failures.
-            return false;
+            return true;
         }
 
         // Wrapped in try & catch in case the redirection has already been executed.
         try {
             $content = $metarefresh->getAttribute('content');
         } catch (NoSuchElement $e) {
-            return false;
+            return true;
         } catch (StaleElementReference $e) {
-            return false;
+            return true;
         }
 
         // Getting the refresh time and the url if present.
@@ -219,21 +219,6 @@ class behat_general extends behat_base {
         $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
     }
 
-    /**
-     * Waits until the editors are all completely loaded.
-     *
-     * @Given /^I wait until the editors are loaded$/
-     * @throws DriverException
-     */
-    public function wait_until_editors_are_loaded() {
-
-        if (!$this->running_javascript()) {
-            throw new DriverException('Editors are not loaded when running without Javascript support');
-        }
-
-        $this->ensure_editors_are_loaded();
-    }
-
     /**
      * Waits until the provided element selector exists in the DOM
      *
diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php
index d7de467345f..7807f3746b7 100644
--- a/lib/tests/behat/behat_navigation.php
+++ b/lib/tests/behat/behat_navigation.php
@@ -57,7 +57,7 @@ class behat_navigation extends behat_base {
                 // Administration menu is not loaded by default any more. Click the link to expand.
                 return new Given('I click on "'.$nodetext.'" "link" in the "'.get_string('administration').'" "block"');
             }
-            return false;
+            return true;
         }
 
         // Avoid problems with quotes.
@@ -91,7 +91,7 @@ class behat_navigation extends behat_base {
 
         // No collapsible nodes with non-JS browsers.
         if (!$this->running_javascript()) {
-            return false;
+            return true;
         }
 
         // Avoid problems with quotes.
diff --git a/user/tests/behat/edituserpassword.feature b/user/tests/behat/edituserpassword.feature
index c468afef7a8..706bdd0a1d5 100644
--- a/user/tests/behat/edituserpassword.feature
+++ b/user/tests/behat/edituserpassword.feature
@@ -17,3 +17,5 @@ Feature: Enable/disable password field based on authentication selected.
     And the "newpassword" "field" should be disabled
     And I select "Email-based self-registration" from "auth"
     And the "newpassword" "field" should be enabled
+    # We need to cancel/submit a form that has been modified.
+    And I press "Create user"