Skip to content

Commit c3f7c05

Browse files
authored
Merge pull request #489 from magento/MQE-1644
MQE-1644: Add ability to see JS log in Allure
2 parents e175381 + ac6daba commit c3f7c05

File tree

6 files changed

+194
-77
lines changed

6 files changed

+194
-77
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace tests\unit\Magento\FunctionalTestFramework\Extension;
8+
9+
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
10+
use Magento\FunctionalTestingFramework\Extension\BrowserLogUtil;
11+
12+
class BrowserLogUtilTest extends MagentoTestCase
13+
{
14+
public function testGetLogsOfType()
15+
{
16+
$entryOne = [
17+
"level" => "WARNING",
18+
"message" => "warningMessage",
19+
"source" => "console-api",
20+
"timestamp" => 1234567890
21+
];
22+
$entryTwo = [
23+
"level" => "ERROR",
24+
"message" => "errorMessage",
25+
"source" => "other",
26+
"timestamp" => 1234567890
27+
];
28+
$entryThree = [
29+
"level" => "LOG",
30+
"message" => "logMessage",
31+
"source" => "javascript",
32+
"timestamp" => 1234567890
33+
];
34+
$log = [
35+
$entryOne,
36+
$entryTwo,
37+
$entryThree
38+
];
39+
40+
$actual = BrowserLogUtil::getLogsOfType($log, 'console-api');
41+
42+
self::assertEquals($entryOne, $actual[0]);
43+
}
44+
45+
public function testFilterLogsOfType()
46+
{
47+
$entryOne = [
48+
"level" => "WARNING",
49+
"message" => "warningMessage",
50+
"source" => "console-api",
51+
"timestamp" => 1234567890
52+
];
53+
$entryTwo = [
54+
"level" => "ERROR",
55+
"message" => "errorMessage",
56+
"source" => "other",
57+
"timestamp" => 1234567890
58+
];
59+
$entryThree = [
60+
"level" => "LOG",
61+
"message" => "logMessage",
62+
"source" => "javascript",
63+
"timestamp" => 1234567890
64+
];
65+
$log = [
66+
$entryOne,
67+
$entryTwo,
68+
$entryThree
69+
];
70+
71+
$actual = BrowserLogUtil::filterLogsOfType($log, 'console-api');
72+
73+
self::assertEquals($entryTwo, $actual[0]);
74+
self::assertEquals($entryThree, $actual[1]);
75+
}
76+
}

docs/configuration.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ Example:
277277
CREDENTIAL_VAULT_SECRET_BASE_PATH=secret
278278
```
279279

280+
### ENABLE_BROWSER_LOG
281+
282+
Enables addition of browser logs to Allure steps
283+
284+
```conf
285+
ENABLE_BROWSER_LOG=true
286+
```
287+
288+
### BROWSER_LOG_BLACKLIST
289+
290+
Blacklists types of browser log entries from appearing in Allure steps.
291+
292+
Denoted in browser log entry as `"SOURCE": "type"`.
293+
294+
```conf
295+
BROWSER_LOG_BLACKLIST=other,console-api
296+
```
297+
280298
<!-- Link definitions -->
281299

282300
[`MAGENTO_CLI_COMMAND_PATH`]: #magento_cli_command_path

etc/config/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ MODULE_WHITELIST=Magento_Framework,ConfigurableProductWishlist,ConfigurableProdu
5454

5555
#*** Default timeout for wait actions
5656
#WAIT_TIMEOUT=10
57+
58+
#*** Uncomment and set to enable browser log entries on actions in Allure. Blacklist is used to filter logs of a specific "source"
59+
#ENABLE_BROWSER_LOG=true
60+
#BROWSER_LOG_BLACKLIST=other
61+
5762
#*** End of .env ***#
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Extension;
8+
9+
/**
10+
* Class BrowserLogUtil
11+
* @package Magento\FunctionalTestingFramework\Extension
12+
*/
13+
class BrowserLogUtil
14+
{
15+
const LOG_TYPE_BROWSER = "browser";
16+
const ERROR_TYPE_JAVASCRIPT = "javascript";
17+
18+
/**
19+
* Loops throw errors in log and logs them to allure. Uses Module to set the error itself
20+
*
21+
* @param array $log
22+
* @param \Codeception\Module\WebDriver $module
23+
* @param \Codeception\Event\StepEvent $stepEvent
24+
* @return void
25+
*/
26+
public static function logErrors($log, $module, $stepEvent)
27+
{
28+
$jsErrors = self::getLogsOfType($log, self::ERROR_TYPE_JAVASCRIPT);
29+
foreach ($jsErrors as $entry) {
30+
self::logError(self::ERROR_TYPE_JAVASCRIPT, $stepEvent, $entry);
31+
//Set javascript error in MagentoWebDriver internal array
32+
$module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]);
33+
}
34+
}
35+
36+
/**
37+
* Loops through given log and returns entries of the given type.
38+
*
39+
* @param array $log
40+
* @param string $type
41+
* @return array
42+
*/
43+
public static function getLogsOfType($log, $type)
44+
{
45+
$errors = [];
46+
foreach ($log as $entry) {
47+
if (array_key_exists("source", $entry) && $entry["source"] === $type) {
48+
$errors[] = $entry;
49+
}
50+
}
51+
return $errors;
52+
}
53+
54+
/**
55+
* Loops through given log and filters entries of the given type.
56+
*
57+
* @param array $log
58+
* @param string $type
59+
* @return array
60+
*/
61+
public static function filterLogsOfType($log, $type)
62+
{
63+
$errors = [];
64+
foreach ($log as $entry) {
65+
if (array_key_exists("source", $entry) && $entry["source"] !== $type) {
66+
$errors[] = $entry;
67+
}
68+
}
69+
return $errors;
70+
}
71+
72+
/**
73+
* Logs errors to console/report.
74+
* @param string $type
75+
* @param \Codeception\Event\StepEvent $stepEvent
76+
* @param array $entry
77+
* @return void
78+
*/
79+
private static function logError($type, $stepEvent, $entry)
80+
{
81+
//TODO Add to overall log
82+
$stepEvent->getTest()->getScenario()->comment("{$type} ERROR({$entry["level"]}) - " . $entry["message"]);
83+
}
84+
}

src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php

Lines changed: 11 additions & 1 deletion
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,7 +174,16 @@ public function beforeStep(\Codeception\Event\StepEvent $e)
173174
*/
174175
public function afterStep(\Codeception\Event\StepEvent $e)
175176
{
176-
ErrorLogger::getInstance()->logErrors($this->getDriver(), $e);
177+
$browserLog = $this->getDriver()->webDriver->manage()->getLog("browser");
178+
if (getenv('ENABLE_BROWSER_LOG')) {
179+
foreach (explode(',', getenv('BROWSER_LOG_BLACKLIST')) as $source) {
180+
$browserLog = BrowserLogUtil::filterLogsOfType($browserLog, $source);
181+
}
182+
if (!empty($browserLog)) {
183+
AllureHelper::addAttachmentToCurrentStep(json_encode($browserLog, JSON_PRETTY_PRINT), "Browser Log");
184+
}
185+
}
186+
BrowserLogUtil::logErrors($browserLog, $this->getDriver(), $e);
177187
}
178188

179189
/**

0 commit comments

Comments
 (0)