Skip to content

Commit

Permalink
[Behat] Added browser log caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Nocoń committed Apr 17, 2020
1 parent 0ab4926 commit ed39fe4
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/lib/Behat/Helper/TestLogProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TestLogProvider
private const APPLICATION_LOGS_LIMIT = 25;
private const LOG_FILE_NAME = 'travis_test.log';

private static $LOGS;

/**
* @var \Behat\Mink\Session
*/
Expand All @@ -36,9 +38,20 @@ public function __construct(Session $session, string $logDirectory)

public function getBrowserLogs(): array
{
if ($this->hasCachedLogs()) {
$logs = $this->getCachedLogs();
$this->clearCachedLogs();

return $logs;
}

$driver = $this->session->getDriver();
if ($driver instanceof Selenium2Driver) {
return $this->parseBrowserLogs($driver->getWebDriverSession()->log(LogType::BROWSER));
$logs = $driver->getWebDriverSession()->log(LogType::BROWSER) ?? [];
$parsedLogs = $this->parseBrowserLogs($logs);
$this->cacheLogs($parsedLogs);

return $parsedLogs;
}

return [];
Expand Down Expand Up @@ -70,4 +83,24 @@ private function parseBrowserLogs(array $logEntries): array

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

private function hasCachedLogs(): bool
{
return !empty(self::$LOGS);
}

private function getCachedLogs(): array
{
return self::$LOGS;
}

private function clearCachedLogs(): void
{
self::$LOGS = [];
}

private function cacheLogs(array $logs): void
{
self::$LOGS = $logs;
}
}

0 comments on commit ed39fe4

Please sign in to comment.