1111
1212/**
1313 * 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.
1515 */
1616class MagentoWebDriverDoctor extends MagentoWebDriver
1717{
1818 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 ;
2130
2231 /**
2332 * Go through parent initialization routines and in addition diagnose potential environment issues
@@ -32,56 +41,121 @@ public function _initialize()
3241 $ context = [];
3342
3443 try {
35- $ this ->checkSeleniumServerReadiness ();
44+ $ this ->connectToSeleniumServer ();
3645 } catch (TestFrameworkException $ e ) {
37- $ context [self ::EXCEPTION_TYPE_SELENIUM ] = $ e ->getMessage ();
46+ $ context [self ::EXCEPTION_CONTEXT_SELENIUM ] = $ e ->getMessage ();
3847 }
3948
4049 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 ();
4473 }
4574
4675 if (!empty ($ context )) {
47- throw new TestFrameworkException ('MagentoWebDriverDoctor initialization failed ' , $ context );
76+ throw new TestFrameworkException ('Exception occurred in MagentoWebDriverDoctor ' , $ context );
4877 }
4978 }
5079
5180 /**
52- * Check connectivity to running selenium server
81+ * Check connecting to running selenium server
5382 *
5483 * @return void
5584 * @throws TestFrameworkException
5685 */
57- private function checkSeleniumServerReadiness ()
86+ private function connectToSeleniumServer ()
5887 {
5988 try {
60- $ driver = RemoteWebDriver::create (
89+ $ this -> remoteWebDriver = RemoteWebDriver::create (
6190 $ this ->wdHost ,
6291 $ this ->capabilities ,
6392 $ this ->connectionTimeoutInMs ,
6493 $ this ->requestTimeoutInMs ,
6594 $ this ->httpProxy ,
6695 $ this ->httpProxyPort
6796 );
68- $ driver ->close ();
6997 } catch (\Exception $ e ) {
7098 throw new TestFrameworkException (
71- "Can't connect to Webdriver at {$ this ->wdHost }. \n"
99+ "Failed to connect Selenium WebDriver at: {$ this ->wdHost }. \n"
72100 . "Please make sure that Selenium Server is running. "
73101 );
74102 }
75103 }
76104
77105 /**
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
79140 *
80141 * @return void
81142 * @throws TestFrameworkException
82143 */
83- private function checkMagentoCLI ()
144+ private function runMagentoCLI ()
84145 {
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+ }
86160 }
87161}
0 commit comments