Skip to content

Commit e354c8c

Browse files
committed
MQE-1257: MFTF doctor command
1 parent f1299b1 commit e354c8c

File tree

2 files changed

+95
-31
lines changed

2 files changed

+95
-31
lines changed

src/Magento/FunctionalTestingFramework/Console/DoctorCommand.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ protected function configure()
5959
* @param OutputInterface $output
6060
* @return integer
6161
* @throws TestFrameworkException
62-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6362
*/
6463
protected function execute(InputInterface $input, OutputInterface $output)
6564
{
6665
$cmdStatus = true;
6766

6867
// Config application
6968
$verbose = $output->isVerbose();
69+
$this->input = $input;
7070
$this->output = $output;
7171
MftfApplicationConfig::create(
7272
false,
@@ -81,11 +81,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
8181
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
8282

8383
// Check connection to Selenium
84-
$status = $this->checkConnectionToSeleniumServer();
84+
$status = $this->checkOnStep(
85+
MagentoWebDriverDoctor::EXCEPTION_TYPE_SELENIUM,
86+
'Checking connection to Selenium Server'
87+
);
88+
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
89+
90+
// Check opening Magento Admin in web browser
91+
$status = $this->checkOnStep(
92+
MagentoWebDriverDoctor::EXCEPTION_TYPE_MAGENTO_ADMIN,
93+
'Checking opening Magento Admin in web browser controlled by Selenium'
94+
);
95+
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
96+
97+
// Check opening Magento Storefront in web browser
98+
$status = $this->checkOnStep(
99+
MagentoWebDriverDoctor::EXCEPTION_TYPE_MAGENTO_STOREFRONT,
100+
'Checking opening Magento Storefront in web browser controlled by Selenium'
101+
);
85102
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
86103

87104
// Check access to Magento CLI
88-
$status = $this->checkAccessToMagentoCLI();
105+
$status = $this->checkOnStep(
106+
MagentoWebDriverDoctor::EXCEPTION_TYPE_MAGENTO_CLI,
107+
'Checking access to Magento CLI'
108+
);
89109
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
90110

91111
if ($cmdStatus) {
@@ -115,38 +135,18 @@ private function checkAuthenticationToMagentoAdmin()
115135
}
116136

117137
/**
118-
* Check Connection to Selenium Server
119-
*
120-
* @return boolean
121-
*/
122-
private function checkConnectionToSeleniumServer()
123-
{
124-
// Check connection to Selenium through Codeception
125-
$this->output->writeln("\nChecking connection to Selenium Server ...");
126-
$this->runMagentoWebDriverDoctor();
127-
128-
if (isset($this->context[MagentoWebDriverDoctor::EXCEPTION_TYPE_SELENIUM])) {
129-
$this->output->write($this->context[MagentoWebDriverDoctor::EXCEPTION_TYPE_SELENIUM] . "\n");
130-
return false;
131-
} else {
132-
$this->output->writeln('Successful');
133-
return true;
134-
}
135-
}
136-
137-
/**
138-
* Check access to Magento CLI setup
138+
* Check on a step context after runMagentoWebDriverDoctor
139139
*
140140
* @return boolean
141+
* @throws TestFrameworkException
141142
*/
142-
private function checkAccessToMagentoCLI()
143+
private function checkOnStep($exceptionType, $message)
143144
{
144-
// Check Magento CLI setup
145-
$this->output->writeln("\nChecking access to Magento CLI ...");
145+
$this->output->writeln("\n$message ...");
146146
$this->runMagentoWebDriverDoctor();
147147

148-
if (isset($this->context[MagentoWebDriverDoctor::EXCEPTION_TYPE_MAGENTO_CLI])) {
149-
$this->output->write($this->context[MagentoWebDriverDoctor::EXCEPTION_TYPE_MAGENTO_CLI] . "\n");
148+
if (isset($this->context[$exceptionType])) {
149+
$this->output->write($this->context[$exceptionType] . "\n");
150150
return false;
151151
} else {
152152
$this->output->writeln('Successful');
@@ -158,6 +158,7 @@ private function checkAccessToMagentoCLI()
158158
* Run diagnose through MagentoWebDriverDoctor
159159
*
160160
* @return void
161+
* @throws TestFrameworkException
161162
*/
162163
private function runMagentoWebDriverDoctor()
163164
{

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriverDoctor.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515
*/
1616
class MagentoWebDriverDoctor extends MagentoWebDriver
1717
{
18+
const POLL_INTERVAL_MS = 1000;
1819
const MAGENTO_CLI_COMMAND = 'list';
1920
const EXCEPTION_TYPE_SELENIUM = 'selenium';
21+
const EXCEPTION_TYPE_MAGENTO_ADMIN = 'admin';
22+
const EXCEPTION_TYPE_MAGENTO_STOREFRONT = 'storefront';
2023
const EXCEPTION_TYPE_MAGENTO_CLI = 'cli';
2124

25+
/**
26+
* Remote Web Driver
27+
*
28+
* @var RemoteWebDriver
29+
*/
30+
private $remoteWebDriver = null;
31+
2232
/**
2333
* Go through parent initialization routines and in addition diagnose potential environment issues
2434
*
@@ -37,12 +47,32 @@ public function _initialize()
3747
$context[self::EXCEPTION_TYPE_SELENIUM] = $e->getMessage();
3848
}
3949

50+
try {
51+
$adminUrl = getenv('MAGENTO_BACKEND_BASE_URL')
52+
?: rtrim(getenv('MAGENTO_BASE_URL'), '/')
53+
. '/' . getenv('MAGENTO_BACKEND_NAME') . '/admin';
54+
$this->validateGoToUrl($adminUrl);
55+
} catch (\Exception $e) {
56+
$context[self::EXCEPTION_TYPE_MAGENTO_ADMIN] = $e->getMessage();
57+
}
58+
59+
try {
60+
$storeUrl = getenv('MAGENTO_BASE_URL');
61+
$this->validateGoToUrl($storeUrl);
62+
} catch (\Exception $e) {
63+
$context[self::EXCEPTION_TYPE_MAGENTO_STOREFRONT] = $e->getMessage();
64+
}
65+
4066
try {
4167
$this->checkMagentoCLI();
4268
} catch (TestFrameworkException $e) {
4369
$context[self::EXCEPTION_TYPE_MAGENTO_CLI] = $e->getMessage();
4470
}
4571

72+
if (null !== $this->remoteWebDriver) {
73+
$this->remoteWebDriver->close();
74+
}
75+
4676
if (!empty($context)) {
4777
throw new TestFrameworkException('MagentoWebDriverDoctor initialization failed', $context);
4878
}
@@ -56,16 +86,17 @@ public function _initialize()
5686
*/
5787
private function checkSeleniumServerReadiness()
5888
{
89+
$this->connectionTimeoutInMs = 5000;
90+
$this->requestTimeoutInMs = 5000;
5991
try {
60-
$driver = RemoteWebDriver::create(
92+
$this->remoteWebDriver = RemoteWebDriver::create(
6193
$this->wdHost,
6294
$this->capabilities,
6395
$this->connectionTimeoutInMs,
6496
$this->requestTimeoutInMs,
6597
$this->httpProxy,
6698
$this->httpProxyPort
6799
);
68-
$driver->close();
69100
} catch (\Exception $e) {
70101
throw new TestFrameworkException(
71102
"Can't connect to Webdriver at {$this->wdHost}.\n"
@@ -74,6 +105,38 @@ private function checkSeleniumServerReadiness()
74105
}
75106
}
76107

108+
/**
109+
* Validate if an url is accessible to browser controlled by selenium
110+
*
111+
* @param string $url
112+
* @return void
113+
* @throws TestFrameworkException
114+
*/
115+
private function validateGoToUrl($url)
116+
{
117+
$this->remoteWebDriver->get($url);
118+
119+
$end = microtime(true) + $this->config['pageload_timeout'];
120+
while ($end > microtime(true)) {
121+
try {
122+
$script = 'var xhr = new XMLHttpRequest();'
123+
. 'xhr.open(\'GET\', \'' . $url . '\', false);'
124+
. 'xhr.send(null);'
125+
. 'return xhr.status';
126+
$status = $this->remoteWebDriver->executeScript($script);
127+
128+
if ($status === 200) {
129+
return;
130+
}
131+
} catch (\Exception $e) {
132+
}
133+
usleep(self::POLL_INTERVAL_MS);
134+
}
135+
throw new TestFrameworkException(
136+
"Timed out opening page at url: $url"
137+
);
138+
}
139+
77140
/**
78141
* Check Magento CLI setup
79142
*

0 commit comments

Comments
 (0)