Skip to content

Commit 1e37e95

Browse files
authored
MQE-2162: Chrome remains running after MFTF suite finishes
1 parent e36fc50 commit 1e37e95

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

etc/config/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ MAGENTO_ADMIN_PASSWORD=123123q
2121
#SELENIUM_PORT=4444
2222
#SELENIUM_PROTOCOL=http
2323
#SELENIUM_PATH=/wd/hub
24+
SELENIUM_CLOSE_ALL_SESSIONS=true
2425

2526
#*** Browser for running tests, default chrome. Uncomment and change if you want to run tests on another browser (ex. firefox).
2627
BROWSER=chrome

etc/config/functional.suite.dist.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ modules:
3434
port: "%SELENIUM_PORT%"
3535
protocol: "%SELENIUM_PROTOCOL%"
3636
path: "%SELENIUM_PATH%"
37+
close_all_sessions: "%SELENIUM_CLOSE_ALL_SESSIONS%"
3738
capabilities:
3839
unhandledPromptBehavior: "ignore"
3940
chromeOptions:

src/Magento/FunctionalTestingFramework/Suite/views/SuiteClass.mustache

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Group;
44

5+
use Facebook\WebDriver\Remote\RemoteWebDriver;
56
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
67
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
8+
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;
79

810
/**
911
* Group class is Codeception Extension which is allowed to handle to all internal events.
@@ -55,18 +57,20 @@ class {{suiteName}} extends \Codeception\GroupObject
5557
if ($this->currentTestRun == 1) {
5658
print sprintf(self::$HOOK_EXECUTION_INIT, "before");
5759
60+
/** @var MagentoWebDriver $webDriver */
61+
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
62+
5863
try {
5964
{{> testActions}}
60-
61-
// reset configuration and close session
62-
$this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver')->_resetConfig();
63-
$webDriver->webDriver->close();
64-
$webDriver->webDriver = null;
65-
6665
} catch (\Exception $exception) {
6766
$this->preconditionFailure = $exception->getMessage();
6867
}
6968

69+
// reset configuration and close session
70+
$webDriver->_resetConfig();
71+
$webDriver->webDriver->close();
72+
$webDriver->webDriver = null;
73+
7074
print sprintf(self::$HOOK_EXECUTION_END, "before");
7175
}
7276
}
@@ -84,6 +88,9 @@ class {{suiteName}} extends \Codeception\GroupObject
8488
if ($this->currentTestRun == $this->testCount) {
8589
print sprintf(self::$HOOK_EXECUTION_INIT, "after");
8690
91+
/** @var MagentoWebDriver $webDriver */
92+
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
93+
8794
try {
8895
// Find out if Test in Suite failed, will cause potential failures in suite after
8996
$cest = $e->getTest();
@@ -112,8 +119,38 @@ class {{suiteName}} extends \Codeception\GroupObject
112119
}
113120

114121
PersistedObjectHandler::getInstance()->clearSuiteObjects();
122+
123+
$this->closeSession($webDriver);
124+
115125
print sprintf(self::$HOOK_EXECUTION_END, "after");
116126
}
117127
}
118128
{{/after}}
129+
130+
/**
131+
* Close session method closes current session.
132+
* If config 'close_all_sessions' is set to 'true' all sessions will be closed.
133+
*
134+
* @param MagentoWebDriver $webDriver
135+
* return void
136+
*/
137+
private function closeSession(MagentoWebDriver $webDriver): void
138+
{
139+
$webDriverConfig = $webDriver->_getConfig();
140+
$webDriver->_closeSession();
141+
if (isset($webDriverConfig['close_all_sessions']) && $webDriverConfig['close_all_sessions'] === "true") {
142+
$wdHost = sprintf(
143+
'%s://%s:%s%s',
144+
$webDriverConfig['protocol'],
145+
$webDriverConfig['host'],
146+
$webDriverConfig['port'],
147+
$webDriverConfig['path']
148+
);
149+
$availableSessions = RemoteWebDriver::getAllSessions($wdHost);
150+
foreach ($availableSessions as $session) {
151+
$remoteWebDriver = RemoteWebDriver::createBySessionID($session['id'], $wdHost);
152+
$remoteWebDriver->quit();
153+
}
154+
}
155+
}
119156
}

src/Magento/FunctionalTestingFramework/Suite/views/partials/testActions.mustache

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
{{#actions}}
22
{{#webDriverInit}}
3-
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
4-
5-
// close any open sessions
63
if ($webDriver->webDriver != null) {
7-
$webDriver->webDriver->close();
8-
$webDriver->webDriver = null;
4+
$webDriver->_restart();
5+
} else {
6+
$webDriver->_initializeSession();
97
}
10-
11-
// initialize the webdriver session
12-
$webDriver->_initializeSession();
138
{{/webDriverInit}}
149
{{#action}}
1510
{{{action}}}

0 commit comments

Comments
 (0)