Skip to content

Commit

Permalink
Merge pull request #1271 from ezsystems/add-attachments
Browse files Browse the repository at this point in the history
[Behat] Refactored displaying of application/browser logs
  • Loading branch information
micszo authored Apr 9, 2020
2 parents 505d2b1 + be85183 commit 769d88a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 35 deletions.
55 changes: 20 additions & 35 deletions src/lib/Behat/Helper/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Behat\Helper;

use Behat\Behat\Hook\Call\BeforeStep;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\BeforeStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Behat\Testwork\Tester\Result\TestResult;
use EzSystems\EzPlatformAdminUi\Behat\PageElement\ElementFactory;
use EzSystems\EzPlatformAdminUi\Behat\PageObject\PageObjectFactory;
use Psr\Log\LoggerInterface;
use WebDriver\LogType;

class Hooks extends RawMinkContext
{
private const CONSOLE_LOGS_LIMIT = 10;
private const APPLICATION_LOGS_LIMIT = 25;
private const LOG_FILE_NAME = 'travis_test.log';
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

use KernelDictionary;
Expand All @@ -41,28 +41,28 @@ public function __construct(LoggerInterface $logger)
*/
public function logStartingScenario(BeforeScenarioScope $scope)
{
$this->logger->error(sprintf('Starting Scenario "%s"', $scope->getScenario()->getTitle()));
$this->logger->error(sprintf('Behat: Starting Scenario "%s"', $scope->getScenario()->getTitle()));
}

/** @BeforeStep
*/
public function logStartingStep(BeforeStepScope $scope)
{
$this->logger->error(sprintf('Starting Step %s', $scope->getStep()->getText()));
$this->logger->error(sprintf('Behat: Starting Step "%s"', $scope->getStep()->getText()));
}

/** @AfterScenario
*/
public function logEndingScenario(AfterScenarioScope $scope)
{
$this->logger->error(sprintf('Ending Scenario "%s"', $scope->getScenario()->getTitle()));
$this->logger->error(sprintf('Behat: Ending Scenario "%s"', $scope->getScenario()->getTitle()));
}

/** @AfterStep
*/
public function logEndingStep(AfterStepScope $scope)
{
$this->logger->error(sprintf('Ending Step %s', $scope->getStep()->getText()));
$this->logger->error(sprintf('Behat: Ending Step "%s"', $scope->getStep()->getText()));
}

/** @BeforeScenario
Expand All @@ -84,42 +84,27 @@ public function getLogsAfterFailedStep(AfterStepScope $scope)
return;
}

$driver = $this->getSession()->getDriver();
if ($driver instanceof Selenium2Driver) {
$browserLogEntries = $this->parseBrowserLogs($driver->getWebDriverSession()->log(LogType::BROWSER));
$this->displayLogEntries('JS console errors:', $browserLogEntries);
}
$testLogProvider = new TestLogProvider($this->getSession(), $this->getKernel()->getLogDir());

$logReader = new LogFileReader();
$applicationsLogs = $testLogProvider->getApplicationLogs();
$browserLogs = $testLogProvider->getBrowserLogs();

$applicationLogEntries = $logReader->getLastLines(sprintf('%s/%s', $this->getKernel()->getLogDir(), self::LOG_FILE_NAME), self::APPLICATION_LOGS_LIMIT);
$this->displayLogEntries('Application errors:', $applicationLogEntries);
echo $this->formatForDisplay($browserLogs, 'JS Console errors:');
echo $this->formatForDisplay($applicationsLogs, 'Application logs:');
}

private function parseBrowserLogs($logEntries): array
public function formatForDisplay(array $logEntries, string $sectionName)
{
$filter = new BrowserLogFilter();

if (empty($logEntries)) {
return [];
}
$output = sprintf('%s' . PHP_EOL, $sectionName);

$errorMessages = array_column($logEntries, 'message');
$errorMessages = $filter->filter($errorMessages);

return \array_slice($errorMessages, 0, self::CONSOLE_LOGS_LIMIT);
}

private function displayLogEntries($sectionName, $logEntries)
{
if (empty($logEntries)) {
return;
$output .= sprintf("\t No logs for this section.") . PHP_EOL;
}

echo sprintf('%s' . PHP_EOL, $sectionName);

foreach ($logEntries as $logEntry) {
echo sprintf('%s' . PHP_EOL, $logEntry);
$output .= sprintf("\t%s" . PHP_EOL, $logEntry);
}

return $output;
}
}
73 changes: 73 additions & 0 deletions src/lib/Behat/Helper/TestLogProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Behat\Helper;

use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Session;
use WebDriver\LogType;

class TestLogProvider
{
private const CONSOLE_LOGS_LIMIT = 10;
private const APPLICATION_LOGS_LIMIT = 25;
private const LOG_FILE_NAME = 'travis_test.log';

/**
* @var \Behat\Mink\Session
*/
private $session;

/**
* @var string
*/
private $logDirectory;

public function __construct(Session $session, string $logDirectory)
{
$this->session = $session;
$this->logDirectory = $logDirectory;
}

public function getBrowserLogs(): array
{
$driver = $this->session->getDriver();
if ($driver instanceof Selenium2Driver) {
return $this->parseBrowserLogs($driver->getWebDriverSession()->log(LogType::BROWSER));
}

return [];
}

public function getApplicationLogs(): array
{
$logReader = new LogFileReader();
$lines = $logReader->getLastLines(sprintf('%s/%s', $this->logDirectory, self::LOG_FILE_NAME), self::APPLICATION_LOGS_LIMIT);
$parsedLines = [];
foreach ($lines as $line) {
$parsedLine = str_replace([' app.ERROR: Behat:', '[] []'], '', $line);
$parsedLines[] = $parsedLine;
}

return $parsedLines;
}

private function parseBrowserLogs(array $logEntries): array
{
$filter = new BrowserLogFilter();

if (empty($logEntries)) {
return [];
}

$errorMessages = array_column($logEntries, 'message');
$errorMessages = $filter->filter($errorMessages);

return \array_slice($errorMessages, 0, self::CONSOLE_LOGS_LIMIT);
}
}

0 comments on commit 769d88a

Please sign in to comment.