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