Skip to content

Commit

Permalink
MDL-74858 behat: Save browser console dump when a test fail
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Aug 16, 2024
1 parent 6cd5507 commit 94723cb
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/tests/behat/behat_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 94723cb

Please sign in to comment.