Skip to content

Commit 26487bc

Browse files
authored
MQE-1117: dontSeeJsError does not catch JS errors (#223)
* MQE-1117: dontSeeJsError does not catch JS errors - changed to only check js errors in current page - bug fix
1 parent 94afc74 commit 26487bc

File tree

7 files changed

+236
-121
lines changed

7 files changed

+236
-121
lines changed

etc/config/codeception.dist.yml

+2
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ extensions:
2323
- env
2424
- zephyrId
2525
- useCaseId
26+
Magento\FunctionalTestingFramework\Extension\TestContextExtension:
27+
driver: \Magento\FunctionalTestingFramework\Module\MagentoWebDriver
2628
params:
2729
- .env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
use Codeception\Events;
10+
use Codeception\Exception\ModuleRequireException;
11+
use Codeception\Extension;
12+
use Codeception\Module\WebDriver;
13+
14+
/**
15+
* Class BaseExtension
16+
*/
17+
class BaseExtension extends Extension
18+
{
19+
/**
20+
* Codeception Events Mapping to methods
21+
*
22+
* @var array
23+
*/
24+
public static $events = [
25+
Events::TEST_BEFORE => 'beforeTest',
26+
Events::STEP_BEFORE => 'beforeStep'
27+
];
28+
29+
/**
30+
* The current URI of the active page
31+
*
32+
* @var string
33+
*/
34+
private $uri;
35+
36+
/**
37+
* Codeception event listener function - initialize uri before test
38+
*
39+
* @param \Codeception\Event\TestEvent $e
40+
* @return void
41+
* @throws \Exception
42+
*
43+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
44+
*/
45+
public function beforeTest(\Codeception\Event\TestEvent $e)
46+
{
47+
$this->uri = null;
48+
}
49+
50+
/**
51+
* Codeception event listener function - check for page uri change before step
52+
*
53+
* @param \Codeception\Event\StepEvent $e
54+
* @return void
55+
* @throws \Exception
56+
*
57+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
58+
*/
59+
public function beforeStep(\Codeception\Event\StepEvent $e)
60+
{
61+
$this->pageChanged();
62+
}
63+
64+
/**
65+
* WebDriver instance for execution
66+
*
67+
* @return WebDriver
68+
* @throws ModuleRequireException
69+
*/
70+
public function getDriver()
71+
{
72+
return $this->getModule($this->config['driver']);
73+
}
74+
75+
/**
76+
* Gets the active page URI from the start of the most recent step
77+
*
78+
* @return string
79+
*/
80+
public function getUri()
81+
{
82+
return $this->uri;
83+
}
84+
85+
/**
86+
* Check if page uri has changed
87+
*
88+
* @return boolean
89+
*/
90+
protected function pageChanged()
91+
{
92+
try {
93+
$currentUri = $this->getDriver()->_getCurrentUri();
94+
95+
if ($this->uri !== $currentUri) {
96+
$this->uri = $currentUri;
97+
return true;
98+
}
99+
} catch (\Exception $e) {
100+
// just fall through and return false
101+
}
102+
return false;
103+
}
104+
}

src/Magento/FunctionalTestingFramework/Extension/ErrorLogger.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@ 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-
if (in_array("browser", $webDriver->manage()->getAvailableLogTypes())) {
52-
$browserLogEntries = $webDriver->manage()->getLog("browser");
52+
if (in_array("browser", $module->$webDriver->manage()->getAvailableLogTypes())) {
53+
$browserLogEntries = $module->$webDriver->manage()->getLog("browser");
5354
foreach ($browserLogEntries as $entry) {
5455
if (array_key_exists("source", $entry) && $entry["source"] === "javascript") {
5556
$this->logError("javascript", $stepEvent, $entry);
57+
//Set javascript error in MagentoWebDriver internal array
58+
$module->setJsError("ERROR({$entry["level"]}) - " . $entry["message"]);
5659
}
5760
}
5861
}

src/Magento/FunctionalTestingFramework/Extension/PageReadinessExtension.php

+22-79
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
use Codeception\Event\StepEvent;
1010
use Codeception\Event\TestEvent;
11-
use Codeception\Events;
12-
use Codeception\Exception\ModuleRequireException;
13-
use Codeception\Extension;
14-
use Codeception\Module\WebDriver;
1511
use Codeception\Step;
1612
use Facebook\WebDriver\Exception\UnexpectedAlertOpenException;
1713
use Magento\FunctionalTestingFramework\Extension\ReadinessMetrics\AbstractMetricCheck;
@@ -23,18 +19,8 @@
2319
/**
2420
* Class PageReadinessExtension
2521
*/
26-
class PageReadinessExtension extends Extension
22+
class PageReadinessExtension extends BaseExtension
2723
{
28-
/**
29-
* Codeception Events Mapping to methods
30-
*
31-
* @var array
32-
*/
33-
public static $events = [
34-
Events::TEST_BEFORE => 'beforeTest',
35-
Events::STEP_BEFORE => 'beforeStep'
36-
];
37-
3824
/**
3925
* List of action types that should bypass metric checks
4026
* shouldSkipCheck() also checks for the 'Comment' step type, which doesn't follow the $step->getAction() pattern
@@ -73,13 +59,6 @@ class PageReadinessExtension extends Extension
7359
*/
7460
private $testName;
7561

76-
/**
77-
* The current URI of the active page
78-
*
79-
* @var string
80-
*/
81-
private $uri;
82-
8362
/**
8463
* Initialize local vars
8564
*
@@ -90,35 +69,26 @@ public function _initialize()
9069
{
9170
$this->logger = LoggingUtil::getInstance()->getLogger(get_class($this));
9271
$this->verbose = MftfApplicationConfig::getConfig()->verboseEnabled();
93-
}
94-
95-
/**
96-
* WebDriver instance to use to execute readiness metric checks
97-
*
98-
* @return WebDriver
99-
* @throws ModuleRequireException
100-
*/
101-
public function getDriver()
102-
{
103-
return $this->getModule($this->config['driver']);
72+
parent::_initialize();
10473
}
10574

10675
/**
10776
* Initialize the readiness metrics for the test
10877
*
109-
* @param \Codeception\Event\TestEvent $e
78+
* @param TestEvent $e
11079
* @return void
80+
* @throws \Exception
11181
*/
11282
public function beforeTest(TestEvent $e)
11383
{
84+
parent::beforeTest($e);
11485
if (isset($this->config['resetFailureThreshold'])) {
11586
$failThreshold = intval($this->config['resetFailureThreshold']);
11687
} else {
11788
$failThreshold = 3;
11889
}
11990

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

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

@@ -136,6 +106,8 @@ public function beforeTest(TestEvent $e)
136106
* @param StepEvent $e
137107
* @return void
138108
* @throws \Exception
109+
*
110+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
139111
*/
140112
public function beforeStep(StepEvent $e)
141113
{
@@ -145,7 +117,20 @@ public function beforeStep(StepEvent $e)
145117
return;
146118
}
147119

148-
$this->checkForNewPage($step);
120+
// Check if page has changed and reset metric tracking if so
121+
if ($this->pageChanged($step)) {
122+
$this->logDebug(
123+
'Page URI changed; resetting readiness metric failure tracking',
124+
[
125+
'step' => $step->__toString(),
126+
'newUri' => $this->getUri()
127+
]
128+
);
129+
/** @var AbstractMetricCheck $metric */
130+
foreach ($this->readinessMetrics as $metric) {
131+
$metric->resetTracker();
132+
}
133+
}
149134

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

185-
/**
186-
* Check if the URI has changed and reset metric tracking if so
187-
*
188-
* @param Step $step
189-
* @return void
190-
*/
191-
private function checkForNewPage($step)
192-
{
193-
try {
194-
$currentUri = $this->getDriver()->_getCurrentUri();
195-
196-
if ($this->uri !== $currentUri) {
197-
$this->logDebug(
198-
'Page URI changed; resetting readiness metric failure tracking',
199-
[
200-
'step' => $step->__toString(),
201-
'newUri' => $currentUri
202-
]
203-
);
204-
205-
/** @var AbstractMetricCheck $metric */
206-
foreach ($this->readinessMetrics as $metric) {
207-
$metric->resetTracker();
208-
}
209-
210-
$this->uri = $currentUri;
211-
}
212-
} catch (\Exception $e) {
213-
$this->logDebug('Could not retrieve current URI', ['step' => $step->__toString()]);
214-
}
215-
}
216-
217-
/**
218-
* Gets the active page URI from the start of the most recent step
219-
*
220-
* @return string
221-
*/
222-
public function getUri()
223-
{
224-
return $this->uri;
225-
}
226-
227170
/**
228171
* Gets the name of the active test
229172
*
@@ -263,7 +206,7 @@ private function logDebug($message, $context = [])
263206
if ($this->verbose) {
264207
$logContext = [
265208
'test' => $this->testName,
266-
'uri' => $this->uri
209+
'uri' => $this->getUri()
267210
];
268211
foreach ($this->readinessMetrics as $metric) {
269212
$logContext[$metric->getName()] = $metric->getStoredValue();

src/Magento/FunctionalTestingFramework/Extension/ReadinessMetrics/AbstractMetricCheck.php

-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ protected function getDriver()
214214
* @param string $script
215215
* @param array $arguments
216216
* @return mixed
217-
* @throws UnexpectedAlertOpenException
218217
* @throws ModuleRequireException
219218
*/
220219
protected function executeJs($script, $arguments = [])

0 commit comments

Comments
 (0)