Skip to content

Commit 36a243a

Browse files
committed
MQE-1644: Add ability to see JS log in Allure
- Initial logging addition.
1 parent 8ac2937 commit 36a243a

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
class ErrorLogger
1414
{
15+
const LOG_TYPE_BROWSER = "browser";
16+
const ERROR_TYPE_JAVASCRIPT = "javascript";
17+
1518
/**
1619
* Error Logger Instance
1720
* @var ErrorLogger
@@ -49,16 +52,33 @@ private function __construct()
4952
public function logErrors($module, $stepEvent)
5053
{
5154
//Types available should be "server", "browser", "driver". Only care about browser at the moment.
52-
if (in_array("browser", $module->webDriver->manage()->getAvailableLogTypes())) {
53-
$browserLogEntries = $module->webDriver->manage()->getLog("browser");
54-
foreach ($browserLogEntries as $entry) {
55-
if (array_key_exists("source", $entry) && $entry["source"] === "javascript") {
56-
$this->logError("javascript", $stepEvent, $entry);
57-
//Set javascript error in MagentoWebDriver internal array
58-
$module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]);
59-
}
55+
if (in_array(self::LOG_TYPE_BROWSER, $module->webDriver->manage()->getAvailableLogTypes())) {
56+
$browserLogEntries = $module->webDriver->manage()->getLog(self::LOG_TYPE_BROWSER);
57+
$jsErrors = $this->getLogsOfType($browserLogEntries, self::ERROR_TYPE_JAVASCRIPT);
58+
foreach ($jsErrors as $entry) {
59+
$this->logError(self::ERROR_TYPE_JAVASCRIPT, $stepEvent, $entry);
60+
//Set javascript error in MagentoWebDriver internal array
61+
$module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]);
62+
}
63+
}
64+
}
65+
66+
/**
67+
* Loops through given logs and returns entries of the given type.
68+
*
69+
* @param array $log
70+
* @param string $type
71+
* @return array
72+
*/
73+
public function getLogsOfType($log, $type)
74+
{
75+
$errors = [];
76+
foreach ($log as $entry) {
77+
if (array_key_exists("source", $entry) && $entry["source"] === $type) {
78+
$errors[] = $entry;
6079
}
6180
}
81+
return $errors;
6282
}
6383

6484
/**

src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\FunctionalTestingFramework\Extension;
88

99
use Codeception\Events;
10+
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
1011
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
1112

1213
/**
@@ -173,6 +174,13 @@ public function beforeStep(\Codeception\Event\StepEvent $e)
173174
*/
174175
public function afterStep(\Codeception\Event\StepEvent $e)
175176
{
177+
if (getenv('ENABLE_JS_LOG')) {
178+
$browserLogEntries = $this->getDriver()->webDriver->manage()->getLog("browser");
179+
$jsErrors = ErrorLogger::getInstance()->getLogsOfType($browserLogEntries, ErrorLogger::ERROR_TYPE_JAVASCRIPT);
180+
if (!empty($jsErrors)) {
181+
AllureHelper::addAttachmentToCurrentStep($jsErrors);
182+
}
183+
}
176184
ErrorLogger::getInstance()->logErrors($this->getDriver(), $e);
177185
}
178186

0 commit comments

Comments
 (0)