2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Behat hooks steps definitions.
20 * This methods are used by Behat CLI command.
24 * @copyright 2012 David Monllaó
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
30 require_once(__DIR__ . '/../../behat/behat_base.php');
32 use Behat\Behat\Event\SuiteEvent as SuiteEvent,
33 Behat\Behat\Event\ScenarioEvent as ScenarioEvent,
34 Behat\Behat\Event\StepEvent as StepEvent,
35 WebDriver\Exception\NoSuchWindow as NoSuchWindow,
36 WebDriver\Exception\UnexpectedAlertOpen as UnexpectedAlertOpen,
37 WebDriver\Exception\NoAlertOpenError as NoAlertOpenError;
40 * Hooks to the behat process.
42 * Behat accepts hooks after and before each
43 * suite, feature, scenario and step.
45 * They can not call other steps as part of their process
46 * like regular steps definitions does.
48 * Throws generic Exception because they are captured by Behat.
52 * @copyright 2012 David Monllaó
53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
55 class behat_hooks extends behat_base {
58 * @var Last browser session start time.
60 protected static $lastbrowsersessionstart = 0;
63 * Gives access to moodle codebase, ensures all is ready and sets up the test lock.
65 * Includes config.php to use moodle codebase with $CFG->behat_*
66 * instead of $CFG->prefix and $CFG->dataroot, called once per suite.
72 public static function before_suite($event) {
75 // Defined only when the behat CLI command is running, the moodle init setup process will
76 // read this value and switch to $CFG->behat_dataroot and $CFG->behat_prefix instead of
78 define('BEHAT_TEST', 1);
80 define('CLI_SCRIPT', 1);
82 // With BEHAT_TEST we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
83 require_once(__DIR__ . '/../../../config.php');
85 // Now that we are MOODLE_INTERNAL.
86 require_once(__DIR__ . '/../../behat/classes/behat_command.php');
87 require_once(__DIR__ . '/../../behat/classes/util.php');
88 require_once(__DIR__ . '/../../testing/classes/test_lock.php');
89 require_once(__DIR__ . '/../../testing/classes/nasty_strings.php');
91 // Avoids vendor/bin/behat to be executed directly without test environment enabled
92 // to prevent undesired db & dataroot modifications, this is also checked
93 // before each scenario (accidental user deletes) in the BeforeScenario hook.
95 if (!behat_util::is_test_mode_enabled()) {
96 throw new Exception('Behat only can run if test mode is enabled. More info in ' . behat_command::DOCS_URL . '#Running_tests');
99 if (!behat_util::is_server_running()) {
100 throw new Exception($CFG->behat_wwwroot . ' is not available, ensure you started your PHP built-in server. More info in ' . behat_command::DOCS_URL . '#Running_tests');
103 // Prevents using outdated data, upgrade script would start and tests would fail.
104 if (!behat_util::is_test_data_updated()) {
105 $commandpath = 'php admin/tool/behat/cli/init.php';
106 throw new Exception('Your behat test site is outdated, please run ' . $commandpath . ' from your moodle dirroot to drop and install the behat test site again.');
108 // Avoid parallel tests execution, it continues when the previous lock is released.
109 test_lock::acquire('behat');
111 // Store the browser reset time if reset after N seconds is specified in config.php.
112 if (!empty($CFG->behat_restart_browser_after)) {
113 // Store the initial browser session opening.
114 self::$lastbrowsersessionstart = time();
119 * Resets the test environment.
121 * @throws coding_exception If here we are not using the test database it should be because of a coding error
124 public function before_scenario($event) {
125 global $DB, $SESSION, $CFG;
127 // As many checks as we can.
128 if (!defined('BEHAT_TEST') ||
129 !defined('BEHAT_SITE_RUNNING') ||
130 php_sapi_name() != 'cli' ||
131 !behat_util::is_test_mode_enabled() ||
132 !behat_util::is_test_site()) {
133 throw new coding_exception('Behat only can modify the test database and the test dataroot!');
136 // Avoid some notices / warnings.
137 $SESSION = new stdClass();
139 behat_util::reset_database();
140 behat_util::reset_dataroot();
143 accesslib_clear_all_caches(true);
145 // Reset the nasty strings list used during the last test.
146 nasty_strings::reset_used_strings();
148 // Assing valid data to admin user (some generator-related code needs a valid user).
149 $user = $DB->get_record('user', array('username' => 'admin'));
150 session_set_user($user);
152 // Reset the browser if specified in config.php.
153 if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) {
155 if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) {
156 $this->getSession()->restart();
157 self::$lastbrowsersessionstart = $now;
161 // Start always in the the homepage.
162 $this->getSession()->visit($this->locate_path('/'));
164 // Closing JS dialogs if present. Otherwise they would block this scenario execution.
165 if ($this->running_javascript()) {
167 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
168 } catch (NoAlertOpenError $e) {
169 // All ok, there should not be JS dialogs in theory.
176 * Ensures selenium is running.
178 * Is only executed in scenarios which requires Javascript to run,
179 * it returns a direct error message about what's going on.
182 * @BeforeScenario @javascript
184 public function before_scenario_javascript($event) {
186 // Just trying if server responds.
188 $this->getSession()->wait(0, false);
189 } catch (Exception $e) {
190 $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
191 $msg = 'Selenium server is not running, you need to start it to run tests that involves Javascript. ' . $moreinfo;
192 throw new Exception($msg);
197 * Checks that all DOM is ready.
199 * Executed only when running against a real browser.
201 * @AfterStep @javascript
203 public function after_step_javascript($event) {
205 // If it doesn't have definition or it fails there is no need to check it.
206 if ($event->getResult() != StepEvent::PASSED ||
207 !$event->hasDefinition()) {
211 // Wait until the page is ready.
212 // We are already checking that we use a JS browser, this could
213 // change in case we use another JS driver.
216 // Safari and Internet Explorer requires time between steps,
217 // otherwise Selenium tries to click in the previous page's DOM.
218 if ($this->getSession()->getDriver()->getBrowserName() == 'safari' ||
219 $this->getSession()->getDriver()->getBrowserName() == 'internet explorer') {
220 $this->getSession()->wait(self::TIMEOUT * 1000, false);
223 // With other browsers we just wait for the DOM ready.
224 $this->getSession()->wait(self::TIMEOUT * 1000, '(document.readyState === "complete")');
227 } catch (NoSuchWindow $e) {
228 // If we were interacting with a popup window it will not exists after closing it.
233 * Internal step definition to find exceptions, debugging() messages and PHP debug messages.
235 * Part of behat_hooks class as is part of the testing framework, is auto-executed
236 * after each step so no features will splicitly use it.
238 * @Given /^I look for exceptions$/
239 * @see Moodle\BehatExtension\Tester\MoodleStepTester
241 public function i_look_for_exceptions() {
243 // Wrap in try in case we were interacting with a closed window.
247 $exceptionsxpath = "//div[@data-rel='fatalerror']";
248 // Debugging messages.
249 $debuggingxpath = "//div[@data-rel='debugging']";
250 // PHP debug messages.
251 $phperrorxpath = "//div[@data-rel='phpdebugmessage']";
252 // Any other backtrace.
253 $othersxpath = "//*[contains(., ': call to ')])[1]";
255 $xpaths = array($exceptionsxpath, $debuggingxpath, $phperrorxpath, $othersxpath);
256 $joinedxpath = implode(' | ', $xpaths);
258 // Joined xpath expression. Most of the time there will be no exceptions, so this pre-check
259 // is faster than to send the 4 xpath queries for each step.
260 if (!$this->getSession()->getDriver()->find($joinedxpath)) {
265 if ($errormsg = $this->getSession()->getPage()->find('xpath', $exceptionsxpath)) {
267 // Getting the debugging info and the backtrace.
268 $errorinfoboxes = $this->getSession()->getPage()->findAll('css', 'div.notifytiny');
269 $errorinfo = $this->get_debug_text($errorinfoboxes[0]->getHtml()) . "\n" .
270 $this->get_debug_text($errorinfoboxes[1]->getHtml());
272 $msg = "Moodle exception: " . $errormsg->getText() . "\n" . $errorinfo;
273 throw new \Exception(html_entity_decode($msg));
276 // Debugging messages.
277 if ($debuggingmessages = $this->getSession()->getPage()->findAll('xpath', $debuggingxpath)) {
279 foreach ($debuggingmessages as $debuggingmessage) {
280 $msgs[] = $this->get_debug_text($debuggingmessage->getHtml());
282 $msg = "debugging() message/s found:\n" . implode("\n", $msgs);
283 throw new \Exception(html_entity_decode($msg));
286 // PHP debug messages.
287 if ($phpmessages = $this->getSession()->getPage()->findAll('xpath', $phperrorxpath)) {
290 foreach ($phpmessages as $phpmessage) {
291 $msgs[] = $this->get_debug_text($phpmessage->getHtml());
293 $msg = "PHP debug message/s found:\n" . implode("\n", $msgs);
294 throw new \Exception(html_entity_decode($msg));
297 // Any other backtrace.
298 // First looking through xpath as it is faster than get and parse the whole page contents,
299 // we get the contents and look for matches once we found something to suspect that there is a backtrace.
300 if ($this->getSession()->getDriver()->find($othersxpath)) {
301 $backtracespattern = '/(line [0-9]* of [^:]*: call to [\->&;:a-zA-Z_\x7f-\xff][\->&;:a-zA-Z0-9_\x7f-\xff]*)/';
302 if (preg_match_all($backtracespattern, $this->getSession()->getPage()->getContent(), $backtraces)) {
304 foreach ($backtraces[0] as $backtrace) {
305 $msgs[] = $backtrace . '()';
307 $msg = "Other backtraces found:\n" . implode("\n", $msgs);
308 throw new \Exception(htmlentities($msg));
312 } catch (NoSuchWindow $e) {
313 // If we were interacting with a popup window it will not exists after closing it.
314 } catch (UnexpectedAlertOpen $e) {
315 // We fail the scenario if we find an opened JS alert/confirm, in most of the cases it
316 // will be there because we are leaving an edited form without submitting/cancelling
317 // it, but moodle is using JS confirms and we can not just cancel the JS dialog
318 // as in some cases (delete activity with JS enabled for example) the test writer should
319 // use extra steps to deal with moodle's behaviour.
320 throw new Exception('Modal window present. Ensure there are no edited forms pending to submit/cancel.');
325 * Converts HTML tags to line breaks to display the info in CLI
327 * @param string $html
330 protected function get_debug_text($html) {
332 // Replacing HTML tags for new lines and keeping only the text.
333 $notags = preg_replace('/<+\s*\/*\s*([A-Z][A-Z0-9]*)\b[^>]*\/*\s*>*/i', "\n", $html);
334 return preg_replace("/(\n)+/s", "\n", $notags);