-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathPageReadinessExtension.php
240 lines (215 loc) · 6.59 KB
/
PageReadinessExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Extension;
use Codeception\Event\StepEvent;
use Codeception\Event\TestEvent;
use Codeception\Step;
use Facebook\WebDriver\Exception\UnexpectedAlertOpenException;
use Magento\FunctionalTestingFramework\Extension\ReadinessMetrics\AbstractMetricCheck;
use Facebook\WebDriver\Exception\TimeOutException;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Monolog\Logger;
/**
* Class PageReadinessExtension
*/
class PageReadinessExtension extends BaseExtension
{
/**
* 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
*
* @var array
*/
private $ignoredActions = [
'saveScreenshot',
'skipReadinessCheck',
'wait'
];
/**
* @var Logger
*/
private $logger;
/**
* Logger verbosity
*
* @var boolean
*/
private $verbose;
/**
* Array of readiness metrics, initialized during beforeTest event
*
* @var AbstractMetricCheck[]
*/
private $readinessMetrics;
/**
* The name of the active test
*
* @var string
*/
private $testName;
/**
* Initialize local vars
*
* @return void
* @throws \Exception
*/
public function _initialize()
{
$this->logger = LoggingUtil::getInstance()->getLogger(get_class($this));
$this->verbose = MftfApplicationConfig::getConfig()->verboseEnabled();
parent::_initialize();
}
/**
* Initialize the readiness metrics for the test
*
* @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->getDriver()->_setConfig(['skipReadiness' => false]);
$metrics = [];
foreach ($this->config['readinessMetrics'] as $metricClass) {
$metrics[] = new $metricClass($this, $failThreshold);
}
$this->readinessMetrics = $metrics;
}
/**
* Waits for busy page flags to disappear before executing a step
*
* @param StepEvent $e
* @return void
* @throws \Exception
*/
public function beforeStep(StepEvent $e)
{
$step = $e->getStep();
$manualSkip = $this->getDriver()->_getConfig()['skipReadiness'];
if ($this->shouldSkipCheck($step, $manualSkip)) {
return;
}
$this->resetMetricTracker($step);
$metrics = $this->readinessMetrics;
$this->waitForReadiness($metrics);
/** @var AbstractMetricCheck $metric */
foreach ($metrics as $metric) {
$metric->finalizeForStep($step);
}
}
/**
* Check if page has changed, if so reset metric tracking
*
* @param Step $step
* @return void
*/
private function resetMetricTracker($step)
{
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();
}
}
}
/**
* Wait for page readiness.
* @param array $metrics
* @return void
* @throws \Codeception\Exception\ModuleRequireException
* @throws \Facebook\WebDriver\Exception\NoSuchElementException
*/
private function waitForReadiness($metrics)
{
// todo: Implement step parameter to override global timeout configuration
if (isset($this->config['timeout'])) {
$timeout = intval($this->config['timeout']);
} else {
$timeout = $this->getDriver()->_getConfig()['pageload_timeout'];
}
try {
$this->getDriver()->webDriver->wait($timeout)->until(
function () use ($metrics) {
$passing = true;
/** @var AbstractMetricCheck $metric */
foreach ($metrics as $metric) {
try {
if (!$metric->runCheck()) {
$passing = false;
}
} catch (UnexpectedAlertOpenException $exception) {
}
}
return $passing;
}
);
} catch (TimeoutException $exception) {
}
}
/**
* Gets the name of the active test
*
* @return string
*/
public function getTestName()
{
return $this->testName;
}
/**
* Should the given step bypass the readiness checks
* todo: Implement step parameter to bypass specific metrics (or all) instead of basing on action type
*
* @param Step $step
* @param boolean $manualSkip
* @return boolean
*/
private function shouldSkipCheck($step, $manualSkip)
{
if ($step instanceof Step\Comment || in_array($step->getAction(), $this->ignoredActions) || $manualSkip) {
return true;
}
return false;
}
/**
* If verbose, log the given message to logger->debug including test context information
*
* @param string $message
* @param array $context
* @return void
* @SuppressWarnings(PHPMD)
*/
private function logDebug($message, $context = [])
{
if ($this->verbose) {
$logContext = [
'test' => $this->testName,
'uri' => $this->getUri()
];
foreach ($this->readinessMetrics as $metric) {
$logContext[$metric->getName()] = $metric->getStoredValue();
$logContext[$metric->getName() . '.failCount'] = $metric->getFailureCount();
}
$context = array_merge($logContext, $context);
//TODO REMOVE THIS LINE, UNCOMMENT LOGGER
//$this->logger->info($message, $context);
}
}
}