forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagentoWebDriverDoctor.php
168 lines (147 loc) · 4.99 KB
/
MagentoWebDriverDoctor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Module;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Facebook\WebDriver\Remote\RemoteWebDriver;
/**
* MagentoWebDriverDoctor module extends MagentoWebDriver module and is a light weighted module to diagnose webdriver
* initialization and other setup issues. It uses in memory version of MagentoWebDriver's configuration file.
*/
class MagentoWebDriverDoctor extends MagentoWebDriver
{
const MAGENTO_CLI_COMMAND = 'info:currency:list';
const EXCEPTION_CONTEXT_SELENIUM = 'selenium';
const EXCEPTION_CONTEXT_ADMIN = 'admin';
const EXCEPTION_CONTEXT_STOREFRONT = 'store';
const EXCEPTION_CONTEXT_CLI = 'cli';
/**
* Remote Web Driver
*
* @var RemoteWebDriver
*/
private $remoteWebDriver = null;
/**
* Go through parent initialization routines and in addition diagnose potential environment issues
*
* @return void
* @throws TestFrameworkException
*/
public function _initialize()
{
parent::_initialize();
$context = [];
try {
$this->connectToSeleniumServer();
} catch (TestFrameworkException $e) {
$context[self::EXCEPTION_CONTEXT_SELENIUM] = $e->getMessage();
}
try {
$adminUrl = rtrim(getenv('MAGENTO_BACKEND_BASE_URL'), '/')
?: rtrim(getenv('MAGENTO_BASE_URL'), '/')
. '/' . getenv('MAGENTO_BACKEND_NAME') . '/admin';
$this->loadPageAtUrl($adminUrl);
} catch (\Exception $e) {
$context[self::EXCEPTION_CONTEXT_ADMIN] = $e->getMessage();
}
try {
$storeUrl = getenv('MAGENTO_BASE_URL');
$this->loadPageAtUrl($storeUrl);
} catch (\Exception $e) {
$context[self::EXCEPTION_CONTEXT_STOREFRONT] = $e->getMessage();
}
try {
$this->runMagentoCLI();
} catch (\Exception $e) {
$context[self::EXCEPTION_CONTEXT_CLI] = $e->getMessage();
}
if (null !== $this->remoteWebDriver) {
$this->remoteWebDriver->close();
}
if (!empty($context)) {
throw new TestFrameworkException('Exception occurred in MagentoWebDriverDoctor', $context);
}
}
/**
* Check connecting to running selenium server
*
* @return void
* @throws TestFrameworkException
*/
private function connectToSeleniumServer()
{
try {
$this->remoteWebDriver = RemoteWebDriver::create(
$this->wdHost,
$this->capabilities,
$this->connectionTimeoutInMs,
$this->requestTimeoutInMs,
$this->httpProxy,
$this->httpProxyPort
);
if (null !== $this->remoteWebDriver) {
return;
}
} catch (\Exception $e) {
}
throw new TestFrameworkException(
"Failed to connect Selenium WebDriver at: {$this->wdHost}.\n"
. "Please make sure that Selenium Server is running."
);
}
/**
* Validate loading a web page at url in the browser controlled by selenium
*
* @param string $url
* @return void
* @throws TestFrameworkException
*/
private function loadPageAtUrl($url)
{
try {
if (null !== $this->remoteWebDriver) {
// Open the web page at url first
$this->remoteWebDriver->get($url);
// Execute Javascript to retrieve HTTP response code
$script = ''
. 'var xhr = new XMLHttpRequest();'
. "xhr.open('GET', '" . $url . "', false);"
. 'xhr.send(null); '
. 'return xhr.status';
$status = $this->remoteWebDriver->executeScript($script);
if ($status === 200) {
return;
}
}
} catch (\Exception $e) {
}
throw new TestFrameworkException(
"Failed to load page at url: $url\n"
. "Please check Selenium Browser session have access to Magento instance."
);
}
/**
* Check running Magento CLI command
*
* @return void
* @throws TestFrameworkException
*/
private function runMagentoCLI()
{
try {
$regex = '~^.*[\r\n]+.*(?<name>Currency).*(?<code>Code).*~';
$output = parent::magentoCLI(self::MAGENTO_CLI_COMMAND);
preg_match($regex, $output, $matches);
if (isset($matches['name']) && isset($matches['code'])) {
return;
}
} catch (\Exception $e) {
}
throw new TestFrameworkException(
"Failed to run Magento CLI command\n"
. "Please reference Magento DevDoc to setup command.php and .htaccess files."
);
}
}