8
8
9
9
use Codeception \Event \StepEvent ;
10
10
use Codeception \Event \TestEvent ;
11
- use Codeception \Events ;
12
- use Codeception \Exception \ModuleRequireException ;
13
- use Codeception \Extension ;
14
- use Codeception \Module \WebDriver ;
15
11
use Codeception \Step ;
16
12
use Facebook \WebDriver \Exception \UnexpectedAlertOpenException ;
17
13
use Magento \FunctionalTestingFramework \Extension \ReadinessMetrics \AbstractMetricCheck ;
23
19
/**
24
20
* Class PageReadinessExtension
25
21
*/
26
- class PageReadinessExtension extends Extension
22
+ class PageReadinessExtension extends BaseExtension
27
23
{
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
-
38
24
/**
39
25
* List of action types that should bypass metric checks
40
26
* shouldSkipCheck() also checks for the 'Comment' step type, which doesn't follow the $step->getAction() pattern
@@ -73,13 +59,6 @@ class PageReadinessExtension extends Extension
73
59
*/
74
60
private $ testName ;
75
61
76
- /**
77
- * The current URI of the active page
78
- *
79
- * @var string
80
- */
81
- private $ uri ;
82
-
83
62
/**
84
63
* Initialize local vars
85
64
*
@@ -90,35 +69,26 @@ public function _initialize()
90
69
{
91
70
$ this ->logger = LoggingUtil::getInstance ()->getLogger (get_class ($ this ));
92
71
$ 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 ();
104
73
}
105
74
106
75
/**
107
76
* Initialize the readiness metrics for the test
108
77
*
109
- * @param \Codeception\Event\ TestEvent $e
78
+ * @param TestEvent $e
110
79
* @return void
80
+ * @throws \Exception
111
81
*/
112
82
public function beforeTest (TestEvent $ e )
113
83
{
84
+ parent ::beforeTest ($ e );
114
85
if (isset ($ this ->config ['resetFailureThreshold ' ])) {
115
86
$ failThreshold = intval ($ this ->config ['resetFailureThreshold ' ]);
116
87
} else {
117
88
$ failThreshold = 3 ;
118
89
}
119
90
120
91
$ this ->testName = $ e ->getTest ()->getMetadata ()->getName ();
121
- $ this ->uri = null ;
122
92
123
93
$ this ->getDriver ()->_setConfig (['skipReadiness ' => false ]);
124
94
@@ -136,6 +106,8 @@ public function beforeTest(TestEvent $e)
136
106
* @param StepEvent $e
137
107
* @return void
138
108
* @throws \Exception
109
+ *
110
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
139
111
*/
140
112
public function beforeStep (StepEvent $ e )
141
113
{
@@ -145,7 +117,20 @@ public function beforeStep(StepEvent $e)
145
117
return ;
146
118
}
147
119
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
+ }
149
134
150
135
// todo: Implement step parameter to override global timeout configuration
151
136
if (isset ($ this ->config ['timeout ' ])) {
@@ -182,48 +167,6 @@ function () use ($metrics) {
182
167
}
183
168
}
184
169
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
-
227
170
/**
228
171
* Gets the name of the active test
229
172
*
@@ -263,7 +206,7 @@ private function logDebug($message, $context = [])
263
206
if ($ this ->verbose ) {
264
207
$ logContext = [
265
208
'test ' => $ this ->testName ,
266
- 'uri ' => $ this ->uri
209
+ 'uri ' => $ this ->getUri ()
267
210
];
268
211
foreach ($ this ->readinessMetrics as $ metric ) {
269
212
$ logContext [$ metric ->getName ()] = $ metric ->getStoredValue ();
0 commit comments