Skip to content

Commit a10efc5

Browse files
committed
MQE-1117: dontSeeJsError does not catch JS errors
- bug fix
1 parent 31ddcc8 commit a10efc5

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ private function __construct()
4141

4242
/**
4343
* Loops through stepEvent for browser log entries
44-
* @param \Facebook\WebDriver\Remote\RemoteWebDriver $webDriver
45-
* @param \Codeception\Event\StepEvent $stepEvent
44+
*
45+
* @param \Magento\FunctionalTestingFramework\Module\MagentoWebDriver $module
46+
* @param \Codeception\Event\StepEvent $stepEvent
4647
* @return void
4748
*/
48-
public function logErrors($webDriver, $stepEvent)
49+
public function logErrors($module, $stepEvent)
4950
{
5051
//Types available should be "server", "browser", "driver". Only care about browser at the moment.
51-
$browserLogEntries = $webDriver->manage()->getLog("browser");
52+
$browserLogEntries = $module->webDriver->manage()->getLog("browser");
5253
foreach ($browserLogEntries as $entry) {
5354
if (array_key_exists("source", $entry) && $entry["source"] === "javascript") {
5455
$this->logError("javascript", $stepEvent, $entry);
56+
//Set javascript error in MagentoWebDriver internal array
57+
$module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]);
5558
}
5659
}
5760
}

src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ public function extractContext($trace, $class)
144144
public function afterStep(\Codeception\Event\StepEvent $e)
145145
{
146146
// @codingStandardsIgnoreStart
147-
$webDriver = $this->getModule("\Magento\FunctionalTestingFramework\Module\MagentoWebDriver")->webDriver;
147+
ErrorLogger::getInstance()->logErrors($this->getModule(self::MAGENTO_WEB_DRIVER_CLASS), $e);
148148
// @codingStandardsIgnoreEnd
149-
ErrorLogger::getInstance()->logErrors($webDriver, $e);
150149
}
151150
}

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ class MagentoWebDriver extends WebDriver
105105
*/
106106
private $htmlReport;
107107

108+
/**
109+
* Array to store Javascript errors
110+
*
111+
* @var string[]
112+
*/
113+
private $jsErrors = [];
114+
108115
/**
109116
* Sanitizes config, then initializes using parent.
110117
* @return void
@@ -113,6 +120,7 @@ public function _initialize()
113120
{
114121
$this->config = ConfigSanitizerUtil::sanitizeWebDriverConfig($this->config);
115122
parent::_initialize();
123+
$this->cleanJsError();
116124
}
117125

118126
/**
@@ -124,6 +132,7 @@ public function _resetConfig()
124132
{
125133
parent::_resetConfig();
126134
$this->config = ConfigSanitizerUtil::sanitizeWebDriverConfig($this->config);
135+
$this->cleanJsError();
127136
}
128137

129138
/**
@@ -390,22 +399,6 @@ public function waitForLoadingMaskToDisappear()
390399
}
391400
}
392401

393-
/**
394-
* Verify that there are no JavaScript errors in the console.
395-
*
396-
* @throws ModuleException
397-
* @return void
398-
*/
399-
public function dontSeeJsError()
400-
{
401-
$logs = $this->webDriver->manage()->getLog('browser');
402-
foreach ($logs as $log) {
403-
if ($log['level'] == 'SEVERE') {
404-
throw new ModuleException($this, 'Errors in JavaScript: ' . json_encode($log));
405-
}
406-
}
407-
}
408-
409402
/**
410403
* @param float $money
411404
* @param string $locale
@@ -708,4 +701,53 @@ public function skipReadinessCheck($check)
708701
{
709702
$this->config['skipReadiness'] = $check;
710703
}
704+
705+
/**
706+
* Clean Javascript errors in internal array
707+
*
708+
* @return void
709+
*/
710+
private function cleanJsError()
711+
{
712+
$this->jsErrors = [];
713+
}
714+
715+
/**
716+
* Save Javascript error message to internal array
717+
*
718+
* @param string $errMsg
719+
* @return void
720+
*/
721+
public function setJsError($errMsg)
722+
{
723+
$this->jsErrors[] = $errMsg;
724+
}
725+
726+
/**
727+
* Get all Javascript errors
728+
*
729+
* @return string
730+
*/
731+
private function getJsErrors()
732+
{
733+
$errors = '';
734+
735+
if (!empty($this->jsErrors)) {
736+
$errors = 'Errors in JavaScript:';
737+
foreach ($this->jsErrors as $jsError) {
738+
$errors .= "\n" . $jsError;
739+
}
740+
}
741+
return $errors;
742+
}
743+
744+
/**
745+
* Verify that there is no JavaScript error in browser logs
746+
*
747+
* @return void
748+
*/
749+
public function dontSeeJsError()
750+
{
751+
$this->assertEmpty($this->jsErrors, $this->getJsErrors());
752+
}
711753
}

0 commit comments

Comments
 (0)