1717class TestContextExtension extends \Codeception \Extension
1818{
1919 const TEST_PHASE_AFTER = "_after " ;
20+ // @codingStandardsIgnoreStart
21+ const MAGENTO_WEB_DRIVER_CLASS = "\Magento\FunctionalTestingFramework\Module\MagentoWebDriver " ;
22+ // @codingStandardsIgnoreEnd
2023
2124 /**
2225 * Codeception Events Mapping to methods
@@ -43,7 +46,7 @@ public function testFail(\Codeception\Event\FailEvent $e)
4346 $ this ->runAfterBlock ($ e , $ cest );
4447 }
4548 }
46-
49+
4750 /**
4851 * Codeception event listener function, triggered on test error.
4952 * @param \Codeception\Event\TestEvent $e
@@ -62,14 +65,21 @@ function () use ($cest) {
6265 ));
6366 $ errors = $ testResultObject ->errors ();
6467 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+ }
7179 }
7280 }
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 ());
7383 }
7484
7585 /**
@@ -90,8 +100,6 @@ function () use ($cest, $I) {
90100 null ,
91101 $ cest
92102 ));
93- // Reset Session and Cookies, workaround due to functional.suite.yml restart: true
94- $ this ->getModule (MagentoWebDriver::class)->_runAfter ($ e ->getTest ());
95103 } catch (\Exception $ e ) {
96104 // Do not rethrow Exception
97105 }
0 commit comments