diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 56bef49cca4dc..2d7b78f6dfd92 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -619,6 +619,7 @@ public function after_step_javascript(AfterStepScope $scope) { if ($isfailed && !empty($CFG->behat_faildump_path)) { // Save the page content (html). $this->take_contentdump($scope); + $this->take_consoledump($scope); if ($this->running_javascript()) { // Save a screenshot. @@ -741,6 +742,39 @@ protected function take_contentdump(AfterStepScope $scope) { file_put_contents($dir . DIRECTORY_SEPARATOR . $filename, $content); } + + /** + * Take a dump of the browser console content when a step fails. + * + * @throws Exception + * @param AfterStepScope $scope scope passed by event after step. + */ + protected function take_consoledump(AfterStepScope $scope) { + $content = ""; + + try { + // Driver may throw an exception. + $logs = $this->getSession()->getDriver()->getWebDriver()->manage()->getLog('browser'); + + foreach ($logs as $log) { + $time = date('H:i:s', $log['timestamp']); + $level = $log['level']; + $message = $log['message']; + $content .= "$time - [$level] $message\n"; + } + } catch (Exception $e) { + // Catching all exceptions as we don't know what the driver might throw. + $content = "Could not save consoledump due to an error\n" . $e->getMessage(); + } + + if (empty($content)) { + return; + } + + list ($dir, $filename) = $this->get_faildump_filename($scope, 'log'); + file_put_contents($dir . DIRECTORY_SEPARATOR . $filename, $content); + } + /** * Determine the full pathname to store a failure-related dump. *