23
23
namespace Test \WebDriver ;
24
24
25
25
use Test \WebDriver \WebDriverTestBase ;
26
+ use WebDriver \Exception \CurlExec ;
27
+ use WebDriver \Exception \NoSuchElement ;
28
+ use WebDriver \Service \CurlService ;
29
+ use WebDriver \ServiceFactory ;
30
+ use WebDriver \WebDriver ;
26
31
27
32
/**
28
33
* Selenium WebDriver
@@ -35,4 +40,165 @@ class SeleniumWebDriverTest extends WebDriverTestBase
35
40
{
36
41
protected $ testWebDriverRootUrl = 'http://localhost:4444/wd/hub ' ;
37
42
protected $ testWebDriverName = 'selenium ' ;
43
+
44
+ /**
45
+ * Test driver sessions
46
+ */
47
+ public function testSessions ()
48
+ {
49
+ try {
50
+ $ sessions = $ this ->driver ->sessions ();
51
+ $ this ->assertCount (0 , $ sessions );
52
+
53
+ $ this ->session = $ this ->driver ->session ();
54
+ } catch (\Exception $ e ) {
55
+ if ($ this ->isWebDriverDown ($ e )) {
56
+ $ this ->markTestSkipped ("{$ this ->testWebDriverName } server not running " );
57
+
58
+ return ;
59
+ }
60
+
61
+ throw $ e ;
62
+ }
63
+
64
+ $ this ->assertCount (1 , $ this ->driver ->sessions ());
65
+ $ this ->assertEquals ($ this ->getTestWebDriverRootUrl (), $ this ->driver ->getUrl ());
66
+ }
67
+
68
+ /**
69
+ * Test driver status
70
+ */
71
+ public function testStatus ()
72
+ {
73
+ try {
74
+ $ status = $ this ->driver ->status ();
75
+ } catch (\Exception $ e ) {
76
+ if ($ this ->isWebDriverDown ($ e )) {
77
+ $ this ->markTestSkipped ("{$ this ->testWebDriverName } server not running " );
78
+
79
+ return ;
80
+ }
81
+
82
+ throw $ e ;
83
+ }
84
+
85
+ $ this ->session = $ this ->driver ->session ();
86
+
87
+ $ this ->assertCount (3 , $ status );
88
+ $ this ->assertTrue (isset ($ status ['os ' ]));
89
+ $ this ->assertTrue (isset ($ status ['build ' ]));
90
+ }
91
+
92
+ /**
93
+ * Checks that an error connecting to WebDriver gives back the expected exception
94
+ */
95
+ public function testWebDriverError ()
96
+ {
97
+ try {
98
+ $ this ->driver = new WebDriver ($ this ->getTestWebDriverRootUrl () . '/../invalidurl ' );
99
+
100
+ $ status = $ this ->driver ->status ();
101
+
102
+ $ this ->fail ('Exception not thrown while connecting to invalid WebDriver url ' );
103
+ } catch (\Exception $ e ) {
104
+ if ($ this ->isWebDriverDown ($ e )) {
105
+ $ this ->markTestSkipped ("{$ this ->testWebDriverName } server not running " );
106
+
107
+ return ;
108
+ }
109
+
110
+ $ this ->assertEquals (CurlExec::class, get_class ($ e ));
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Checks that a successful command to WebDriver which returns an http error response gives back the expected exception
116
+ */
117
+ public function testWebDriverErrorResponse ()
118
+ {
119
+ try {
120
+ $ status = $ this ->driver ->status ();
121
+ } catch (\Exception $ e ) {
122
+ if ($ this ->isWebDriverDown ($ e )) {
123
+ $ this ->markTestSkipped ("{$ this ->testWebDriverName } server not running " );
124
+
125
+ return ;
126
+ }
127
+
128
+ throw $ e ;
129
+ }
130
+
131
+ try {
132
+ $ this ->session = $ this ->driver ->session ();
133
+ $ this ->session ->open ($ this ->getTestDocumentRootUrl () . '/test/Assets/index.html ' );
134
+
135
+ $ element = $ this ->session ->element ('id ' , 'a-quite-unlikely-html-element-id ' );
136
+
137
+ $ this ->fail ('Exception not thrown while looking for missing element in page ' );
138
+ } catch (\Exception $ e ) {
139
+ $ this ->assertEquals (NoSuchElement::class, get_class ($ e ));
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Checks that a successful command to WebDriver which returns 'nothing' according to spec does not raise an error
145
+ */
146
+ public function testWebDriverNoResponse ()
147
+ {
148
+ try {
149
+ $ status = $ this ->driver ->status ();
150
+ } catch (\Exception $ e ) {
151
+ if ($ this ->isWebDriverDown ($ e )) {
152
+ $ this ->markTestSkipped ("{$ this ->testWebDriverName } server not running " );
153
+
154
+ return ;
155
+ }
156
+
157
+ throw $ e ;
158
+ }
159
+
160
+ $ this ->session = $ this ->driver ->session ();
161
+ $ timeouts = $ this ->session ->timeouts ();
162
+ $ out = $ timeouts ->async_script (array ('type ' => 'implicit ' , 'ms ' => 1000 ));
163
+
164
+ $ this ->assertEquals (null , $ out );
165
+ }
166
+
167
+ /**
168
+ * Assert that empty response does not trigger exception, but invalid JSON does
169
+ */
170
+ public function testNonJsonResponse ()
171
+ {
172
+ $ mockCurlService = $ this ->createMock (CurlService::class);
173
+ $ mockCurlService ->expects ($ this ->any ())
174
+ ->method ('execute ' )
175
+ ->will ($ this ->returnCallback (function ($ requestMethod , $ url ) {
176
+ $ info = array (
177
+ 'url ' => $ url ,
178
+ 'request_method ' => $ requestMethod ,
179
+ 'http_code ' => 200 ,
180
+ );
181
+
182
+ $ result = preg_match ('#.*session$# ' , $ url )
183
+ ? $ result = 'some invalid json '
184
+ : $ result = '' ;
185
+
186
+ return array ($ result , $ info );
187
+ }));
188
+
189
+ ServiceFactory::getInstance ()->setService ('service.curl ' , $ mockCurlService );
190
+
191
+ $ this ->driver = new WebDriver ($ this ->getTestWebDriverRootUrl ());
192
+ $ result = $ this ->driver ->status ();
193
+
194
+ $ this ->assertNull ($ result );
195
+
196
+ // Test /session should error
197
+ $ this ->expectException (\WebDriver \Exception \CurlExec::class);
198
+ $ this ->expectExceptionMessage ('Payload received from webdriver is not valid json: some invalid json ' );
199
+
200
+ $ result = $ this ->driver ->session ();
201
+
202
+ $ this ->assertNull ($ result );
203
+ }
38
204
}
0 commit comments