weekly release 3.9+
[moodle.git] / lib / tests / behat / behat_hooks.php
CommitLineData
f5ceb6c2 1<?php
f5ceb6c2
DM
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Behat hooks steps definitions.
19 *
20 * This methods are used by Behat CLI command.
21 *
22 * @package core
23 * @category test
24 * @copyright 2012 David Monllaó
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
29
30require_once(__DIR__ . '/../../behat/behat_base.php');
31
42ad096f
RT
32use Behat\Testwork\Hook\Scope\BeforeSuiteScope,
33 Behat\Testwork\Hook\Scope\AfterSuiteScope,
34 Behat\Behat\Hook\Scope\BeforeFeatureScope,
35 Behat\Behat\Hook\Scope\AfterFeatureScope,
36 Behat\Behat\Hook\Scope\BeforeScenarioScope,
37 Behat\Behat\Hook\Scope\AfterScenarioScope,
38 Behat\Behat\Hook\Scope\BeforeStepScope,
39 Behat\Behat\Hook\Scope\AfterStepScope,
bee40dbf 40 Behat\Mink\Exception\ExpectationException,
89cf999a 41 Behat\Mink\Exception\DriverException as DriverException,
870349ee
DM
42 WebDriver\Exception\NoSuchWindow as NoSuchWindow,
43 WebDriver\Exception\UnexpectedAlertOpen as UnexpectedAlertOpen,
3b7d3fb8
DM
44 WebDriver\Exception\UnknownError as UnknownError,
45 WebDriver\Exception\CurlExec as CurlExec,
870349ee 46 WebDriver\Exception\NoAlertOpenError as NoAlertOpenError;
f5ceb6c2
DM
47
48/**
49 * Hooks to the behat process.
50 *
51 * Behat accepts hooks after and before each
52 * suite, feature, scenario and step.
53 *
54 * They can not call other steps as part of their process
55 * like regular steps definitions does.
56 *
57 * Throws generic Exception because they are captured by Behat.
58 *
59 * @package core
60 * @category test
61 * @copyright 2012 David Monllaó
62 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
63 */
64class behat_hooks extends behat_base {
65
95b43d6b
DM
66 /**
67 * @var For actions that should only run once.
68 */
69 protected static $initprocessesfinished = false;
70
bee40dbf
AN
71 /**
72 * @var bool Scenario running
73 */
74 protected $scenariorunning = false;
75
f49cede7
DM
76 /**
77 * Some exceptions can only be caught in a before or after step hook,
78 * they can not be thrown there as they will provoke a framework level
79 * failure, but we can store them here to fail the step in i_look_for_exceptions()
80 * which result will be parsed by the framework as the last step result.
81 *
82 * @var Null or the exception last step throw in the before or after hook.
83 */
84 protected static $currentstepexception = null;
85
5c0dfe32 86 /**
a964ead0 87 * If we are saving any kind of dump on failure we should use the same parent dir during a run.
5c0dfe32
DM
88 *
89 * @var The parent dir name
90 */
a964ead0 91 protected static $faildumpdirname = false;
5c0dfe32 92
3c71c15c
RT
93 /**
94 * Keeps track of time taken by feature to execute.
95 *
96 * @var array list of feature timings
97 */
98 protected static $timings = array();
99
5aa9b5ce
RT
100 /**
101 * Keeps track of current running suite name.
102 *
103 * @var string current running suite name
104 */
b4bc4286 105 protected static $runningsuite = '';
5aa9b5ce 106
66901a69 107 /**
108 * @var array Array (with tag names in keys) of all tags in current scenario.
109 */
110 protected static $scenariotags;
111
25fbce21
RT
112 /**
113 * Hook to capture BeforeSuite event so as to give access to moodle codebase.
114 * This will try and catch any exception and exists if anything fails.
115 *
116 * @param BeforeSuiteScope $scope scope passed by event fired before suite.
117 * @BeforeSuite
118 */
119 public static function before_suite_hook(BeforeSuiteScope $scope) {
b468dbf5
RT
120 // If behat has been initialised then no need to do this again.
121 if (self::$initprocessesfinished) {
122 return;
123 }
124
25fbce21
RT
125 try {
126 self::before_suite($scope);
127 } catch (behat_stop_exception $e) {
128 echo $e->getMessage() . PHP_EOL;
129 exit(1);
130 }
131 }
132
f5ceb6c2
DM
133 /**
134 * Gives access to moodle codebase, ensures all is ready and sets up the test lock.
135 *
136 * Includes config.php to use moodle codebase with $CFG->behat_*
137 * instead of $CFG->prefix and $CFG->dataroot, called once per suite.
138 *
25fbce21 139 * @param BeforeSuiteScope $scope scope passed by event fired before suite.
f5ceb6c2 140 * @static
25fbce21 141 * @throws behat_stop_exception
f5ceb6c2 142 */
42ad096f 143 public static function before_suite(BeforeSuiteScope $scope) {
f5ceb6c2
DM
144 global $CFG;
145
cfcbc34a
DM
146 // Defined only when the behat CLI command is running, the moodle init setup process will
147 // read this value and switch to $CFG->behat_dataroot and $CFG->behat_prefix instead of
148 // the normal site.
5aa9b5ce
RT
149 if (!defined('BEHAT_TEST')) {
150 define('BEHAT_TEST', 1);
151 }
152
153 if (!defined('CLI_SCRIPT')) {
154 define('CLI_SCRIPT', 1);
155 }
cfcbc34a 156
cfcbc34a 157 // With BEHAT_TEST we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
f5ceb6c2
DM
158 require_once(__DIR__ . '/../../../config.php');
159
160 // Now that we are MOODLE_INTERNAL.
161 require_once(__DIR__ . '/../../behat/classes/behat_command.php');
30374580 162 require_once(__DIR__ . '/../../behat/classes/behat_selectors.php');
af4830a2 163 require_once(__DIR__ . '/../../behat/classes/behat_context_helper.php');
f5ceb6c2
DM
164 require_once(__DIR__ . '/../../behat/classes/util.php');
165 require_once(__DIR__ . '/../../testing/classes/test_lock.php');
b08b0a28 166 require_once(__DIR__ . '/../../testing/classes/nasty_strings.php');
f5ceb6c2
DM
167
168 // Avoids vendor/bin/behat to be executed directly without test environment enabled
169 // to prevent undesired db & dataroot modifications, this is also checked
170 // before each scenario (accidental user deletes) in the BeforeScenario hook.
171
172 if (!behat_util::is_test_mode_enabled()) {
25fbce21 173 throw new behat_stop_exception('Behat only can run if test mode is enabled. More info in ' .
d1ac356a 174 behat_command::DOCS_URL);
f5ceb6c2
DM
175 }
176
d9c55725 177 // Reset all data, before checking for check_server_status.
ab25d8a7 178 // If not done, then it can return apache error, while running tests.
39eb8abe 179 behat_util::clean_tables_updated_by_scenario_list();
ab25d8a7
RT
180 behat_util::reset_all_data();
181
e891c838
RT
182 // Check if server is running and using same version for cli and apache.
183 behat_util::check_server_status();
f5ceb6c2 184
b831d479
DM
185 // Prevents using outdated data, upgrade script would start and tests would fail.
186 if (!behat_util::is_test_data_updated()) {
b32ca4ca 187 $commandpath = 'php admin/tool/behat/cli/init.php';
25fbce21 188 throw new behat_stop_exception("Your behat test site is outdated, please run\n\n " .
d69a6ad9 189 $commandpath . "\n\nfrom your moodle dirroot to drop and install the behat test site again.");
b831d479 190 }
f5ceb6c2
DM
191 // Avoid parallel tests execution, it continues when the previous lock is released.
192 test_lock::acquire('behat');
41eb672b 193
a964ead0 194 if (!empty($CFG->behat_faildump_path) && !is_writable($CFG->behat_faildump_path)) {
25fbce21 195 throw new behat_stop_exception('You set $CFG->behat_faildump_path to a non-writable directory');
5c0dfe32 196 }
e5f2478b
RT
197
198 // Handle interrupts on PHP7.
199 if (extension_loaded('pcntl')) {
200 $disabled = explode(',', ini_get('disable_functions'));
201 if (!in_array('pcntl_signal', $disabled)) {
202 declare(ticks = 1);
203 }
204 }
f5ceb6c2
DM
205 }
206
3c71c15c
RT
207 /**
208 * Gives access to moodle codebase, to keep track of feature start time.
209 *
25fbce21 210 * @param BeforeFeatureScope $scope scope passed by event fired before feature.
3c71c15c
RT
211 * @BeforeFeature
212 */
25fbce21 213 public static function before_feature(BeforeFeatureScope $scope) {
027212b0 214 if (!defined('BEHAT_FEATURE_TIMING_FILE')) {
3c71c15c
RT
215 return;
216 }
25fbce21 217 $file = $scope->getFeature()->getFile();
08e7f97e
TL
218 self::$timings[$file] = microtime(true);
219 }
220
3c71c15c
RT
221 /**
222 * Gives access to moodle codebase, to keep track of feature end time.
223 *
25fbce21 224 * @param AfterFeatureScope $scope scope passed by event fired after feature.
3c71c15c
RT
225 * @AfterFeature
226 */
25fbce21 227 public static function after_feature(AfterFeatureScope $scope) {
027212b0 228 if (!defined('BEHAT_FEATURE_TIMING_FILE')) {
3c71c15c
RT
229 return;
230 }
25fbce21 231 $file = $scope->getFeature()->getFile();
08e7f97e
TL
232 self::$timings[$file] = microtime(true) - self::$timings[$file];
233 // Probably didn't actually run this, don't output it.
234 if (self::$timings[$file] < 1) {
235 unset(self::$timings[$file]);
236 }
237 }
238
3c71c15c
RT
239 /**
240 * Gives access to moodle codebase, to keep track of suite timings.
241 *
25fbce21 242 * @param AfterSuiteScope $scope scope passed by event fired after suite.
3c71c15c
RT
243 * @AfterSuite
244 */
25fbce21 245 public static function after_suite(AfterSuiteScope $scope) {
027212b0 246 if (!defined('BEHAT_FEATURE_TIMING_FILE')) {
08e7f97e
TL
247 return;
248 }
249 $realroot = realpath(__DIR__.'/../../../').'/';
250 foreach (self::$timings as $k => $v) {
251 $new = str_replace($realroot, '', $k);
252 self::$timings[$new] = round($v, 1);
253 unset(self::$timings[$k]);
254 }
027212b0 255 if ($existing = @json_decode(file_get_contents(BEHAT_FEATURE_TIMING_FILE), true)) {
08e7f97e
TL
256 self::$timings = array_merge($existing, self::$timings);
257 }
258 arsort(self::$timings);
027212b0 259 @file_put_contents(BEHAT_FEATURE_TIMING_FILE, json_encode(self::$timings, JSON_PRETTY_PRINT));
08e7f97e
TL
260 }
261
f5ceb6c2 262 /**
25fbce21 263 * Hook to capture before scenario event to get scope.
f5ceb6c2 264 *
25fbce21 265 * @param BeforeScenarioScope $scope scope passed by event fired before scenario.
f5ceb6c2
DM
266 * @BeforeScenario
267 */
25fbce21
RT
268 public function before_scenario_hook(BeforeScenarioScope $scope) {
269 try {
270 $this->before_scenario($scope);
271 } catch (behat_stop_exception $e) {
272 echo $e->getMessage() . PHP_EOL;
273 exit(1);
274 }
275 }
276
277 /**
278 * Resets the test environment.
279 *
280 * @param BeforeScenarioScope $scope scope passed by event fired before scenario.
281 * @throws behat_stop_exception If here we are not using the test database it should be because of a coding error
282 */
42ad096f 283 public function before_scenario(BeforeScenarioScope $scope) {
5aa9b5ce 284 global $DB, $CFG;
f5ceb6c2
DM
285
286 // As many checks as we can.
cfcbc34a
DM
287 if (!defined('BEHAT_TEST') ||
288 !defined('BEHAT_SITE_RUNNING') ||
f5ceb6c2
DM
289 php_sapi_name() != 'cli' ||
290 !behat_util::is_test_mode_enabled() ||
cfcbc34a 291 !behat_util::is_test_site()) {
25fbce21 292 throw new behat_stop_exception('Behat only can modify the test database and the test dataroot!');
f5ceb6c2
DM
293 }
294
d1ac356a 295 $moreinfo = 'More info in ' . behat_command::DOCS_URL;
89cf999a 296 $driverexceptionmsg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo;
f59f6b6b
DM
297 try {
298 $session = $this->getSession();
98f56e6f
EL
299 if (!$session->isStarted()) {
300 $session->start();
301 }
f59f6b6b
DM
302 } catch (CurlExec $e) {
303 // Exception thrown by WebDriver, so only @javascript tests will be caugth; in
d9c55725 304 // behat_util::check_server_status() we already checked that the server is running.
25fbce21 305 throw new behat_stop_exception($driverexceptionmsg);
89cf999a 306 } catch (DriverException $e) {
25fbce21 307 throw new behat_stop_exception($driverexceptionmsg);
f59f6b6b
DM
308 } catch (UnknownError $e) {
309 // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions.
25fbce21 310 throw new behat_stop_exception($e->getMessage());
f59f6b6b
DM
311 }
312
b4bc4286
RT
313 $suitename = $scope->getSuite()->getName();
314
315 // Register behat selectors for theme, if suite is changed. We do it for every suite change.
316 if ($suitename !== self::$runningsuite) {
cd5382b2 317 self::$runningsuite = $suitename;
b4bc4286
RT
318 behat_context_helper::set_environment($scope->getEnvironment());
319
320 // We need the Mink session to do it and we do it only before the first scenario.
520071f0
AN
321 $namedpartialclass = 'behat_partial_named_selector';
322 $namedexactclass = 'behat_exact_named_selector';
323
4422ffc5
RT
324 // If override selector exist, then set it as default behat selectors class.
325 $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_partial', true);
326 if (class_exists($overrideclass)) {
327 $namedpartialclass = $overrideclass;
b4bc4286 328 }
4422ffc5
RT
329
330 // If override selector exist, then set it as default behat selectors class.
331 $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_exact', true);
332 if (class_exists($overrideclass)) {
333 $namedexactclass = $overrideclass;
334 }
335
520071f0
AN
336 $this->getSession()->getSelectorsHandler()->registerSelector('named_partial', new $namedpartialclass());
337 $this->getSession()->getSelectorsHandler()->registerSelector('named_exact', new $namedexactclass());
e99a3cca
AN
338
339 // Register component named selectors.
340 foreach (\core_component::get_component_names() as $component) {
341 $this->register_component_selectors_for_component($component);
342 }
343
17344d4c
DM
344 }
345
55f47341
RT
346 // Reset mink session between the scenarios.
347 $session->reset();
348
de230fd3 349 // Reset $SESSION.
2e00d01d 350 \core\session\manager::init_empty_session();
c8619f33 351
8c35e49b
RT
352 // Ignore E_NOTICE and E_WARNING during reset, as this might be caused because of some existing process
353 // running ajax. This will be investigated in another issue.
354 $errorlevel = error_reporting();
355 error_reporting($errorlevel & ~E_NOTICE & ~E_WARNING);
ef1d45b3 356 behat_util::reset_all_data();
8c35e49b 357 error_reporting($errorlevel);
b08b0a28 358
7af84414
AN
359 if ($this->running_javascript()) {
360 // Fetch the user agent.
361 // This isused to choose between the SVG/Non-SVG versions of themes.
362 $useragent = $this->getSession()->evaluateScript('return navigator.userAgent;');
363 \core_useragent::instance(true, $useragent);
364
365 // Restore the saved themes.
366 behat_util::restore_saved_themes();
367 }
368
de230fd3 369 // Assign valid data to admin user (some generator-related code needs a valid user).
f5ceb6c2 370 $user = $DB->get_record('user', array('username' => 'admin'));
d79d5ac2 371 \core\session\manager::set_user($user);
5f4b4e91 372
b4bc4286
RT
373 // Set the theme if not default.
374 if ($suitename !== "default") {
375 set_config('theme', $suitename);
b4bc4286
RT
376 }
377
bee40dbf
AN
378 // Reset the scenariorunning variable to ensure that Step 0 occurs.
379 $this->scenariorunning = false;
5aa9b5ce 380
66901a69 381 // Set up the tags for current scenario.
382 self::fetch_tags_for_scenario($scope);
a3892e0e 383
384 // If scenario requires the Moodle app to be running, set this up.
385 if ($this->has_tag('app')) {
386 $this->execute('behat_app::start_scenario');
d60b6af9
PFO
387
388 return;
a3892e0e 389 }
d60b6af9
PFO
390
391 // Run all test with medium (1024x768) screen size, to avoid responsive problems.
392 $this->resize_window('medium');
66901a69 393 }
394
bee40dbf
AN
395 /**
396 * Hook to open the site root before the first step in the suite.
397 * Yes, this is in a strange location and should be in the BeforeScenario hook, but failures in the test setUp lead
398 * to the test being incorrectly marked as skipped with no way to force the test to be failed.
399 *
61832fae 400 * @param BeforeStepScope $scope
bee40dbf
AN
401 * @BeforeStep
402 */
403 public function before_step(BeforeStepScope $scope) {
404 global $CFG;
405
406 if (!$this->scenariorunning) {
407 // We need to visit / before the first step in any Scenario.
408 // This is our Step 0.
409 // Ideally this would be in the BeforeScenario hook, but any exception in there will lead to the test being
410 // skipped rather than it being failed.
411 //
412 // We also need to check that the site returned is a Behat site.
413 // Again, this would be better in the BeforeSuite hook, but that does not have access to the selectors in
414 // order to perform the necessary searches.
415 $session = $this->getSession();
416 $session->visit($this->locate_path('/'));
417
418 // Checking that the root path is a Moodle test site.
419 if (self::is_first_scenario()) {
420 $message = "The base URL ({$CFG->wwwroot}) is not a behat test site. " .
421 'Ensure that you started the built-in web server in the correct directory, ' .
422 'or that your web server is correctly set up and started.';
423
424 $this->find(
425 "xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']",
426 new ExpectationException($message, $session)
427 );
428
bee40dbf
AN
429 }
430 $this->scenariorunning = true;
431 }
432 }
433
66901a69 434 /**
435 * Sets up the tags for the current scenario.
436 *
437 * @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope Scope
438 */
439 protected static function fetch_tags_for_scenario(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scope) {
440 self::$scenariotags = array_flip(array_merge(
441 $scope->getScenario()->getTags(),
442 $scope->getFeature()->getTags()
443 ));
444 }
445
446 /**
447 * Gets the tags for the current scenario
448 *
449 * @return array Array where key is tag name and value is an integer
450 */
451 public static function get_tags_for_scenario() : array {
452 return self::$scenariotags;
f5ceb6c2
DM
453 }
454
f5ceb6c2 455 /**
d1e55a47 456 * Wait for JS to complete before beginning interacting with the DOM.
f5ceb6c2 457 *
f49cede7
DM
458 * Executed only when running against a real browser. We wrap it
459 * all in a try & catch to forward the exception to i_look_for_exceptions
460 * so the exception will be at scenario level, which causes a failure, by
461 * default would be at framework level, which will stop the execution of
462 * the run.
f5ceb6c2 463 *
25fbce21 464 * @param BeforeStepScope $scope scope passed by event fired before step.
42ad096f 465 * @BeforeStep
f5ceb6c2 466 */
42ad096f
RT
467 public function before_step_javascript(BeforeStepScope $scope) {
468 self::$currentstepexception = null;
f49cede7 469
42ad096f
RT
470 // Only run if JS.
471 if ($this->running_javascript()) {
472 try {
473 $this->wait_for_pending_js();
474 } catch (Exception $e) {
475 self::$currentstepexception = $e;
476 }
f49cede7 477 }
d1e55a47
DM
478 }
479
480 /**
481 * Wait for JS to complete after finishing the step.
482 *
483 * With this we ensure that there are not AJAX calls
484 * still in progress.
485 *
f49cede7
DM
486 * Executed only when running against a real browser. We wrap it
487 * all in a try & catch to forward the exception to i_look_for_exceptions
488 * so the exception will be at scenario level, which causes a failure, by
489 * default would be at framework level, which will stop the execution of
490 * the run.
d1e55a47 491 *
25fbce21 492 * @param AfterStepScope $scope scope passed by event fired after step..
42ad096f 493 * @AfterStep
d1e55a47 494 */
42ad096f
RT
495 public function after_step_javascript(AfterStepScope $scope) {
496 global $CFG, $DB;
497
ada21cb0
RT
498 // If step is undefined then throw exception, to get failed exit code.
499 if ($scope->getTestResult()->getResultCode() === Behat\Behat\Tester\Result\StepResult::UNDEFINED) {
500 throw new coding_exception("Step '" . $scope->getStep()->getText() . "'' is undefined.");
501 }
502
0b07ea42 503 $isfailed = $scope->getTestResult()->getResultCode() === Behat\Testwork\Tester\Result\TestResult::FAILED;
42ad096f
RT
504
505 // Abort any open transactions to prevent subsequent tests hanging.
506 // This does the same as abort_all_db_transactions(), but doesn't call error_log() as we don't
507 // want to see a message in the behat output.
508 if (($scope->getTestResult() instanceof \Behat\Behat\Tester\Result\ExecutedStepResult) &&
509 $scope->getTestResult()->hasException()) {
510 if ($DB && $DB->is_transaction_started()) {
511 $DB->force_transaction_rollback();
512 }
513 }
514
0b07ea42
AN
515 if ($isfailed && !empty($CFG->behat_faildump_path)) {
516 // Save the page content (html).
517 $this->take_contentdump($scope);
518
519 if ($this->running_javascript()) {
520 // Save a screenshot.
521 $this->take_screenshot($scope);
522 }
523 }
524
525 if ($isfailed && !empty($CFG->behat_pause_on_fail)) {
526 $exception = $scope->getTestResult()->getException();
527 $message = "<colour:lightRed>Scenario failed. ";
528 $message .= "<colour:lightYellow>Paused for inspection. Press <colour:lightRed>Enter/Return<colour:lightYellow> to continue.<newline>";
529 $message .= "<colour:lightRed>Exception follows:<newline>";
530 $message .= trim($exception->getMessage());
531 behat_util::pause($this->getSession(), $message);
532 }
533
42ad096f
RT
534 // Only run if JS.
535 if (!$this->running_javascript()) {
536 return;
537 }
5c0dfe32 538
f49cede7
DM
539 try {
540 $this->wait_for_pending_js();
541 self::$currentstepexception = null;
542 } catch (UnexpectedAlertOpen $e) {
543 self::$currentstepexception = $e;
544
545 // Accepting the alert so the framework can continue properly running
546 // the following scenarios. Some browsers already closes the alert, so
547 // wrapping in a try & catch.
548 try {
549 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
550 } catch (Exception $e) {
551 // Catching the generic one as we never know how drivers reacts here.
552 }
553 } catch (Exception $e) {
554 self::$currentstepexception = $e;
555 }
d1e55a47
DM
556 }
557
131d4ac2 558 /**
bb344d07 559 * Reset the session between each scenario.
131d4ac2 560 *
25fbce21 561 * @param AfterScenarioScope $scope scope passed by event fired after scenario.
bb344d07 562 * @AfterScenario
131d4ac2 563 */
bb344d07
AN
564 public function reset_webdriver_between_scenarios(AfterScenarioScope $scope) {
565 $this->getSession()->stop();
131d4ac2
T
566 }
567
a964ead0
AN
568 /**
569 * Getter for self::$faildumpdirname
5c0dfe32
DM
570 *
571 * @return string
572 */
a964ead0
AN
573 protected function get_run_faildump_dir() {
574 return self::$faildumpdirname;
5c0dfe32
DM
575 }
576
577 /**
578 * Take screenshot when a step fails.
3ec07614 579 *
5c0dfe32 580 * @throws Exception
25fbce21 581 * @param AfterStepScope $scope scope passed by event after step.
3ec07614 582 */
42ad096f 583 protected function take_screenshot(AfterStepScope $scope) {
5c0dfe32
DM
584 // Goutte can't save screenshots.
585 if (!$this->running_javascript()) {
586 return false;
3ec07614 587 }
5c0dfe32 588
be8b8950
MJ
589 // Some drivers (e.g. chromedriver) may throw an exception while trying to take a screenshot. If this isn't handled,
590 // the behat run dies. We don't want to lose the information about the failure that triggered the screenshot,
591 // so let's log the exception message to a file (to explain why there's no screenshot) and allow the run to continue,
592 // handling the failure as normal.
593 try {
594 list ($dir, $filename) = $this->get_faildump_filename($scope, 'png');
595 $this->saveScreenshot($filename, $dir);
596 } catch (Exception $e) {
597 // Catching all exceptions as we don't know what the driver might throw.
598 list ($dir, $filename) = $this->get_faildump_filename($scope, 'txt');
599 $message = "Could not save screenshot due to an error\n" . $e->getMessage();
600 file_put_contents($dir . DIRECTORY_SEPARATOR . $filename, $message);
601 }
a964ead0
AN
602 }
603
604 /**
605 * Take a dump of the page content when a step fails.
606 *
607 * @throws Exception
25fbce21 608 * @param AfterStepScope $scope scope passed by event after step.
a964ead0 609 */
42ad096f
RT
610 protected function take_contentdump(AfterStepScope $scope) {
611 list ($dir, $filename) = $this->get_faildump_filename($scope, 'html');
a964ead0 612
be8b8950
MJ
613 try {
614 // Driver may throw an exception during getContent(), so do it first to avoid getting an empty file.
615 $content = $this->getSession()->getPage()->getContent();
616 } catch (Exception $e) {
617 // Catching all exceptions as we don't know what the driver might throw.
618 $content = "Could not save contentdump due to an error\n" . $e->getMessage();
619 }
620 file_put_contents($dir . DIRECTORY_SEPARATOR . $filename, $content);
a964ead0
AN
621 }
622
623 /**
624 * Determine the full pathname to store a failure-related dump.
625 *
626 * This is used for content such as the DOM, and screenshots.
627 *
25fbce21 628 * @param AfterStepScope $scope scope passed by event after step.
167486b3 629 * @param String $filetype The file suffix to use. Limited to 4 chars.
a964ead0 630 */
42ad096f 631 protected function get_faildump_filename(AfterStepScope $scope, $filetype) {
a964ead0
AN
632 global $CFG;
633
634 // All the contentdumps should be in the same parent dir.
635 if (!$faildumpdir = self::get_run_faildump_dir()) {
636 $faildumpdir = self::$faildumpdirname = date('Ymd_His');
5c0dfe32 637
a964ead0 638 $dir = $CFG->behat_faildump_path . DIRECTORY_SEPARATOR . $faildumpdir;
5c0dfe32 639
a964ead0 640 if (!is_dir($dir) && !mkdir($dir, $CFG->directorypermissions, true)) {
5c0dfe32 641 // It shouldn't, we already checked that the directory is writable.
a964ead0 642 throw new Exception('No directories can be created inside $CFG->behat_faildump_path, check the directory permissions.');
5c0dfe32
DM
643 }
644 } else {
645 // We will always need to know the full path.
a964ead0 646 $dir = $CFG->behat_faildump_path . DIRECTORY_SEPARATOR . $faildumpdir;
5c0dfe32
DM
647 }
648
649 // The scenario title + the failed step text.
a964ead0 650 // We want a i-am-the-scenario-title_i-am-the-failed-step.$filetype format.
42ad096f 651 $filename = $scope->getFeature()->getTitle() . '_' . $scope->getStep()->getText();
167486b3 652
484503da 653 // As file name is limited to 255 characters. Leaving 5 chars for line number and 4 chars for the file.
167486b3 654 // extension as we allow .png for images and .html for DOM contents.
484503da
RT
655 $filenamelen = 245;
656
657 // Suffix suite name to faildump file, if it's not default suite.
658 $suitename = $scope->getSuite()->getName();
659 if ($suitename != 'default') {
660 $suitename = '_' . $suitename;
661 $filenamelen = $filenamelen - strlen($suitename);
662 } else {
663 // No need to append suite name for default.
664 $suitename = '';
665 }
666
667 $filename = preg_replace('/([^a-zA-Z0-9\_]+)/', '-', $filename);
668 $filename = substr($filename, 0, $filenamelen) . $suitename . '_' . $scope->getStep()->getLine() . '.' . $filetype;
5c0dfe32 669
a964ead0 670 return array($dir, $filename);
3ec07614
DW
671 }
672
5f4b4e91
DM
673 /**
674 * Internal step definition to find exceptions, debugging() messages and PHP debug messages.
675 *
676 * Part of behat_hooks class as is part of the testing framework, is auto-executed
677 * after each step so no features will splicitly use it.
678 *
679 * @Given /^I look for exceptions$/
f49cede7 680 * @throw Exception Unknown type, depending on what we caught in the hook or basic \Exception.
1b372a77 681 * @see Moodle\BehatExtension\EventDispatcher\Tester\ChainedStepTester
5f4b4e91
DM
682 */
683 public function i_look_for_exceptions() {
f49cede7
DM
684 // If the step already failed in a hook throw the exception.
685 if (!is_null(self::$currentstepexception)) {
686 throw self::$currentstepexception;
687 }
688
eb9ca848 689 $this->look_for_exceptions();
5f4b4e91
DM
690 }
691
95b43d6b
DM
692 /**
693 * Returns whether the first scenario of the suite is running
694 *
695 * @return bool
696 */
697 protected static function is_first_scenario() {
698 return !(self::$initprocessesfinished);
699 }
61832fae 700
61832fae
AN
701 /**
702 * Register a set of component selectors.
703 *
704 * @param string $component
705 */
706 public function register_component_selectors_for_component(string $component): void {
afe13dfe
AN
707 $context = behat_context_helper::get_component_context($component);
708
709 if ($context === null) {
710 return;
61832fae
AN
711 }
712
61832fae
AN
713 $namedpartial = $this->getSession()->getSelectorsHandler()->getSelector('named_partial');
714 $namedexact = $this->getSession()->getSelectorsHandler()->getSelector('named_exact');
715
716 // Replacements must come before selectors as they are used in the selectors.
717 foreach ($context->get_named_replacements() as $replacement) {
718 $namedpartial->register_replacement($component, $replacement);
719 $namedexact->register_replacement($component, $replacement);
720 }
721
722 foreach ($context->get_partial_named_selectors() as $selector) {
723 $namedpartial->register_component_selector($component, $selector);
724 }
725
726 foreach ($context->get_exact_named_selectors() as $selector) {
727 $namedexact->register_component_selector($component, $selector);
728 }
729
730 }
731
732 /**
733 * Mark the first step as having been completed.
734 *
735 * This must be the last BeforeStep hook in the setup.
736 *
737 * @param BeforeStepScope $scope
738 * @BeforeStep
739 */
740 public function first_step_setup_complete(BeforeStepScope $scope) {
741 self::$initprocessesfinished = true;
742 }
743
f5ceb6c2 744}
a964ead0 745
25fbce21
RT
746/**
747 * Behat stop exception
748 *
749 * This exception is thrown from before suite or scenario if any setup problem found.
750 *
751 * @package core_test
752 * @copyright 2016 Rajesh Taneja <rajesh@moodle.com>
753 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
754 */
e584e6ae 755class behat_stop_exception extends \Exception {
bd1edc83 756}