Skip to content

MQE-1117: dontSeeJsError does not catch JS errors #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 28, 2018
11 changes: 7 additions & 4 deletions src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ private function __construct()

/**
* Loops through stepEvent for browser log entries
* @param \Facebook\WebDriver\Remote\RemoteWebDriver $webDriver
* @param \Codeception\Event\StepEvent $stepEvent
*
* @param \Magento\FunctionalTestingFramework\Module\MagentoWebDriver $module
* @param \Codeception\Event\StepEvent $stepEvent
* @return void
*/
public function logErrors($webDriver, $stepEvent)
public function logErrors($module, $stepEvent)
{
//Types available should be "server", "browser", "driver". Only care about browser at the moment.
$browserLogEntries = $webDriver->manage()->getLog("browser");
$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"]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ public function extractContext($trace, $class)
public function afterStep(\Codeception\Event\StepEvent $e)
{
// @codingStandardsIgnoreStart
$webDriver = $this->getModule("\Magento\FunctionalTestingFramework\Module\MagentoWebDriver")->webDriver;
ErrorLogger::getInstance()->logErrors($this->getModule(self::MAGENTO_WEB_DRIVER_CLASS), $e);
// @codingStandardsIgnoreEnd
ErrorLogger::getInstance()->logErrors($webDriver, $e);
}
}
74 changes: 58 additions & 16 deletions src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ class MagentoWebDriver extends WebDriver
*/
private $htmlReport;

/**
* Array to store Javascript errors
*
* @var string[]
*/
private $jsErrors = [];

/**
* Sanitizes config, then initializes using parent.
* @return void
Expand All @@ -113,6 +120,7 @@ public function _initialize()
{
$this->config = ConfigSanitizerUtil::sanitizeWebDriverConfig($this->config);
parent::_initialize();
$this->cleanJsError();
}

/**
Expand All @@ -124,6 +132,7 @@ public function _resetConfig()
{
parent::_resetConfig();
$this->config = ConfigSanitizerUtil::sanitizeWebDriverConfig($this->config);
$this->cleanJsError();
}

/**
Expand Down Expand Up @@ -390,22 +399,6 @@ public function waitForLoadingMaskToDisappear()
}
}

/**
* Verify that there are no JavaScript errors in the console.
*
* @throws ModuleException
* @return void
*/
public function dontSeeJsError()
{
$logs = $this->webDriver->manage()->getLog('browser');
foreach ($logs as $log) {
if ($log['level'] == 'SEVERE') {
throw new ModuleException($this, 'Errors in JavaScript: ' . json_encode($log));
}
}
}

/**
* @param float $money
* @param string $locale
Expand Down Expand Up @@ -708,4 +701,53 @@ public function skipReadinessCheck($check)
{
$this->config['skipReadiness'] = $check;
}

/**
* Clean Javascript errors in internal array
*
* @return void
*/
private function cleanJsError()
{
$this->jsErrors = [];
}

/**
* Save Javascript error message to internal array
*
* @param string $errMsg
* @return void
*/
public function setJsError($errMsg)
{
$this->jsErrors[] = $errMsg;
}

/**
* Get all Javascript errors
*
* @return string
*/
private function getJsErrors()
{
$errors = '';

if (!empty($this->jsErrors)) {
$errors = 'Errors in JavaScript:';
foreach ($this->jsErrors as $jsError) {
$errors .= "\n" . $jsError;
}
}
return $errors;
}

/**
* Verify that there is no JavaScript error in browser logs
*
* @return void
*/
public function dontSeeJsError()
{
$this->assertEmpty($this->jsErrors, $this->getJsErrors());
}
}