diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Extension/BrowserLogUtilTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Extension/BrowserLogUtilTest.php new file mode 100644 index 000000000..32cac698e --- /dev/null +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Extension/BrowserLogUtilTest.php @@ -0,0 +1,76 @@ + "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]); + } +} diff --git a/docs/configuration.md b/docs/configuration.md index c741e2f60..4af2c4e82 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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 +``` + [`MAGENTO_CLI_COMMAND_PATH`]: #magento_cli_command_path diff --git a/etc/config/.env.example b/etc/config/.env.example index f25cb3de7..bb1d1ad63 100644 --- a/etc/config/.env.example +++ b/etc/config/.env.example @@ -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 ***# diff --git a/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php new file mode 100644 index 000000000..ca9d2be9e --- /dev/null +++ b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php @@ -0,0 +1,84 @@ +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"]); + } +} diff --git a/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php b/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php deleted file mode 100644 index b0621df1b..000000000 --- a/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php +++ /dev/null @@ -1,76 +0,0 @@ -webDriver->manage()->getAvailableLogTypes())) { - $browserLogEntries = $module->webDriver->manage()->getLog("browser"); - foreach ($browserLogEntries as $entry) { - if (array_key_exists("source", $entry) && $entry["source"] === "javascript") { - $this->logError("javascript", $stepEvent, $entry); - //Set javascript error in MagentoWebDriver internal array - $module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]); - } - } - } - } - - /** - * Logs errors to console/report. - * @param string $type - * @param \Codeception\Event\StepEvent $stepEvent - * @param array $entry - * @return void - */ - private function logError($type, $stepEvent, $entry) - { - //TODO Add to overall log - $stepEvent->getTest()->getScenario()->comment("{$type} ERROR({$entry["level"]}) - " . $entry["message"]); - } -} diff --git a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php index 467074097..9fe7adb7a 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php +++ b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php @@ -7,6 +7,7 @@ namespace Magento\FunctionalTestingFramework\Extension; use Codeception\Events; +use Magento\FunctionalTestingFramework\Allure\AllureHelper; use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler; /** @@ -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); } /**