From 36a243a40b378d6d81022b41cb19cfefb74a0100 Mon Sep 17 00:00:00 2001 From: Kevin Kozan Date: Wed, 16 Oct 2019 14:08:09 -0500 Subject: [PATCH 1/6] MQE-1644: Add ability to see JS log in Allure - Initial logging addition. --- .../Extension/ErrorLogger.php | 36 ++++++++++++++----- .../Extension/TestContextExtension.php | 8 +++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php b/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php index b0621df1b..07a6d2779 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php +++ b/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php @@ -12,6 +12,9 @@ */ class ErrorLogger { + const LOG_TYPE_BROWSER = "browser"; + const ERROR_TYPE_JAVASCRIPT = "javascript"; + /** * Error Logger Instance * @var ErrorLogger @@ -49,16 +52,33 @@ private function __construct() public function logErrors($module, $stepEvent) { //Types available should be "server", "browser", "driver". Only care about browser at the moment. - if (in_array("browser", $module->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"]); - } + if (in_array(self::LOG_TYPE_BROWSER, $module->webDriver->manage()->getAvailableLogTypes())) { + $browserLogEntries = $module->webDriver->manage()->getLog(self::LOG_TYPE_BROWSER); + $jsErrors = $this->getLogsOfType($browserLogEntries, self::ERROR_TYPE_JAVASCRIPT); + foreach ($jsErrors as $entry) { + $this->logError(self::ERROR_TYPE_JAVASCRIPT, $stepEvent, $entry); + //Set javascript error in MagentoWebDriver internal array + $module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]); + } + } + } + + /** + * Loops through given logs and returns entries of the given type. + * + * @param array $log + * @param string $type + * @return array + */ + public function getLogsOfType($log, $type) + { + $errors = []; + foreach ($log as $entry) { + if (array_key_exists("source", $entry) && $entry["source"] === $type) { + $errors[] = $entry; } } + return $errors; } /** diff --git a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php index 467074097..93faf9e82 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,6 +174,13 @@ public function beforeStep(\Codeception\Event\StepEvent $e) */ public function afterStep(\Codeception\Event\StepEvent $e) { + if (getenv('ENABLE_JS_LOG')) { + $browserLogEntries = $this->getDriver()->webDriver->manage()->getLog("browser"); + $jsErrors = ErrorLogger::getInstance()->getLogsOfType($browserLogEntries, ErrorLogger::ERROR_TYPE_JAVASCRIPT); + if (!empty($jsErrors)) { + AllureHelper::addAttachmentToCurrentStep($jsErrors); + } + } ErrorLogger::getInstance()->logErrors($this->getDriver(), $e); } From 997a9ef190f696903d1ecb3206fd31185f5dc886 Mon Sep 17 00:00:00 2001 From: Kevin Kozan Date: Thu, 17 Oct 2019 09:40:22 -0500 Subject: [PATCH 2/6] MQE-1644: Add ability to see JS log in Allure - Addec blacklist - Renamed variables --- etc/config/.env.example | 5 +++++ .../Extension/ErrorLogger.php | 20 ++++++++++++++++++- .../Extension/TestContextExtension.php | 13 +++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) 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/ErrorLogger.php b/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php index 07a6d2779..b76a9c1f4 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php +++ b/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php @@ -64,7 +64,7 @@ public function logErrors($module, $stepEvent) } /** - * Loops through given logs and returns entries of the given type. + * Loops through given log and returns entries of the given type. * * @param array $log * @param string $type @@ -81,6 +81,24 @@ public function getLogsOfType($log, $type) return $errors; } + /** + * Loops through given log and filters entries of the given type. + * + * @param array $log + * @param string $type + * @return array + */ + public 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 diff --git a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php index 93faf9e82..639d65133 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php +++ b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php @@ -174,11 +174,14 @@ public function beforeStep(\Codeception\Event\StepEvent $e) */ public function afterStep(\Codeception\Event\StepEvent $e) { - if (getenv('ENABLE_JS_LOG')) { - $browserLogEntries = $this->getDriver()->webDriver->manage()->getLog("browser"); - $jsErrors = ErrorLogger::getInstance()->getLogsOfType($browserLogEntries, ErrorLogger::ERROR_TYPE_JAVASCRIPT); - if (!empty($jsErrors)) { - AllureHelper::addAttachmentToCurrentStep($jsErrors); + if (getenv('ENABLE_BROWSER_LOG')) { + $browserLog = $this->getDriver()->webDriver->manage()->getLog("browser"); + foreach (explode(',', getenv('BROWSER_LOG_BLACKLIST')) as $source) { + $browserLog = ErrorLogger::getInstance()->filterLogsOfType($browserLog, $source); + } + + if (!empty($browserLog)) { + AllureHelper::addAttachmentToCurrentStep(json_encode($browserLog, JSON_PRETTY_PRINT), "Browser Log"); } } ErrorLogger::getInstance()->logErrors($this->getDriver(), $e); From dc0bf75cc902212b7eb1572c6757f55134881995 Mon Sep 17 00:00:00 2001 From: Kevin Kozan Date: Thu, 17 Oct 2019 10:39:34 -0500 Subject: [PATCH 3/6] MQE-1644: Add ability to see JS log in Allure - Refactor ErrorLogger into BrowserLogUtil, no longer a singleton --- .../{ErrorLogger.php => BrowserLogUtil.php} | 43 ++++--------------- .../Extension/TestContextExtension.php | 4 +- 2 files changed, 10 insertions(+), 37 deletions(-) rename src/Magento/FunctionalTestingFramework/Extension/{ErrorLogger.php => BrowserLogUtil.php} (71%) diff --git a/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php similarity index 71% rename from src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php rename to src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php index b76a9c1f4..330c21ebf 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php +++ b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php @@ -7,41 +7,14 @@ namespace Magento\FunctionalTestingFramework\Extension; /** - * Class ErrorLogger + * Class BrowserLogUtil * @package Magento\FunctionalTestingFramework\Extension */ -class ErrorLogger +class BrowserLogUtil { const LOG_TYPE_BROWSER = "browser"; const ERROR_TYPE_JAVASCRIPT = "javascript"; - /** - * Error Logger Instance - * @var ErrorLogger - */ - private static $errorLogger; - - /** - * Singleton method to return ErrorLogger. - * @return ErrorLogger - */ - public static function getInstance() - { - if (!self::$errorLogger) { - self::$errorLogger = new ErrorLogger(); - } - - return self::$errorLogger; - } - - /** - * ErrorLogger constructor. - */ - private function __construct() - { - // private constructor - } - /** * Loops through stepEvent for browser log entries * @@ -49,14 +22,14 @@ private function __construct() * @param \Codeception\Event\StepEvent $stepEvent * @return void */ - public function logErrors($module, $stepEvent) + public static function logErrors($module, $stepEvent) { //Types available should be "server", "browser", "driver". Only care about browser at the moment. if (in_array(self::LOG_TYPE_BROWSER, $module->webDriver->manage()->getAvailableLogTypes())) { $browserLogEntries = $module->webDriver->manage()->getLog(self::LOG_TYPE_BROWSER); - $jsErrors = $this->getLogsOfType($browserLogEntries, self::ERROR_TYPE_JAVASCRIPT); + $jsErrors = self::getLogsOfType($browserLogEntries, self::ERROR_TYPE_JAVASCRIPT); foreach ($jsErrors as $entry) { - $this->logError(self::ERROR_TYPE_JAVASCRIPT, $stepEvent, $entry); + self::logError(self::ERROR_TYPE_JAVASCRIPT, $stepEvent, $entry); //Set javascript error in MagentoWebDriver internal array $module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]); } @@ -70,7 +43,7 @@ public function logErrors($module, $stepEvent) * @param string $type * @return array */ - public function getLogsOfType($log, $type) + public static function getLogsOfType($log, $type) { $errors = []; foreach ($log as $entry) { @@ -88,7 +61,7 @@ public function getLogsOfType($log, $type) * @param string $type * @return array */ - public function filterLogsOfType($log, $type) + public static function filterLogsOfType($log, $type) { $errors = []; foreach ($log as $entry) { @@ -106,7 +79,7 @@ public function filterLogsOfType($log, $type) * @param array $entry * @return void */ - private function logError($type, $stepEvent, $entry) + 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/TestContextExtension.php b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php index 639d65133..10b7621b4 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php +++ b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php @@ -177,14 +177,14 @@ public function afterStep(\Codeception\Event\StepEvent $e) if (getenv('ENABLE_BROWSER_LOG')) { $browserLog = $this->getDriver()->webDriver->manage()->getLog("browser"); foreach (explode(',', getenv('BROWSER_LOG_BLACKLIST')) as $source) { - $browserLog = ErrorLogger::getInstance()->filterLogsOfType($browserLog, $source); + $browserLog = BrowserLogUtil::filterLogsOfType($browserLog, $source); } if (!empty($browserLog)) { AllureHelper::addAttachmentToCurrentStep(json_encode($browserLog, JSON_PRETTY_PRINT), "Browser Log"); } } - ErrorLogger::getInstance()->logErrors($this->getDriver(), $e); + BrowserLogUtil::logErrors($this->getDriver(), $e); } /** From 31073a38efa5d6d47943004995c99539fdd71908 Mon Sep 17 00:00:00 2001 From: Kevin Kozan Date: Thu, 17 Oct 2019 11:34:26 -0500 Subject: [PATCH 4/6] MQE-1644: Add ability to see JS log in Allure - Unit test + static check fixes --- .../Extension/BrowserLogUtilTest.php | 76 +++++++++++++++++++ .../Extension/BrowserLogUtil.php | 4 +- 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 dev/tests/unit/Magento/FunctionalTestFramework/Extension/BrowserLogUtilTest.php 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/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php index 330c21ebf..a4f5c6d4c 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php +++ b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php @@ -39,7 +39,7 @@ public static function logErrors($module, $stepEvent) /** * Loops through given log and returns entries of the given type. * - * @param array $log + * @param array $log * @param string $type * @return array */ @@ -57,7 +57,7 @@ public static function getLogsOfType($log, $type) /** * Loops through given log and filters entries of the given type. * - * @param array $log + * @param array $log * @param string $type * @return array */ From d7d14c333efbe97836cde7d2fefbec6f05651249 Mon Sep 17 00:00:00 2001 From: Kevin Kozan Date: Thu, 17 Oct 2019 11:57:33 -0500 Subject: [PATCH 5/6] MQE-1644: Add ability to see JS log in Allure - Added documentation --- docs/configuration.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 From 39d987a4e08d393cb994c362672f50900aa0e385 Mon Sep 17 00:00:00 2001 From: Kevin Kozan Date: Tue, 22 Oct 2019 09:00:30 -0500 Subject: [PATCH 6/6] MQE-1644: Add ability to see JS log in Allure - Moved redundant logic --- .../Extension/BrowserLogUtil.php | 23 ++++++++----------- .../Extension/TestContextExtension.php | 5 ++-- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php index a4f5c6d4c..ca9d2be9e 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php +++ b/src/Magento/FunctionalTestingFramework/Extension/BrowserLogUtil.php @@ -16,23 +16,20 @@ class BrowserLogUtil const ERROR_TYPE_JAVASCRIPT = "javascript"; /** - * Loops through stepEvent for browser log entries + * Loops throw errors in log and logs them to allure. Uses Module to set the error itself * - * @param \Magento\FunctionalTestingFramework\Module\MagentoWebDriver $module - * @param \Codeception\Event\StepEvent $stepEvent + * @param array $log + * @param \Codeception\Module\WebDriver $module + * @param \Codeception\Event\StepEvent $stepEvent * @return void */ - public static function logErrors($module, $stepEvent) + public static function logErrors($log, $module, $stepEvent) { - //Types available should be "server", "browser", "driver". Only care about browser at the moment. - if (in_array(self::LOG_TYPE_BROWSER, $module->webDriver->manage()->getAvailableLogTypes())) { - $browserLogEntries = $module->webDriver->manage()->getLog(self::LOG_TYPE_BROWSER); - $jsErrors = self::getLogsOfType($browserLogEntries, 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"]); - } + $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"]); } } diff --git a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php index 10b7621b4..9fe7adb7a 100644 --- a/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php +++ b/src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php @@ -174,17 +174,16 @@ public function beforeStep(\Codeception\Event\StepEvent $e) */ public function afterStep(\Codeception\Event\StepEvent $e) { + $browserLog = $this->getDriver()->webDriver->manage()->getLog("browser"); if (getenv('ENABLE_BROWSER_LOG')) { - $browserLog = $this->getDriver()->webDriver->manage()->getLog("browser"); 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($this->getDriver(), $e); + BrowserLogUtil::logErrors($browserLog, $this->getDriver(), $e); } /**