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
2 changes: 2 additions & 0 deletions etc/config/codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ extensions:
- env
- zephyrId
- useCaseId
Magento\FunctionalTestingFramework\Extension\TestContextExtension:
driver: \Magento\FunctionalTestingFramework\Module\MagentoWebDriver
params:
- .env
104 changes: 104 additions & 0 deletions src/Magento/FunctionalTestingFramework/Extension/BaseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\FunctionalTestingFramework\Extension;

use Codeception\Events;
use Codeception\Exception\ModuleRequireException;
use Codeception\Extension;
use Codeception\Module\WebDriver;

/**
* Class BaseExtension
*/
class BaseExtension extends Extension
{
/**
* Codeception Events Mapping to methods
*
* @var array
*/
public static $events = [
Events::TEST_BEFORE => 'beforeTest',
Events::STEP_BEFORE => 'beforeStep'
];

/**
* The current URI of the active page
*
* @var string
*/
private $uri;

/**
* Codeception event listener function - initialize uri before test
*
* @param \Codeception\Event\TestEvent $e
* @return void
* @throws \Exception
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeTest(\Codeception\Event\TestEvent $e)
{
$this->uri = null;
}

/**
* Codeception event listener function - check for page uri change before step
*
* @param \Codeception\Event\StepEvent $e
* @return void
* @throws \Exception
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeStep(\Codeception\Event\StepEvent $e)
{
$this->pageChanged();
}

/**
* WebDriver instance for execution
*
* @return WebDriver
* @throws ModuleRequireException
*/
public function getDriver()
{
return $this->getModule($this->config['driver']);
}

/**
* Gets the active page URI from the start of the most recent step
*
* @return string
*/
public function getUri()
{
return $this->uri;
}

/**
* Check if page uri has changed
*
* @return boolean
*/
protected function pageChanged()
{
try {
$currentUri = $this->getDriver()->_getCurrentUri();

if ($this->uri !== $currentUri) {
$this->uri = $currentUri;
return true;
}
} catch (\Exception $e) {
// just fall through and return false
}
return false;
}
}
13 changes: 8 additions & 5 deletions src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@ 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.
if (in_array("browser", $webDriver->manage()->getAvailableLogTypes())) {
$browserLogEntries = $webDriver->manage()->getLog("browser");
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"]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

use Codeception\Event\StepEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Exception\ModuleRequireException;
use Codeception\Extension;
use Codeception\Module\WebDriver;
use Codeception\Step;
use Facebook\WebDriver\Exception\UnexpectedAlertOpenException;
use Magento\FunctionalTestingFramework\Extension\ReadinessMetrics\AbstractMetricCheck;
Expand All @@ -23,18 +19,8 @@
/**
* Class PageReadinessExtension
*/
class PageReadinessExtension extends Extension
class PageReadinessExtension extends BaseExtension
{
/**
* Codeception Events Mapping to methods
*
* @var array
*/
public static $events = [
Events::TEST_BEFORE => 'beforeTest',
Events::STEP_BEFORE => 'beforeStep'
];

/**
* List of action types that should bypass metric checks
* shouldSkipCheck() also checks for the 'Comment' step type, which doesn't follow the $step->getAction() pattern
Expand Down Expand Up @@ -73,13 +59,6 @@ class PageReadinessExtension extends Extension
*/
private $testName;

/**
* The current URI of the active page
*
* @var string
*/
private $uri;

/**
* Initialize local vars
*
Expand All @@ -90,35 +69,26 @@ public function _initialize()
{
$this->logger = LoggingUtil::getInstance()->getLogger(get_class($this));
$this->verbose = MftfApplicationConfig::getConfig()->verboseEnabled();
}

/**
* WebDriver instance to use to execute readiness metric checks
*
* @return WebDriver
* @throws ModuleRequireException
*/
public function getDriver()
{
return $this->getModule($this->config['driver']);
parent::_initialize();
}

/**
* Initialize the readiness metrics for the test
*
* @param \Codeception\Event\TestEvent $e
* @param TestEvent $e
* @return void
* @throws \Exception
*/
public function beforeTest(TestEvent $e)
{
parent::beforeTest($e);
if (isset($this->config['resetFailureThreshold'])) {
$failThreshold = intval($this->config['resetFailureThreshold']);
} else {
$failThreshold = 3;
}

$this->testName = $e->getTest()->getMetadata()->getName();
$this->uri = null;

$this->getDriver()->_setConfig(['skipReadiness' => false]);

Expand All @@ -136,6 +106,8 @@ public function beforeTest(TestEvent $e)
* @param StepEvent $e
* @return void
* @throws \Exception
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function beforeStep(StepEvent $e)
{
Expand All @@ -145,7 +117,20 @@ public function beforeStep(StepEvent $e)
return;
}

$this->checkForNewPage($step);
// Check if page has changed and reset metric tracking if so
if ($this->pageChanged($step)) {
$this->logDebug(
'Page URI changed; resetting readiness metric failure tracking',
[
'step' => $step->__toString(),
'newUri' => $this->getUri()
]
);
/** @var AbstractMetricCheck $metric */
foreach ($this->readinessMetrics as $metric) {
$metric->resetTracker();
}
}

// todo: Implement step parameter to override global timeout configuration
if (isset($this->config['timeout'])) {
Expand Down Expand Up @@ -182,48 +167,6 @@ function () use ($metrics) {
}
}

/**
* Check if the URI has changed and reset metric tracking if so
*
* @param Step $step
* @return void
*/
private function checkForNewPage($step)
{
try {
$currentUri = $this->getDriver()->_getCurrentUri();

if ($this->uri !== $currentUri) {
$this->logDebug(
'Page URI changed; resetting readiness metric failure tracking',
[
'step' => $step->__toString(),
'newUri' => $currentUri
]
);

/** @var AbstractMetricCheck $metric */
foreach ($this->readinessMetrics as $metric) {
$metric->resetTracker();
}

$this->uri = $currentUri;
}
} catch (\Exception $e) {
$this->logDebug('Could not retrieve current URI', ['step' => $step->__toString()]);
}
}

/**
* Gets the active page URI from the start of the most recent step
*
* @return string
*/
public function getUri()
{
return $this->uri;
}

/**
* Gets the name of the active test
*
Expand Down Expand Up @@ -263,7 +206,7 @@ private function logDebug($message, $context = [])
if ($this->verbose) {
$logContext = [
'test' => $this->testName,
'uri' => $this->uri
'uri' => $this->getUri()
];
foreach ($this->readinessMetrics as $metric) {
$logContext[$metric->getName()] = $metric->getStoredValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ protected function getDriver()
* @param string $script
* @param array $arguments
* @return mixed
* @throws UnexpectedAlertOpenException
* @throws ModuleRequireException
*/
protected function executeJs($script, $arguments = [])
Expand Down
Loading