Skip to content

MQE-1644: Add ability to see JS log in Allure #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace tests\unit\Magento\FunctionalTestFramework\Extension;

use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
use Magento\FunctionalTestingFramework\Extension\BrowserLogUtil;

class BrowserLogUtilTest extends MagentoTestCase
{
public function testGetLogsOfType()
{
$entryOne = [
"level" => "WARNING",
"message" => "warningMessage",
"source" => "console-api",
"timestamp" => 1234567890
];
$entryTwo = [
"level" => "ERROR",
"message" => "errorMessage",
"source" => "other",
"timestamp" => 1234567890
];
$entryThree = [
"level" => "LOG",
"message" => "logMessage",
"source" => "javascript",
"timestamp" => 1234567890
];
$log = [
$entryOne,
$entryTwo,
$entryThree
];

$actual = BrowserLogUtil::getLogsOfType($log, 'console-api');

self::assertEquals($entryOne, $actual[0]);
}

public function testFilterLogsOfType()
{
$entryOne = [
"level" => "WARNING",
"message" => "warningMessage",
"source" => "console-api",
"timestamp" => 1234567890
];
$entryTwo = [
"level" => "ERROR",
"message" => "errorMessage",
"source" => "other",
"timestamp" => 1234567890
];
$entryThree = [
"level" => "LOG",
"message" => "logMessage",
"source" => "javascript",
"timestamp" => 1234567890
];
$log = [
$entryOne,
$entryTwo,
$entryThree
];

$actual = BrowserLogUtil::filterLogsOfType($log, 'console-api');

self::assertEquals($entryTwo, $actual[0]);
self::assertEquals($entryThree, $actual[1]);
}
}
18 changes: 18 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ Example:
CREDENTIAL_VAULT_SECRET_BASE_PATH=secret
```

### ENABLE_BROWSER_LOG

Enables addition of browser logs to Allure steps

```conf
ENABLE_BROWSER_LOG=true
```

### BROWSER_LOG_BLACKLIST

Blacklists types of browser log entries from appearing in Allure steps.

Denoted in browser log entry as `"SOURCE": "type"`.

```conf
BROWSER_LOG_BLACKLIST=other,console-api
```

<!-- Link definitions -->

[`MAGENTO_CLI_COMMAND_PATH`]: #magento_cli_command_path
Expand Down
5 changes: 5 additions & 0 deletions etc/config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ MODULE_WHITELIST=Magento_Framework,ConfigurableProductWishlist,ConfigurableProdu

#*** Default timeout for wait actions
#WAIT_TIMEOUT=10

#*** Uncomment and set to enable browser log entries on actions in Allure. Blacklist is used to filter logs of a specific "source"
#ENABLE_BROWSER_LOG=true
#BROWSER_LOG_BLACKLIST=other

#*** End of .env ***#
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\FunctionalTestingFramework\Extension;

/**
* Class BrowserLogUtil
* @package Magento\FunctionalTestingFramework\Extension
*/
class BrowserLogUtil
{
const LOG_TYPE_BROWSER = "browser";
const ERROR_TYPE_JAVASCRIPT = "javascript";

/**
* Loops throw errors in log and logs them to allure. Uses Module to set the error itself
*
* @param array $log
* @param \Codeception\Module\WebDriver $module
* @param \Codeception\Event\StepEvent $stepEvent
* @return void
*/
public static function logErrors($log, $module, $stepEvent)
{
$jsErrors = self::getLogsOfType($log, self::ERROR_TYPE_JAVASCRIPT);
foreach ($jsErrors as $entry) {
self::logError(self::ERROR_TYPE_JAVASCRIPT, $stepEvent, $entry);
//Set javascript error in MagentoWebDriver internal array
$module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]);
}
}

/**
* Loops through given log and returns entries of the given type.
*
* @param array $log
* @param string $type
* @return array
*/
public static function getLogsOfType($log, $type)
{
$errors = [];
foreach ($log as $entry) {
if (array_key_exists("source", $entry) && $entry["source"] === $type) {
$errors[] = $entry;
}
}
return $errors;
}

/**
* Loops through given log and filters entries of the given type.
*
* @param array $log
* @param string $type
* @return array
*/
public static function filterLogsOfType($log, $type)
{
$errors = [];
foreach ($log as $entry) {
if (array_key_exists("source", $entry) && $entry["source"] !== $type) {
$errors[] = $entry;
}
}
return $errors;
}

/**
* Logs errors to console/report.
* @param string $type
* @param \Codeception\Event\StepEvent $stepEvent
* @param array $entry
* @return void
*/
private static function logError($type, $stepEvent, $entry)
{
//TODO Add to overall log
$stepEvent->getTest()->getScenario()->comment("{$type} ERROR({$entry["level"]}) - " . $entry["message"]);
}
}
76 changes: 0 additions & 76 deletions src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\FunctionalTestingFramework\Extension;

use Codeception\Events;
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;

/**
Expand Down Expand Up @@ -173,7 +174,16 @@ public function beforeStep(\Codeception\Event\StepEvent $e)
*/
public function afterStep(\Codeception\Event\StepEvent $e)
{
ErrorLogger::getInstance()->logErrors($this->getDriver(), $e);
$browserLog = $this->getDriver()->webDriver->manage()->getLog("browser");
if (getenv('ENABLE_BROWSER_LOG')) {
foreach (explode(',', getenv('BROWSER_LOG_BLACKLIST')) as $source) {
$browserLog = BrowserLogUtil::filterLogsOfType($browserLog, $source);
}
if (!empty($browserLog)) {
AllureHelper::addAttachmentToCurrentStep(json_encode($browserLog, JSON_PRETTY_PRINT), "Browser Log");
}
}
BrowserLogUtil::logErrors($browserLog, $this->getDriver(), $e);
}

/**
Expand Down