Skip to content

MQE-2162: Chrome remains running after MFTF suite finishes #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 49 additions & 22 deletions dev/tests/verification/Resources/functionalSuiteHooks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Group;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;

/**
* Group class is Codeception Extension which is allowed to handle to all internal events.
Expand Down Expand Up @@ -41,17 +43,15 @@ class functionalSuiteHooks extends \Codeception\GroupObject
if ($this->currentTestRun == 1) {
print sprintf(self::$HOOK_EXECUTION_INIT, "before");

/** @var MagentoWebDriver $webDriver */
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

try {
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

// close any open sessions
if ($webDriver->webDriver != null) {
$webDriver->webDriver->close();
$webDriver->webDriver = null;
$webDriver->_restart();
} else {
$webDriver->_initializeSession();
}

// initialize the webdriver session
$webDriver->_initializeSession();
$webDriver->amOnPage("some.url"); // stepKey: before
$createFields['someKey'] = "dataHere";
PersistedObjectHandler::getInstance()->createEntity(
Expand All @@ -64,16 +64,15 @@ class functionalSuiteHooks extends \Codeception\GroupObject
print("Entering Action Group [AC] actionGroupWithTwoArguments");
$webDriver->see("John", msq("uniqueData") . "John"); // stepKey: seeFirstNameAC
print("Exiting Action Group [AC] actionGroupWithTwoArguments");

// reset configuration and close session
$this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver')->_resetConfig();
$webDriver->webDriver->close();
$webDriver->webDriver = null;

} catch (\Exception $exception) {
$this->preconditionFailure = $exception->getMessage();
}

// reset configuration and close session
$webDriver->_resetConfig();
$webDriver->webDriver->close();
$webDriver->webDriver = null;

print sprintf(self::$HOOK_EXECUTION_END, "before");
}
}
Expand All @@ -89,6 +88,9 @@ class functionalSuiteHooks extends \Codeception\GroupObject
if ($this->currentTestRun == $this->testCount) {
print sprintf(self::$HOOK_EXECUTION_INIT, "after");

/** @var MagentoWebDriver $webDriver */
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

try {
// Find out if Test in Suite failed, will cause potential failures in suite after
$cest = $e->getTest();
Expand All @@ -111,16 +113,11 @@ class functionalSuiteHooks extends \Codeception\GroupObject
}
}
}
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

// close any open sessions
if ($webDriver->webDriver != null) {
$webDriver->webDriver->close();
$webDriver->webDriver = null;
$webDriver->_restart();
} else {
$webDriver->_initializeSession();
}

// initialize the webdriver session
$webDriver->_initializeSession();
$webDriver->amOnPage("some.url"); // stepKey: after
$webDriver->deleteEntityByUrl("deleteThis"); // stepKey: delete
print("Entering Action Group [AC] actionGroupWithTwoArguments");
Expand All @@ -131,7 +128,37 @@ class functionalSuiteHooks extends \Codeception\GroupObject
}

PersistedObjectHandler::getInstance()->clearSuiteObjects();

$this->closeSession($webDriver);

print sprintf(self::$HOOK_EXECUTION_END, "after");
}
}

/**
* Close session method closes current session.
* If config 'close_all_sessions' is set to 'true' all sessions will be closed.
*
* @param MagentoWebDriver $webDriver
* return void
*/
private function closeSession(MagentoWebDriver $webDriver): void
{
$webDriverConfig = $webDriver->_getConfig();
$webDriver->_closeSession();
if (isset($webDriverConfig['close_all_sessions']) && $webDriverConfig['close_all_sessions'] === "true") {
$wdHost = sprintf(
'%s://%s:%s%s',
$webDriverConfig['protocol'],
$webDriverConfig['host'],
$webDriverConfig['port'],
$webDriverConfig['path']
);
$availableSessions = RemoteWebDriver::getAllSessions($wdHost);
foreach ($availableSessions as $session) {
$remoteWebDriver = RemoteWebDriver::createBySessionID($session['id'], $wdHost);
$remoteWebDriver->quit();
}
}
}
}
71 changes: 49 additions & 22 deletions dev/tests/verification/Resources/functionalSuiteWithComments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Group;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;

/**
* Group class is Codeception Extension which is allowed to handle to all internal events.
Expand Down Expand Up @@ -41,17 +43,15 @@ class functionalSuiteWithComments extends \Codeception\GroupObject
if ($this->currentTestRun == 1) {
print sprintf(self::$HOOK_EXECUTION_INIT, "before");

/** @var MagentoWebDriver $webDriver */
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

try {
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

// close any open sessions
if ($webDriver->webDriver != null) {
$webDriver->webDriver->close();
$webDriver->webDriver = null;
$webDriver->_restart();
} else {
$webDriver->_initializeSession();
}

// initialize the webdriver session
$webDriver->_initializeSession();
print("Comment in Before");
$webDriver->amOnPage("some.url"); // stepKey: before
$createFields['someKey'] = "dataHere";
Expand All @@ -66,16 +66,15 @@ class functionalSuiteWithComments extends \Codeception\GroupObject
print("Entering Action Group [AC] actionGroupWithTwoArguments");
$webDriver->see("John", msq("uniqueData") . "John"); // stepKey: seeFirstNameAC
print("Exiting Action Group [AC] actionGroupWithTwoArguments");

// reset configuration and close session
$this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver')->_resetConfig();
$webDriver->webDriver->close();
$webDriver->webDriver = null;

} catch (\Exception $exception) {
$this->preconditionFailure = $exception->getMessage();
}

// reset configuration and close session
$webDriver->_resetConfig();
$webDriver->webDriver->close();
$webDriver->webDriver = null;

print sprintf(self::$HOOK_EXECUTION_END, "before");
}
}
Expand All @@ -91,6 +90,9 @@ class functionalSuiteWithComments extends \Codeception\GroupObject
if ($this->currentTestRun == $this->testCount) {
print sprintf(self::$HOOK_EXECUTION_INIT, "after");

/** @var MagentoWebDriver $webDriver */
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

try {
// Find out if Test in Suite failed, will cause potential failures in suite after
$cest = $e->getTest();
Expand All @@ -113,23 +115,48 @@ class functionalSuiteWithComments extends \Codeception\GroupObject
}
}
}
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

// close any open sessions
if ($webDriver->webDriver != null) {
$webDriver->webDriver->close();
$webDriver->webDriver = null;
$webDriver->_restart();
} else {
$webDriver->_initializeSession();
}

// initialize the webdriver session
$webDriver->_initializeSession();
print("afterBlock");
} catch (\Exception $exception) {
print $exception->getMessage();
}

PersistedObjectHandler::getInstance()->clearSuiteObjects();

$this->closeSession($webDriver);

print sprintf(self::$HOOK_EXECUTION_END, "after");
}
}

/**
* Close session method closes current session.
* If config 'close_all_sessions' is set to 'true' all sessions will be closed.
*
* @param MagentoWebDriver $webDriver
* return void
*/
private function closeSession(MagentoWebDriver $webDriver): void
{
$webDriverConfig = $webDriver->_getConfig();
$webDriver->_closeSession();
if (isset($webDriverConfig['close_all_sessions']) && $webDriverConfig['close_all_sessions'] === "true") {
$wdHost = sprintf(
'%s://%s:%s%s',
$webDriverConfig['protocol'],
$webDriverConfig['host'],
$webDriverConfig['port'],
$webDriverConfig['path']
);
$availableSessions = RemoteWebDriver::getAllSessions($wdHost);
foreach ($availableSessions as $session) {
$remoteWebDriver = RemoteWebDriver::createBySessionID($session['id'], $wdHost);
$remoteWebDriver->quit();
}
}
}
}
10 changes: 10 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ Enables addition of browser logs to Allure steps
ENABLE_BROWSER_LOG=true
```

### SELENIUM_CLOSE_ALL_SESSIONS

Forces MFTF to close all Selenium sessions after running a suite.

Use this if you're having issues with sessions hanging in an MFTF suite.

```conf
SELENIUM_CLOSE_ALL_SESSIONS=true
```

### BROWSER_LOG_BLACKLIST

Blacklists types of browser log entries from appearing in Allure steps.
Expand Down
1 change: 1 addition & 0 deletions etc/config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ MAGENTO_ADMIN_PASSWORD=123123q
#SELENIUM_PORT=4444
#SELENIUM_PROTOCOL=http
#SELENIUM_PATH=/wd/hub
SELENIUM_CLOSE_ALL_SESSIONS=true

#*** Browser for running tests, default chrome. Uncomment and change if you want to run tests on another browser (ex. firefox).
BROWSER=chrome
Expand Down
1 change: 1 addition & 0 deletions etc/config/functional.suite.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ modules:
port: "%SELENIUM_PORT%"
protocol: "%SELENIUM_PROTOCOL%"
path: "%SELENIUM_PATH%"
close_all_sessions: "%SELENIUM_CLOSE_ALL_SESSIONS%"
capabilities:
unhandledPromptBehavior: "ignore"
chromeOptions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Group;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;

/**
* Group class is Codeception Extension which is allowed to handle to all internal events.
Expand Down Expand Up @@ -55,18 +57,20 @@ class {{suiteName}} extends \Codeception\GroupObject
if ($this->currentTestRun == 1) {
print sprintf(self::$HOOK_EXECUTION_INIT, "before");

/** @var MagentoWebDriver $webDriver */
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

try {
{{> testActions}}

// reset configuration and close session
$this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver')->_resetConfig();
$webDriver->webDriver->close();
$webDriver->webDriver = null;

} catch (\Exception $exception) {
$this->preconditionFailure = $exception->getMessage();
}

// reset configuration and close session
$webDriver->_resetConfig();
$webDriver->webDriver->close();
$webDriver->webDriver = null;

print sprintf(self::$HOOK_EXECUTION_END, "before");
}
}
Expand All @@ -84,6 +88,9 @@ class {{suiteName}} extends \Codeception\GroupObject
if ($this->currentTestRun == $this->testCount) {
print sprintf(self::$HOOK_EXECUTION_INIT, "after");

/** @var MagentoWebDriver $webDriver */
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we use MagentoWebDriver::class if possible (here and all other places in this PR).


try {
// Find out if Test in Suite failed, will cause potential failures in suite after
$cest = $e->getTest();
Expand Down Expand Up @@ -112,8 +119,38 @@ class {{suiteName}} extends \Codeception\GroupObject
}

PersistedObjectHandler::getInstance()->clearSuiteObjects();

$this->closeSession($webDriver);

print sprintf(self::$HOOK_EXECUTION_END, "after");
}
}
{{/after}}

/**
* Close session method closes current session.
* If config 'close_all_sessions' is set to 'true' all sessions will be closed.
*
* @param MagentoWebDriver $webDriver
* return void
*/
private function closeSession(MagentoWebDriver $webDriver): void
{
$webDriverConfig = $webDriver->_getConfig();
$webDriver->_closeSession();
if (isset($webDriverConfig['close_all_sessions']) && $webDriverConfig['close_all_sessions'] === "true") {
$wdHost = sprintf(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a protected function stopAllSessions() in WebDriver.php, which MagentoWebDriver extends. It seems to do similarly what we do here, did you try and make it work?

'%s://%s:%s%s',
$webDriverConfig['protocol'],
$webDriverConfig['host'],
$webDriverConfig['port'],
$webDriverConfig['path']
);
$availableSessions = RemoteWebDriver::getAllSessions($wdHost);
foreach ($availableSessions as $session) {
$remoteWebDriver = RemoteWebDriver::createBySessionID($session['id'], $wdHost);
$remoteWebDriver->quit();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
{{#actions}}
{{#webDriverInit}}
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');

// close any open sessions
if ($webDriver->webDriver != null) {
$webDriver->webDriver->close();
$webDriver->webDriver = null;
$webDriver->_restart();
} else {
$webDriver->_initializeSession();
}

// initialize the webdriver session
$webDriver->_initializeSession();
{{/webDriverInit}}
{{#action}}
{{{action}}}
Expand Down