/** * We print the site info + driver used and OS. * * At this point behat_hooks::before_suite() already * ran, so we have $CFG and family. * * @param SuiteEvent $event * @return void */ public function beforeSuite(SuiteEvent $event) { global $CFG; require_once $CFG->dirroot . '/lib/behat/classes/util.php'; $browser = \Moodle\BehatExtension\Driver\MoodleSelenium2Driver::getBrowser(); // Calling all directly from here as we avoid more behat framework extensions. $runinfo = \behat_util::get_site_info(); $runinfo .= 'Server OS "' . PHP_OS . '"' . ', Browser: "' . $browser . '"' . PHP_EOL; $runinfo .= 'Behat specific fixes have been applied. See http://docs.moodle.org/dev/Acceptance_testing#Browser_specific_fixes' . PHP_EOL; $runinfo .= 'Started at ' . date('d-m-Y, H:i', time()); $this->writeln($runinfo); }
/** * Resets the test environment. * * @param BeforeScenarioScope $scope scope passed by event fired before scenario. * @throws behat_stop_exception If here we are not using the test database it should be because of a coding error */ public function before_scenario(BeforeScenarioScope $scope) { global $DB, $CFG; // As many checks as we can. if (!defined('BEHAT_TEST') || !defined('BEHAT_SITE_RUNNING') || php_sapi_name() != 'cli' || !behat_util::is_test_mode_enabled() || !behat_util::is_test_site()) { throw new behat_stop_exception('Behat only can modify the test database and the test dataroot!'); } $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests'; $driverexceptionmsg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo; try { $session = $this->getSession(); } catch (CurlExec $e) { // Exception thrown by WebDriver, so only @javascript tests will be caugth; in // behat_util::check_server_status() we already checked that the server is running. throw new behat_stop_exception($driverexceptionmsg); } catch (DriverException $e) { throw new behat_stop_exception($driverexceptionmsg); } catch (UnknownError $e) { // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions. throw new behat_stop_exception($e->getMessage()); } $suitename = $scope->getSuite()->getName(); // Register behat selectors for theme, if suite is changed. We do it for every suite change. if ($suitename !== self::$runningsuite) { behat_context_helper::set_environment($scope->getEnvironment()); // We need the Mink session to do it and we do it only before the first scenario. $namedpartialclass = 'behat_partial_named_selector'; $namedexactclass = 'behat_exact_named_selector'; if ($suitename !== 'default') { // If override selector exist, then set it as default behat selectors class. $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_partial', true); if (class_exists($overrideclass)) { $namedpartialclass = $overrideclass; } // If override selector exist, then set it as default behat selectors class. $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_exact', true); if (class_exists($overrideclass)) { $namedexactclass = $overrideclass; } } $this->getSession()->getSelectorsHandler()->registerSelector('named_partial', new $namedpartialclass()); $this->getSession()->getSelectorsHandler()->registerSelector('named_exact', new $namedexactclass()); } // Reset mink session between the scenarios. $session->reset(); // Reset $SESSION. \core\session\manager::init_empty_session(); behat_util::reset_all_data(); // Assign valid data to admin user (some generator-related code needs a valid user). $user = $DB->get_record('user', array('username' => 'admin')); \core\session\manager::set_user($user); // Reset the browser if specified in config.php. if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) { $now = time(); if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) { $session->restart(); self::$lastbrowsersessionstart = $now; } } // Set the theme if not default. if ($suitename !== "default") { set_config('theme', $suitename); self::$runningsuite = $suitename; } // Start always in the the homepage. try { // Let's be conservative as we never know when new upstream issues will affect us. $session->visit($this->locate_path('/')); } catch (UnknownError $e) { throw new behat_stop_exception($e->getMessage()); } // Checking that the root path is a Moodle test site. if (self::is_first_scenario()) { $notestsiteexception = new behat_stop_exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' . 'ensure you started the built-in web server in the correct directory or your web server is correctly started and set up'); $this->find("xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']", $notestsiteexception); self::$initprocessesfinished = true; } // Run all test with medium (1024x768) screen size, to avoid responsive problems. $this->resize_window('medium'); }
// Run command (only one per time). if ($options['install']) { behat_util::install_site(); mtrace("Acceptance tests site installed"); } else { if ($options['drop']) { // Ensure no tests are running. test_lock::acquire('behat'); behat_util::drop_site(); mtrace("Acceptance tests site dropped"); } else { if ($options['enable']) { behat_util::start_test_mode(); $runtestscommand = behat_command::get_behat_command(true) . ' --config ' . behat_config_manager::get_behat_cli_config_filepath(); mtrace("Acceptance tests environment enabled, to run the tests use:\n " . $runtestscommand); } else { if ($options['disable']) { behat_util::stop_test_mode(); mtrace("Acceptance tests environment disabled"); } else { if ($options['diag']) { $code = behat_util::get_behat_status(); exit($code); } else { echo $help; } } } } } exit(0);
/** * Resets the test environment. * * @throws coding_exception If here we are not using the test database it should be because of a coding error * @BeforeScenario */ public function before_scenario($event) { global $DB, $SESSION, $CFG; // As many checks as we can. if (!defined('BEHAT_TEST') || !defined('BEHAT_SITE_RUNNING') || php_sapi_name() != 'cli' || !behat_util::is_test_mode_enabled() || !behat_util::is_test_site()) { throw new coding_exception('Behat only can modify the test database and the test dataroot!'); } $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests'; $driverexceptionmsg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo; try { $session = $this->getSession(); } catch (CurlExec $e) { // Exception thrown by WebDriver, so only @javascript tests will be caugth; in // behat_util::is_server_running() we already checked that the server is running. throw new Exception($driverexceptionmsg); } catch (DriverException $e) { throw new Exception($driverexceptionmsg); } catch (UnknownError $e) { // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions. $this->throw_unknown_exception($e); } // 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); } // Reset $SESSION. \core\session\manager::init_empty_session(); behat_util::reset_database(); behat_util::reset_dataroot(); accesslib_clear_all_caches(true); // Reset the nasty strings list used during the last test. nasty_strings::reset_used_strings(); // Assign valid data to admin user (some generator-related code needs a valid user). $user = $DB->get_record('user', array('username' => 'admin')); \core\session\manager::set_user($user); // Reset the browser if specified in config.php. if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) { $now = time(); if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) { $session->restart(); self::$lastbrowsersessionstart = $now; } } // Start always in the the homepage. try { // Let's be conservative as we never know when new upstream issues will affect us. $session->visit($this->locate_path('/')); } catch (UnknownError $e) { $this->throw_unknown_exception($e); } // Checking that the root path is a Moodle test site. if (self::is_first_scenario()) { $notestsiteexception = new Exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' . 'ensure you started the built-in web server in the correct directory or your web server is correctly started and set up'); $this->find("xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']", $notestsiteexception); self::$initprocessesfinished = true; } // Run all test with medium (1024x768) screen size, to avoid responsive problems. $this->resize_window('medium'); }
// This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * Display environment used for running behat. * * This file is used for behat testing to ensure cli and apache * version of environment is same. * * @package tool_behat * @copyright 2016 onwards Rajesh Taneja * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once __DIR__ . '/../../../../../../config.php'; // Only continue for behat site. defined('BEHAT_SITE_RUNNING') || die; require_once $CFG->libdir . '/behat/classes/util.php'; echo json_encode(behat_util::get_environment(), true);
/** * Resets the test environment. * * @throws coding_exception If here we are not using the test database it should be because of a coding error * @BeforeScenario */ public function before_scenario($event) { global $DB, $SESSION, $CFG; // As many checks as we can. if (!defined('BEHAT_TEST') || !defined('BEHAT_SITE_RUNNING') || php_sapi_name() != 'cli' || !behat_util::is_test_mode_enabled() || !behat_util::is_test_site()) { throw new coding_exception('Behat only can modify the test database and the test dataroot!'); } // Avoid some notices / warnings. $SESSION = new stdClass(); behat_util::reset_database(); behat_util::reset_dataroot(); purge_all_caches(); accesslib_clear_all_caches(true); // Reset the nasty strings list used during the last test. nasty_strings::reset_used_strings(); // Assing valid data to admin user (some generator-related code needs a valid user). $user = $DB->get_record('user', array('username' => 'admin')); session_set_user($user); // Start always in the the homepage. $this->getSession()->visit($this->locate_path('/')); }
cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); } // Behat utilities. require_once $CFG->libdir . '/behat/classes/util.php'; require_once $CFG->libdir . '/behat/classes/behat_command.php'; // Run command (only one per time). if ($options['install']) { behat_util::install_site(); mtrace("Acceptance tests site installed"); } else { if ($options['drop']) { // Ensure no tests are running. test_lock::acquire('behat'); behat_util::drop_site(); mtrace("Acceptance tests site dropped"); } else { if ($options['enable']) { behat_util::start_test_mode(); $runtestscommand = behat_command::get_behat_command() . ' --config ' . $CFG->behat_dataroot . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'behat.yml'; mtrace("Acceptance tests environment enabled, to run the tests use:\n " . $runtestscommand); } else { if ($options['disable']) { behat_util::stop_test_mode(); mtrace("Acceptance tests environment disabled"); } else { echo $help; } } } } exit(0);