Skip to content

Commit 8b2fa37

Browse files
committed
MQE-1117: dontSeeJsError does not catch JS errors
- changed to only check js errors in current page
1 parent 8634cbd commit 8b2fa37

File tree

6 files changed

+165
-99
lines changed

6 files changed

+165
-99
lines changed

etc/config/codeception.dist.yml

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

src/Magento/FunctionalTestingFramework/Extension/PageReadinessExtension.php

Lines changed: 20 additions & 79 deletions
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

@@ -145,7 +115,20 @@ public function beforeStep(StepEvent $e)
145115
return;
146116
}
147117

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

150133
// todo: Implement step parameter to override global timeout configuration
151134
if (isset($this->config['timeout'])) {
@@ -182,48 +165,6 @@ function () use ($metrics) {
182165
}
183166
}
184167

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-
227168
/**
228169
* Gets the name of the active test
229170
*
@@ -263,7 +204,7 @@ private function logDebug($message, $context = [])
263204
if ($this->verbose) {
264205
$logContext = [
265206
'test' => $this->testName,
266-
'uri' => $this->uri
207+
'uri' => $this->getUri()
267208
];
268209
foreach ($this->readinessMetrics as $metric) {
269210
$logContext[$metric->getName()] = $metric->getStoredValue();

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

Lines changed: 0 additions & 1 deletion
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 = [])

src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,40 @@
66

77
namespace Magento\FunctionalTestingFramework\Extension;
88

9-
use \Codeception\Events;
9+
use Codeception\Events;
1010
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
11-
use Magento\FunctionalTestingFramework\Extension\ErrorLogger;
12-
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;
1311

1412
/**
1513
* Class TestContextExtension
1614
* @SuppressWarnings(PHPMD.UnusedPrivateField)
1715
*/
18-
class TestContextExtension extends \Codeception\Extension
16+
class TestContextExtension extends BaseExtension
1917
{
2018
const TEST_PHASE_AFTER = "_after";
21-
// @codingStandardsIgnoreStart
22-
const MAGENTO_WEB_DRIVER_CLASS = "\Magento\FunctionalTestingFramework\Module\MagentoWebDriver";
23-
// @codingStandardsIgnoreEnd
2419

2520
/**
2621
* Codeception Events Mapping to methods
2722
* @var array
2823
*/
29-
public static $events = [
30-
Events::TEST_START => 'testStart',
31-
Events::TEST_FAIL => 'testFail',
32-
Events::STEP_AFTER => 'afterStep',
33-
Events::TEST_END => 'testEnd'
34-
];
24+
public static $events;
25+
26+
/**
27+
* Initialize local vars
28+
*
29+
* @return void
30+
* @throws \Exception
31+
*/
32+
public function _initialize()
33+
{
34+
$events = [
35+
Events::TEST_START => 'testStart',
36+
Events::TEST_FAIL => 'testFail',
37+
Events::STEP_AFTER => 'afterStep',
38+
Events::TEST_END => 'testEnd'
39+
];
40+
self::$events = array_merge(parent::$events, $events);
41+
parent::_initialize();
42+
}
3543

3644
/**
3745
* Codeception event listener function, triggered on test start.
@@ -64,6 +72,7 @@ public function testFail(\Codeception\Event\FailEvent $e)
6472
* Codeception event listener function, triggered on test ending (naturally or by error).
6573
* @param \Codeception\Event\TestEvent $e
6674
* @return void
75+
* @throws \Exception
6776
*/
6877
public function testEnd(\Codeception\Event\TestEvent $e)
6978
{
@@ -92,12 +101,12 @@ function () use ($cest) {
92101
}
93102
}
94103
// Reset Session and Cookies after all Test Runs, workaround due to functional.suite.yml restart: true
95-
$this->getModule(self::MAGENTO_WEB_DRIVER_CLASS)->_runAfter($e->getTest());
104+
$this->getDriver()->_runAfter($e->getTest());
96105
}
97106

98107
/**
99108
* Runs cest's after block, if necessary.
100-
* @param Symfony\Component\EventDispatcher\Event $e
109+
* @param \Symfony\Component\EventDispatcher\Event $e
101110
* @param \Codeception\TestInterface $cest
102111
* @return void
103112
*/
@@ -135,16 +144,30 @@ public function extractContext($trace, $class)
135144
return null;
136145
}
137146

147+
/**
148+
* Codeception event listener function, triggered before step.
149+
* Check if it's a new page.
150+
*
151+
* @param \Codeception\Event\StepEvent $e
152+
* @return void
153+
* @throws \Exception
154+
*/
155+
public function beforeStep(\Codeception\Event\StepEvent $e)
156+
{
157+
if ($this->pageChanged($e->getStep())) {
158+
$this->getDriver()->cleanJsError();
159+
}
160+
}
161+
138162
/**
139163
* Codeception event listener function, triggered after step.
140164
* Calls ErrorLogger to log JS errors encountered.
141165
* @param \Codeception\Event\StepEvent $e
142166
* @return void
167+
* @throws \Exception
143168
*/
144169
public function afterStep(\Codeception\Event\StepEvent $e)
145170
{
146-
// @codingStandardsIgnoreStart
147-
ErrorLogger::getInstance()->logErrors($this->getModule(self::MAGENTO_WEB_DRIVER_CLASS), $e);
148-
// @codingStandardsIgnoreEnd
171+
ErrorLogger::getInstance()->logErrors($this->getDriver(), $e);
149172
}
150173
}

0 commit comments

Comments
 (0)