From 2d98e42166f0045ccbfc529c2015387942b5f581 Mon Sep 17 00:00:00 2001 From: rajesh Taneja Date: Thu, 22 Oct 2015 11:35:00 +0800 Subject: [PATCH 1/2] MDL-50163 behat: Restart session after scenario with switch_window Restart session after a scenario with switch window is finished. This will close all open browser windows and open a new browser window --- lib/tests/behat/behat_hooks.php | 12 ++++++++++++ mod/book/tests/behat/log_entries.feature | 2 +- mod/quiz/tests/behat/attempt_redo_questions.feature | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 5df77bbaac2..fe8c9455c22 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -384,6 +384,18 @@ class behat_hooks extends behat_base { } } + /** + * Executed after scenario having switch window to restart session. + * This is needed to close all extra browser windows and starting + * one browser window. + * + * @param ScenarioEvent $event event fired after scenario. + * @AfterScenario @_switch_window + */ + public function after_scenario_switchwindow(ScenarioEvent $event) { + $this->getSession()->restart(); + } + /** * Getter for self::$faildumpdirname * diff --git a/mod/book/tests/behat/log_entries.feature b/mod/book/tests/behat/log_entries.feature index 79e95d44ee9..1cb165b2753 100644 --- a/mod/book/tests/behat/log_entries.feature +++ b/mod/book/tests/behat/log_entries.feature @@ -4,7 +4,7 @@ Feature: In a book, verify log entries As an admin I need to perform various actions in a book. - @javascript + @javascript @_switch_window Scenario: perform various book actions and verify log entries. Given the following "courses" exist: | fullname | shortname | category | groupmode | diff --git a/mod/quiz/tests/behat/attempt_redo_questions.feature b/mod/quiz/tests/behat/attempt_redo_questions.feature index ae9903107e5..ce6cf09a450 100644 --- a/mod/quiz/tests/behat/attempt_redo_questions.feature +++ b/mod/quiz/tests/behat/attempt_redo_questions.feature @@ -68,7 +68,7 @@ Feature: Allow students to redo questions in a practice quiz, without starting a And I click on "Submit all and finish" "button" in the "Confirmation" "dialogue" Then "Redo question" "button" should not exist - @javascript + @javascript @_switch_window Scenario: Teachers reviewing can see all the qestions attempted in a slot When I follow "Quiz 1" And I press "Attempt quiz now" From 90c5e7f8d17f5f3afc149c6cb0090fec1dd7af96 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Tue, 17 Nov 2015 12:59:06 +0800 Subject: [PATCH 2/2] MDL-50163 behat: Return subcontext from session Creating a new context with new mink context with different session was keeping the session and was not returning proper session information --- lib/behat/classes/behat_context_helper.php | 57 ++++------------------ lib/tests/behat/behat_hooks.php | 2 +- 2 files changed, 11 insertions(+), 48 deletions(-) diff --git a/lib/behat/classes/behat_context_helper.php b/lib/behat/classes/behat_context_helper.php index cdeea57cbbc..0223495888a 100644 --- a/lib/behat/classes/behat_context_helper.php +++ b/lib/behat/classes/behat_context_helper.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Helper to initialise behat contexts from moodle code. + * Helper to get behat contexts from other contexts. * * @package core * @category test @@ -25,8 +25,7 @@ // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. -use Behat\Mink\Session as Session, - Behat\Mink\Mink as Mink; +use \Behat\Behat\Context\BehatContext; /** * Helper to get behat contexts. @@ -39,28 +38,18 @@ use Behat\Mink\Session as Session, class behat_context_helper { /** - * List of already initialized contexts. - * - * @var array + * @var BehatContext main behat context. */ - protected static $contexts = array(); + protected static $maincontext = false; /** - * @var Mink. - */ - protected static $mink = false; - - /** - * Sets the browser session. + * Save main behat context reference to be used for finding sub-contexts. * - * @param Session $session + * @param BehatContext $maincontext * @return void */ - public static function set_session(Session $session) { - - // Set mink to be able to init a context. - self::$mink = new Mink(array('mink' => $session)); - self::$mink->setDefaultSessionName('mink'); + public static function set_main_context(BehatContext $maincontext) { + self::$maincontext = $maincontext; } /** @@ -76,36 +65,10 @@ class behat_context_helper { */ public static function get($classname) { - if (!self::init_context($classname)) { + if (!$subcontext = self::$maincontext->getSubcontextByClassName($classname)) { throw coding_exception('The required "' . $classname . '" class does not exist'); } - return self::$contexts[$classname]; + return $subcontext; } - - /** - * Initializes the required context. - * - * @throws coding_exception - * @param string $classname - * @return bool - */ - protected static function init_context($classname) { - - if (!empty(self::$contexts[$classname])) { - return true; - } - - if (!class_exists($classname)) { - return false; - } - - $instance = new $classname(); - $instance->setMink(self::$mink); - - self::$contexts[$classname] = $instance; - - return true; - } - } diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index fe8c9455c22..78ea80959a9 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -256,7 +256,7 @@ class behat_hooks extends behat_base { // We need the Mink session to do it and we do it only before the first scenario. if (self::is_first_scenario()) { behat_selectors::register_moodle_selectors($session); - behat_context_helper::set_session($session); + behat_context_helper::set_main_context($event->getContext()->getMainContext()); } // Reset mink session between the scenarios.