17
17
class TestContextExtension extends \Codeception \Extension
18
18
{
19
19
const TEST_PHASE_AFTER = "_after " ;
20
+ // @codingStandardsIgnoreStart
21
+ const MAGENTO_WEB_DRIVER_CLASS = "\Magento\FunctionalTestingFramework\Module\MagentoWebDriver " ;
22
+ // @codingStandardsIgnoreEnd
20
23
21
24
/**
22
25
* Codeception Events Mapping to methods
@@ -43,7 +46,7 @@ public function testFail(\Codeception\Event\FailEvent $e)
43
46
$ this ->runAfterBlock ($ e , $ cest );
44
47
}
45
48
}
46
-
49
+
47
50
/**
48
51
* Codeception event listener function, triggered on test error.
49
52
* @param \Codeception\Event\TestEvent $e
@@ -62,14 +65,21 @@ function () use ($cest) {
62
65
));
63
66
$ errors = $ testResultObject ->errors ();
64
67
if (!empty ($ errors )) {
65
- $ stack = $ errors [0 ]->thrownException ()->getTrace ();
66
- $ context = $ this ->extractContext ($ stack , $ cest ->getTestMethod ());
67
- // Do not attempt to run _after if failure was in the _after block
68
- // Try to run _after but catch exceptions to prevent them from overwriting original failure.
69
- if ($ context != TestContextExtension::TEST_PHASE_AFTER ) {
70
- $ this ->runAfterBlock ($ e , $ cest );
68
+ foreach ($ errors as $ error ) {
69
+ if ($ error ->failedTest ()->getTestMethod () == $ cest ->getName ()) {
70
+ $ stack = $ errors [0 ]->thrownException ()->getTrace ();
71
+ $ context = $ this ->extractContext ($ stack , $ cest ->getTestMethod ());
72
+ // Do not attempt to run _after if failure was in the _after block
73
+ // Try to run _after but catch exceptions to prevent them from overwriting original failure.
74
+ if ($ context != TestContextExtension::TEST_PHASE_AFTER ) {
75
+ $ this ->runAfterBlock ($ e , $ cest );
76
+ }
77
+ continue ;
78
+ }
71
79
}
72
80
}
81
+ // Reset Session and Cookies after all Test Runs, workaround due to functional.suite.yml restart: true
82
+ $ this ->getModule (self ::MAGENTO_WEB_DRIVER_CLASS )->_runAfter ($ e ->getTest ());
73
83
}
74
84
75
85
/**
@@ -90,8 +100,6 @@ function () use ($cest, $I) {
90
100
null ,
91
101
$ cest
92
102
));
93
- // Reset Session and Cookies, workaround due to functional.suite.yml restart: true
94
- $ this ->getModule (MagentoWebDriver::class)->_runAfter ($ e ->getTest ());
95
103
} catch (\Exception $ e ) {
96
104
// Do not rethrow Exception
97
105
}
0 commit comments