From b3502be32095b1f4acfdf74c3e892201061cd4f0 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Wed, 23 Nov 2016 09:07:26 +0800 Subject: [PATCH 1/2] MDL-56898 behat: Ensure we have button in viewport and page is stable --- admin/tool/behat/tests/behat/manipulate_forms.feature | 1 + auth/tests/behat/behat_auth.php | 4 ++++ enrol/tests/behat/behat_enrol.php | 3 +++ 3 files changed, 8 insertions(+) diff --git a/admin/tool/behat/tests/behat/manipulate_forms.feature b/admin/tool/behat/tests/behat/manipulate_forms.feature index ed978997e4a..a26fd203efd 100644 --- a/admin/tool/behat/tests/behat/manipulate_forms.feature +++ b/admin/tool/behat/tests/behat/manipulate_forms.feature @@ -15,6 +15,7 @@ Feature: Forms manipulation Then the field "First name" matches value "Field value" And the "Select a country" select box should contain "Japan" And the field "New password" matches value "TestPass" + And I take focus off "Update profile" "button" And I press "Update profile" @javascript diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index 685503cca70..27ebd411dff 100644 --- a/auth/tests/behat/behat_auth.php +++ b/auth/tests/behat/behat_auth.php @@ -61,6 +61,10 @@ class behat_auth extends behat_base { * @Given /^I log out$/ */ public function i_log_out() { + + // Wait for page to be loaded. + $this->wait_for_pending_js(); + // Click on logout link in footer, as it's much faster. $this->execute('behat_general::i_click_on_in_the', array(get_string('logout'), 'link', '#page-footer', "css_element")); } diff --git a/enrol/tests/behat/behat_enrol.php b/enrol/tests/behat/behat_enrol.php index 281843018b8..6218b31b8dd 100644 --- a/enrol/tests/behat/behat_enrol.php +++ b/enrol/tests/behat/behat_enrol.php @@ -64,6 +64,9 @@ class behat_enrol extends behat_base { // Set form fields. $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table); + // Ensure we get button in focus, before pressing button. + $this->execute("behat_general::i_take_focus_off_field", array(get_string('addinstance', 'enrol'), "button")); + // Save changes. $this->execute("behat_forms::press_button", get_string('addinstance', 'enrol')); From 8c35e49b379d1cc58f3f0c20baa079cf177c28a4 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Wed, 23 Nov 2016 13:05:32 +0800 Subject: [PATCH 2/2] MDL-56898 behat: No need to trigger error, as it sometimes hangs behat --- lib/behat/lib.php | 14 ------------- lib/tests/behat/behat_hooks.php | 36 +++++---------------------------- 2 files changed, 5 insertions(+), 45 deletions(-) diff --git a/lib/behat/lib.php b/lib/behat/lib.php index 6dd4784999d..662a784ec18 100644 --- a/lib/behat/lib.php +++ b/lib/behat/lib.php @@ -134,13 +134,6 @@ function behat_error_handler($errno, $errstr, $errfile, $errline, $errcontext) { return true; } - // No need to report the before_scenario warning generated to clear last error. - // As error_clear_last is only available in php 7.0+, we trigger E_USER_WARNING - // to clear any last error which was generated during reset in before_scenario. - if (($errno === E_USER_WARNING) && $errstr == 'before_scenario') { - return; - } - // This error handler receives E_ALL | E_STRICT, running the behat test site the debug level is // set to DEVELOPER and will always include E_NOTICE,E_USER_NOTICE... as part of E_ALL, if the current // error_reporting() value does not include one of those levels is because it has been forced through @@ -183,13 +176,6 @@ function behat_shutdown_function() { // Ignore E_WARNING, as they might come via ( @ )suppression and might lead to false failure. if (isset($error['type']) && !($error['type'] & E_WARNING)) { - // No need to report the before_scenario warning generated to clear last error. - // As error_clear_last is only available in php 7.0+, we trigger E_USER_WARNING - // to clear any last error which was generated during reset in before_scenario. - if (($error['type'] & E_USER_WARNING) && $error['message'] == 'before_scenario') { - return; - } - $errors = behat_get_shutdown_process_errors(); $errors[] = $error; diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index e1be9fdb586..014f7c6f6d1 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -103,13 +103,6 @@ class behat_hooks extends behat_base { */ protected static $runningsuite = ''; - /** - * Keeps track of php error generated during reset. - * - * @var int keep track of how many php errors were generated. - */ - public static $phperrorduringresetcounter = 0; - /** * Hook to capture BeforeSuite event so as to give access to moodle codebase. * This will try and catch any exception and exists if anything fails. @@ -346,30 +339,12 @@ class behat_hooks extends behat_base { // Reset $SESSION. \core\session\manager::init_empty_session(); - // Set custom handler to try reset all data, if failed because of previous ajax. - set_error_handler( - function($errno, $errstr, $errfile, $errline) { - behat_hooks::$phperrorduringresetcounter++; - if (behat_hooks::$phperrorduringresetcounter < self::TIMEOUT) { - sleep(1); - behat_util::reset_all_data(); - } - return true; - }, -1 & ~E_NOTICE & ~E_WARNING); + // Ignore E_NOTICE and E_WARNING during reset, as this might be caused because of some existing process + // running ajax. This will be investigated in another issue. + $errorlevel = error_reporting(); + error_reporting($errorlevel & ~E_NOTICE & ~E_WARNING); behat_util::reset_all_data(); - restore_error_handler(); - - // Trigger an error which will be ignored by behat_shutdown_function, this is hacky way to clear last error in php < 7.0. - if (self::$phperrorduringresetcounter > 0) { - if (function_exists('error_clear_last')) { - error_clear_last(); - } else { - trigger_error('before_scenario', E_USER_WARNING); - } - } - - // Reset the counter here, as this won't be required. - self::$phperrorduringresetcounter = 0; + error_reporting($errorlevel); // Assign valid data to admin user (some generator-related code needs a valid user). $user = $DB->get_record('user', array('username' => 'admin')); @@ -422,7 +397,6 @@ class behat_hooks extends behat_base { public function after_scenario(AfterScenarioScope $scope) { try { $this->wait_for_pending_js(); - $this->getSession()->visit($this->locate_path('/README.txt')); $this->getSession()->reset(); } catch (DriverException $e) { // Try restart session, if DriverException caught.