Skip to content

Commit

Permalink
MDL-74858 behat: Save browser console dump when a test fail in extension
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Aug 16, 2024
1 parent 94723cb commit ac7249b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Behat\Behat\EventDispatcher\Event\BeforeStepTested;
use Behat\Testwork\Output\Formatter;
use Behat\Testwork\Output\Printer\OutputPrinter;
use Behat\Context\Context\Context;

// phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod

Expand Down Expand Up @@ -177,6 +178,11 @@ public function afterStep(AfterStepTested $event) {
if (in_array('html', $formats)) {
$this->take_contentdump($event, $behathookcontext);
}

// Save console content.
if (in_array('console', $formats)) {
$this->take_consoledump($event, $behathookcontext);
}
}

/**
Expand Down Expand Up @@ -227,7 +233,7 @@ protected function get_run_screenshot_dir() {
* @param AfterStepTested $event
* @param Context $context
*/
protected function take_screenshot(AfterStepTested $event, $context) {
protected function take_screenshot(AfterStepTested $event, Context $context) {
// BrowserKit can't save screenshots.
if ($context->getMink()->isSessionStarted($context->getMink()->getDefaultSessionName())) {
if (get_class($context->getMink()->getSession()->getDriver()) === 'Behat\Mink\Driver\BrowserKitDriver') {
Expand All @@ -243,15 +249,46 @@ protected function take_screenshot(AfterStepTested $event, $context) {
*
* @throws Exception
* @param AfterStepTested $event
* @param \Behat\Context\Context\Context $context
* @param Context $context
*/
protected function take_contentdump(AfterStepTested $event, $context) {
protected function take_contentdump(AfterStepTested $event, Context $context) {
list ($dir, $filename) = $this->get_faildump_filename($event, 'html');
$fh = fopen($dir . DIRECTORY_SEPARATOR . $filename, 'w');
fwrite($fh, $context->getMink()->getSession()->getPage()->getContent());
fclose($fh);
}

/**
* Take a dump of the browser console content when a step fails.
*
* @throws Exception
* @param AfterStepTested $event
* @param Context $context
*/
protected function take_consoledump(AfterStepTested $event, Context $context) {
$logs = $this->getSession()->getDriver()->getWebDriver()->manage()->getLog('browser');
if (empty($logs)) {
return;
}

$content = "";
foreach ($logs as $log) {
$time = date('H:i:s', $log['timestamp']);
$level = $log['level'];
$message = $log['message'];
$content .= "$time - [$level] $message\n";
}

if (empty($content)) {
return;
}

list ($dir, $filename) = $this->get_faildump_filename($event, 'log');
$fh = fopen($dir . DIRECTORY_SEPARATOR . $filename, 'w');
fwrite($fh, $content);
fclose($fh);
}

/**
* Determine the full pathname to store a failure-related dump.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ protected function loadMoodleScreenshotFormatter(ContainerBuilder $container) {
$definition = new Definition('Moodle\BehatExtension\Output\Formatter\MoodleScreenshotFormatter', [
'moodle_screenshot',
// phpcs:ignore Generic.Files.LineLength.TooLong
'Take screenshot of all steps. Use --format-settings \'{"formats": "html,image"}\' to get specific o/p type',
['formats' => 'html,image'],
'Take screenshot of all steps. Use --format-settings \'{"formats": "html,image,console"}\' to get specific o/p type',
['formats' => 'html,image,console'],
$this->createOutputPrinterDefinition()
]);
$definition->addTag(OutputExtension::FORMATTER_TAG, ['priority' => 102]);
Expand Down

0 comments on commit ac7249b

Please sign in to comment.