Security update for permissions_by_term
[yaffs-website] / vendor / instaclick / php-webdriver / test / Test / WebDriver / WebDriverTest.php
1 <?php
2 /**
3  * Copyright 2014-2017 Anthon Pang. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * @package WebDriver
18  *
19  * @author Anthon Pang <apang@softwaredevelopment.ca>
20  * @author Damian Mooyman <damian@silverstripe.com>
21  */
22
23 namespace Test\WebDriver;
24
25 use WebDriver\ServiceFactory;
26 use WebDriver\WebDriver;
27
28 /**
29  * Test WebDriver\WebDriver class
30  *
31  * @package WebDriver
32  *
33  * @group Functional
34  */
35 class WebDriverTest extends \PHPUnit_Framework_TestCase
36 {
37     private $driver;
38     private $session;
39     private $testDocumentRootUrl = 'http://localhost';
40     private $testSeleniumRootUrl = 'http://localhost:4444/wd/hub';
41
42     /**
43      * {@inheritdoc}
44      */
45     protected function setUp()
46     {
47         ServiceFactory::getInstance()->setServiceClass('service.curl', '\\WebDriver\\Service\\CurlService');
48
49         if ($url = getenv('ROOT_URL')) {
50             $this->testDocumentRootUrl = $url;
51         }
52
53         if ($url = getenv('SELENIUM_URL')) {
54             $this->testSeleniumRootUrl = $url;
55         }
56
57         $this->driver  = new WebDriver($this->getTestSeleniumRootUrl());
58         $this->session = null;
59     }
60
61     /**
62      * {@inheritdoc}
63      */
64     protected function tearDown()
65     {
66         if ($this->session) {
67             $this->session->close();
68         }
69     }
70
71     /**
72      * Returns the full url to the test site (corresponding to the root dir of the library).
73      * You can set this via env var ROOT_URL
74      *
75      * @return string
76      */
77     protected function getTestDocumentRootUrl()
78     {
79         return $this->testDocumentRootUrl;
80     }
81
82     /**
83      * Returns the full url to the Selenium server used for functional tests
84      *
85      * @return string
86      *
87      * @todo make this configurable via env var
88      */
89     protected function getTestSeleniumRootUrl()
90     {
91         return $this->testSeleniumRootUrl;
92     }
93
94     /**
95      * Is Selenium down?
96      *
97      * @param \Exception $exception
98      *
99      * @return boolean
100      */
101     protected function isSeleniumDown($exception)
102     {
103         return preg_match('/Failed to connect to .* Connection refused/', $exception->getMessage()) != false
104             || strpos($exception->getMessage(), 'couldn\'t connect to host') !== false
105             || strpos($exception->getMessage(), 'Unable to connect to host') !== false;
106     }
107
108     /**
109      * Test driver sessions
110      */
111     public function testSessions()
112     {
113         try {
114             $this->assertCount(0, $this->driver->sessions());
115
116             $this->session = $this->driver->session();
117         } catch (\Exception $e) {
118             if ($this->isSeleniumDown($e)) {
119                 $this->markTestSkipped('selenium server not running');
120
121                 return;
122             }
123
124             throw $e;
125         }
126
127         $this->assertCount(1, $this->driver->sessions());
128         $this->assertEquals($this->getTestSeleniumRootUrl(), $this->driver->getUrl());
129     }
130
131     /**
132      * Test driver status
133      */
134     public function testStatus()
135     {
136         try {
137             $status = $this->driver->status();
138         } catch (\Exception $e) {
139             if ($this->isSeleniumDown($e)) {
140                 $this->markTestSkipped('selenium server not running');
141
142                 return;
143             }
144
145             throw $e;
146         }
147
148         $this->assertCount(3, $status);
149         $this->assertTrue(isset($status['java']));
150         $this->assertTrue(isset($status['os']));
151         $this->assertTrue(isset($status['build']));
152     }
153
154     /**
155      * Checks that an error connecting to Selenium gives back the expected exception
156      */
157     public function testSeleniumError()
158     {
159         try {
160             $this->driver = new WebDriver($this->getTestSeleniumRootUrl() . '/../invalidurl');
161
162             $status = $this->driver->status();
163
164             $this->fail('Exception not thrown while connecting to invalid Selenium url');
165         } catch (\Exception $e) {
166             if ($this->isSeleniumDown($e)) {
167                 $this->markTestSkipped('selenium server not running');
168
169                 return;
170             }
171
172             $this->assertEquals('WebDriver\Exception\CurlExec', get_class($e));
173         }
174     }
175
176     /**
177      * Checks that a successful command to Selenium which returns an http error response gives back the expected exception
178      */
179     public function testSeleniumErrorResponse()
180     {
181         try {
182             $status = $this->driver->status();
183         } catch (\Exception $e) {
184             if ($this->isSeleniumDown($e)) {
185                 $this->markTestSkipped('selenium server not running');
186
187                 return;
188             }
189
190             throw $e;
191         }
192
193         try {
194             $this->session = $this->driver->session();
195             $this->session->open($this->getTestDocumentRootUrl().'/test/Assets/index.html');
196
197             $element = $this->session->element('id', 'a-quite-unlikely-html-element-id');
198
199             $this->fail('Exception not thrown while looking for missing element in page');
200         } catch (\Exception $e) {
201             $this->assertEquals('WebDriver\Exception\NoSuchElement', get_class($e));
202         }
203     }
204
205     /**
206      * Checks that a successful command to Selenium which returns 'nothing' according to spec does not raise an error
207      */
208     public function testSeleniumNoResponse()
209     {
210         try {
211             $status = $this->driver->status();
212         } catch (\Exception $e) {
213             if ($this->isSeleniumDown($e)) {
214                 $this->markTestSkipped('selenium server not running');
215
216                 return;
217             }
218
219             throw $e;
220         }
221
222         $this->session = $this->driver->session();
223         $timeouts = $this->session->timeouts();
224         $out = $timeouts->async_script(array('type' => 'implicit', 'ms' => 1000));
225
226         $this->assertEquals(null, $out);
227     }
228
229     /**
230      * Assert that empty response does not trigger exception, but invalid JSON does
231      */
232     public function testNonJsonResponse()
233     {
234         $mockCurlService = $this->createMock('WebDriver\Service\CurlService');
235         $mockCurlService->expects($this->once())
236             ->method('execute')
237             ->will($this->returnCallback(function ($requestMethod, $url) {
238                 $info = array(
239                     'url' => $url,
240                     'request_method' => $requestMethod,
241                     'http_code' => 200,
242                 );
243
244                 $result = preg_match('#.*session$#', $url)
245                     ? $result = 'some invalid json'
246                     : $result = '';
247
248                 return array($result, $info);
249             }));
250
251         ServiceFactory::getInstance()->setService('service.curl', $mockCurlService);
252
253         $result = $this->driver->status();
254
255         $this->assertNull($result);
256
257         // Test /session should error
258         $this->setExpectedException(
259             'WebDriver\Exception\CurlExec',
260             'Payload received from webdriver is not valid json: some invalid json'
261         );
262
263         $result = $this->driver->session();
264
265         $this->assertNull($result);
266     }
267 }