-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathTestContextExtension.php
265 lines (245 loc) · 8.86 KB
/
TestContextExtension.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Extension;
use Codeception\Events;
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
/**
* Class TestContextExtension
* @SuppressWarnings(PHPMD.UnusedPrivateField)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TestContextExtension extends BaseExtension
{
const TEST_PHASE_AFTER = "_after";
const CODECEPT_AFTER_VERSION = "2.3.9";
const TEST_FAILED_FILE = 'failed';
/**
* Codeception Events Mapping to methods
* @var array
*/
public static $events;
/**
* Initialize local vars
*
* @return void
* @throws \Exception
*/
public function _initialize()
{
$events = [
Events::TEST_START => 'testStart',
Events::TEST_FAIL => 'testFail',
Events::STEP_AFTER => 'afterStep',
Events::TEST_END => 'testEnd',
Events::RESULT_PRINT_AFTER => 'saveFailed'
];
self::$events = array_merge(parent::$events, $events);
parent::_initialize();
}
/**
* Codeception event listener function, triggered on test start.
* @throws \Exception
* @return void
*/
public function testStart()
{
PersistedObjectHandler::getInstance()->clearHookObjects();
PersistedObjectHandler::getInstance()->clearTestObjects();
}
/**
* Codeception event listener function, triggered on test failure.
* @param \Codeception\Event\FailEvent $e
* @return void
*/
public function testFail(\Codeception\Event\FailEvent $e)
{
$cest = $e->getTest();
//log suppressed exception in case of _after hook failure
$this->logPreviousException($e->getFail());
$context = $this->extractContext($e->getFail()->getTrace(), $cest->getTestMethod());
// Do not attempt to run _after if failure was in the _after block
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
$this->runAfterBlock($e, $cest);
}
}
/**
* Codeception event listener function, triggered on test ending (naturally or by error).
* @param \Codeception\Event\TestEvent $e
* @return void
* @throws \Exception
*/
public function testEnd(\Codeception\Event\TestEvent $e)
{
$cest = $e->getTest();
//Access private TestResultObject to find stack and if there are any errors (as opposed to failures)
$testResultObject = call_user_func(\Closure::bind(
function () use ($cest) {
return $cest->getTestResultObject();
},
$cest
));
$errors = $testResultObject->errors();
if (!empty($errors)) {
foreach ($errors as $error) {
if ($error->failedTest()->getTestMethod() == $cest->getName()) {
//log suppressed exception in case of _after hook failure
$this->logPreviousException($error->thrownException());
$stack = $error->thrownException()->getTrace();
$context = $this->extractContext($stack, $cest->getTestMethod());
// Do not attempt to run _after if failure was in the _after block
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
$this->runAfterBlock($e, $cest);
}
continue;
}
}
}
// Reset Session and Cookies after all Test Runs, workaround due to functional.suite.yml restart: true
$this->getDriver()->_runAfter($e->getTest());
}
/**
* Runs cest's after block, if necessary.
* @param \Symfony\Component\EventDispatcher\Event $e
* @param \Codeception\TestInterface $cest
* @return void
*/
private function runAfterBlock($e, $cest)
{
try {
$actorClass = $e->getTest()->getMetadata()->getCurrent('actor');
$I = new $actorClass($cest->getScenario());
if (version_compare(\Codeception\Codecept::VERSION, TestContextExtension::CODECEPT_AFTER_VERSION, "<=")) {
call_user_func(\Closure::bind(
function () use ($cest, $I) {
$cest->executeHook($I, 'after');
},
null,
$cest
));
}
} catch (\Exception $e) {
// Do not rethrow Exception
}
}
/**
* Extracts hook method from trace, looking specifically for the cest class given.
* @param array $trace
* @param string $class
* @return string
*/
public function extractContext($trace, $class)
{
foreach ($trace as $entry) {
$traceClass = $entry["class"] ?? null;
if (strpos($traceClass, $class) != 0) {
return $entry["function"];
}
}
return null;
}
/**
* Attach suppressed exception thrown before _after hook to the current step.
* @param \Exception $exception
* @return mixed
*/
public function logPreviousException(\Exception $exception)
{
$change = function () {
if ($this instanceof \PHPUnit\Framework\ExceptionWrapper) {
return $this->previous;
} else {
return $this->getPrevious();
}
};
$firstException = $change->call($exception);
$bind = function () use ($firstException) {
$exception = $firstException;
};
$bind->call($exception);
if ($firstException !== null) {
AllureHelper::addAttachmentToCurrentStep($firstException, 'Exception');
}
}
/**
* Codeception event listener function, triggered before step.
* Check if it's a new page.
*
* @param \Codeception\Event\StepEvent $e
* @return void
* @throws \Exception
*/
public function beforeStep(\Codeception\Event\StepEvent $e)
{
if ($this->pageChanged($e->getStep())) {
$this->getDriver()->cleanJsError();
}
}
/**
* Codeception event listener function, triggered after step.
* Calls ErrorLogger to log JS errors encountered.
* @param \Codeception\Event\StepEvent $e
* @return void
* @throws \Exception
*/
public function afterStep(\Codeception\Event\StepEvent $e)
{
$browserLog = $this->getDriver()->webDriver->manage()->getLog("browser");
if (getenv('ENABLE_BROWSER_LOG')) {
foreach (explode(',', getenv('BROWSER_LOG_BLACKLIST')) as $source) {
$browserLog = BrowserLogUtil::filterLogsOfType($browserLog, $source);
}
if (!empty($browserLog)) {
AllureHelper::addAttachmentToCurrentStep(json_encode($browserLog, JSON_PRETTY_PRINT), "Browser Log");
}
}
BrowserLogUtil::logErrors($browserLog, $this->getDriver(), $e);
}
/**
* Saves failed tests from last codecept run command into a file in _output directory
* Removes file if there were no failures in last run command
* @param \Codeception\Event\PrintResultEvent $e
* @return void
*/
public function saveFailed(\Codeception\Event\PrintResultEvent $e)
{
$file = $this->getLogDir() . self::TEST_FAILED_FILE;
$result = $e->getResult();
$output = [];
// Remove previous file regardless if we're writing a new file
if (is_file($file)) {
unlink($file);
}
foreach ($result->failures() as $fail) {
$output[] = $this->localizePath(\Codeception\Test\Descriptor::getTestFullName($fail->failedTest()));
}
foreach ($result->errors() as $fail) {
$output[] = $this->localizePath(\Codeception\Test\Descriptor::getTestFullName($fail->failedTest()));
}
foreach ($result->notImplemented() as $fail) {
$output[] = $this->localizePath(\Codeception\Test\Descriptor::getTestFullName($fail->failedTest()));
}
if (empty($output)) {
return;
}
file_put_contents($file, implode("\n", $output));
}
/**
* Returns localized path to string, for writing failed file.
* @param string $path
* @return string
*/
protected function localizePath($path)
{
$root = realpath($this->getRootDir()) . DIRECTORY_SEPARATOR;
if (substr($path, 0, strlen($root)) == $root) {
return substr($path, strlen($root));
}
return $path;
}
}