@@ -32,13 +32,21 @@ class WebDriverTest extends \PHPUnit_Framework_TestCase
3232{
3333 private $ driver ;
3434 private $ session ;
35+ private $ testDocumentRootUrl = 'http://localhost ' ;
36+ private $ testSeleniumRootUrl = 'http://localhost:4444/wd/hub ' ;
3537
3638 /**
3739 * {@inheritdoc}
3840 */
3941 protected function setUp ()
4042 {
41- $ this ->driver = new WebDriver ();
43+ if ($ url = getenv ('ROOT_URL ' )) {
44+ $ this ->testDocumentRootUrl = $ url ;
45+ }
46+ if ($ url = getenv ('SELENIUM_URL ' )) {
47+ $ this ->testSeleniumRootUrl = $ url ;
48+ }
49+ $ this ->driver = new WebDriver ($ this ->getTestSeleniumRootUrl ());
4250 $ this ->session = null ;
4351 }
4452
@@ -52,27 +60,52 @@ protected function tearDown()
5260 }
5361 }
5462
63+ /**
64+ * Returns the full url to the test site (corresponding to the root dir of the library).
65+ * You can set this via env var ROOT_URL
66+ * @return string
67+ */
68+ protected function getTestDocumentRootUrl ()
69+ {
70+ return $ this ->testDocumentRootUrl ;
71+ }
72+
73+ /**
74+ * Returns the full url to the Selenium server used for functional tests
75+ * @return string
76+ *
77+ * @todo make this configurable via env var
78+ */
79+ protected function getTestSeleniumRootUrl ()
80+ {
81+ return $ this ->testSeleniumRootUrl ;
82+ }
83+
84+ protected function isSeleniumDown ($ exception )
85+ {
86+ return preg_match ('/Failed to connect to .* Connection refused/ ' , $ exception ->getMessage ()) != false
87+ || strpos ($ exception ->getMessage (), 'couldn \'t connect to host ' ) !== false ;
88+ }
89+
5590 /**
5691 * @group Functional
5792 */
5893 public function testSessions ()
5994 {
6095 try {
61- $ this ->assertCount (0 , $ this ->driver ->sessions ());
96+ $ this ->assertCount (0 , $ this ->driver ->sessions ());
6297
6398 $ this ->session = $ this ->driver ->session ();
6499 } catch (\Exception $ e ) {
65- if (strpos ($ e ->getMessage (),'Failed connect to localhost:4444; Connection refused ' ) !== false
66- || strpos ($ e ->getMessage (), 'couldn \'t connect to host ' ) !== false
67- ) {
100+ if ($ this ->isSeleniumDown ($ e )) {
68101 $ this ->markTestSkipped ('selenium server not running ' );
69102 } else {
70103 throw $ e ;
71104 }
72105 }
73106
74- $ this ->assertCount (1 , $ this ->driver ->sessions ());
75- $ this ->assertEquals (' http://localhost:4444/wd/hub ' , $ this ->driver ->getUrl ());
107+ $ this ->assertCount (1 , $ this ->driver ->sessions ());
108+ $ this ->assertEquals ($ this -> getTestSeleniumRootUrl () , $ this ->driver ->getUrl ());
76109 }
77110
78111 /**
@@ -83,9 +116,7 @@ public function testStatus()
83116 try {
84117 $ status = $ this ->driver ->status ();
85118 } catch (\Exception $ e ) {
86- if (strpos ($ e ->getMessage (),'Failed connect to localhost:4444; Connection refused ' ) !== false
87- || strpos ($ e ->getMessage (), 'couldn \'t connect to host ' ) !== false
88- ) {
119+ if ($ this ->isSeleniumDown ($ e )) {
89120 $ this ->markTestSkipped ('selenium server not running ' );
90121 } else {
91122 throw $ e ;
@@ -97,4 +128,73 @@ public function testStatus()
97128 $ this ->assertTrue (isset ($ status ['os ' ]));
98129 $ this ->assertTrue (isset ($ status ['build ' ]));
99130 }
131+
132+ /**
133+ * Checks that an error connecting to Selenium gives back the expected exception
134+ * @group Functional
135+ */
136+ public function testSeleniumError ()
137+ {
138+ try {
139+ $ this ->driver = new WebDriver ($ this ->getTestSeleniumRootUrl ().'/../invalidurl ' );
140+ $ status = $ this ->driver ->status ();
141+
142+ $ this ->fail ('Exception not thrown while connecting to invalid Selenium url ' );
143+ } catch (\Exception $ e ) {
144+ if ($ this ->isSeleniumDown ($ e )) {
145+ $ this ->markTestSkipped ('selenium server not running ' );
146+ } else {
147+ $ this ->assertEquals ('WebDriver\Exception\CurlExec ' , get_class ($ e ));
148+ }
149+ }
150+ }
151+
152+ /**
153+ * Checks that a successful command to Selenium which returns an http error response gives back the expected exception
154+ * @group Functional
155+ */
156+ public function testSeleniumErrorResponse ()
157+ {
158+ try {
159+ $ status = $ this ->driver ->status ();
160+ } catch (\Exception $ e ) {
161+ if ($ this ->isSeleniumDown ($ e )) {
162+ $ this ->markTestSkipped ('selenium server not running ' );
163+ } else {
164+ throw $ e ;
165+ }
166+ }
167+
168+ try {
169+ $ this ->session = $ this ->driver ->session ();
170+ $ this ->session ->open ($ this ->getTestDocumentRootUrl ().'/test/Assets/index.html ' );
171+ $ element = $ this ->session ->element ('id ' , 'a-quite-unlikely-html-element-id ' );
172+
173+ $ this ->fail ('Exception not thrown while looking for missing element in page ' );
174+ } catch (\Exception $ e ) {
175+ $ this ->assertEquals ('WebDriver\Exception\NoSuchElement ' , get_class ($ e ));
176+ }
177+ }
178+
179+ /**
180+ * Checks that a successful command to Selenium which returns 'nothing' according to spec does not raise an error
181+ * @group Functional
182+ */
183+ public function testSeleniumNoResponse ()
184+ {
185+ try {
186+ $ status = $ this ->driver ->status ();
187+ } catch (\Exception $ e ) {
188+ if ($ this ->isSeleniumDown ($ e )) {
189+ $ this ->markTestSkipped ('selenium server not running ' );
190+ } else {
191+ throw $ e ;
192+ }
193+ }
194+
195+ $ this ->session = $ this ->driver ->session ();
196+ $ timeouts = $ this ->session ->timeouts ();
197+ $ out = $ timeouts ->async_script (array ('type ' => 'implicit ' , 'ms ' => 1000 ));
198+ $ this ->assertEquals (null , $ out );
199+ }
100200}
0 commit comments