Skip to content

Commit 794539e

Browse files
committed
Update tests
1 parent 6582b5b commit 794539e

File tree

5 files changed

+304
-184
lines changed

5 files changed

+304
-184
lines changed

test/Test/WebDriver/ChromeDriverTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace Test\WebDriver;
2424

2525
use Test\WebDriver\WebDriverTestBase;
26+
use WebDriver\Session;
2627

2728
/**
2829
* ChromeDriver
@@ -35,4 +36,49 @@ class ChromeDriverTest extends WebDriverTestBase
3536
{
3637
protected $testWebDriverRootUrl = 'http://localhost:9515';
3738
protected $testWebDriverName = 'chromedriver';
39+
40+
/**
41+
* Test driver session
42+
*/
43+
public function testSession()
44+
{
45+
try {
46+
$this->session = $this->driver->session();
47+
} catch (\Exception $e) {
48+
if ($this->isWebDriverDown($e)) {
49+
$this->markTestSkipped("{$this->testWebDriverName} server not running");
50+
51+
return;
52+
}
53+
54+
throw $e;
55+
}
56+
57+
$this->assertTrue($this->session instanceof Session);
58+
}
59+
60+
/**
61+
/**
62+
* Test driver status
63+
*/
64+
public function testStatus()
65+
{
66+
try {
67+
$status = $this->driver->status();
68+
} catch (\Exception $e) {
69+
if ($this->isWebDriverDown($e)) {
70+
$this->markTestSkipped("{$this->testWebDriverName} server not running");
71+
72+
return;
73+
}
74+
75+
throw $e;
76+
}
77+
78+
$this->assertCount(4, $status);
79+
$this->assertTrue(isset($status['build']));
80+
$this->assertTrue(isset($status['message']));
81+
$this->assertTrue(isset($status['os']));
82+
$this->assertTrue(isset($status['ready']));
83+
}
3884
}

test/Test/WebDriver/ExceptionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function testFactory()
4747
$this->assertTrue($out instanceof Exception\UnknownError);
4848
$this->assertTrue($out->getMessage() === 'An unknown server-side error occurred while processing the command.');
4949

50+
$out = Exception::factory(Exception::SESSION_NOT_CREATED);
51+
$this->assertTrue($out instanceof Exception\SessionNotCreated);
52+
$this->assertTrue(strpos($out->getMessage(), 'A new session could not be created') !== false);
53+
5054
$out = Exception::factory(Exception::CURL_EXEC);
5155
$this->assertTrue($out instanceof Exception\CurlExec);
5256
$this->assertTrue($out->getMessage() === 'curl_exec() error.');
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2021-2021 Anthon Pang. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package WebDriver
19+
*
20+
* @author Anthon Pang <[email protected]>
21+
*/
22+
23+
namespace Test\WebDriver;
24+
25+
use Test\WebDriver\WebDriverTestBase;
26+
use WebDriver\Session;
27+
28+
/**
29+
* GeckoDriver
30+
*
31+
* @package WebDriver
32+
*
33+
* @group Functional
34+
*/
35+
class GeckoDriverTest extends WebDriverTestBase
36+
{
37+
protected $testWebDriverRootUrl = 'http://localhost:4444';
38+
protected $testWebDriverName = 'geckodriver';
39+
40+
/**
41+
* Test driver session
42+
*/
43+
public function testSession()
44+
{
45+
try {
46+
$this->session = $this->driver->session();
47+
} catch (\Exception $e) {
48+
if ($this->isWebDriverDown($e)) {
49+
$this->markTestSkipped("{$this->testWebDriverName} server not running");
50+
51+
return;
52+
}
53+
54+
throw $e;
55+
}
56+
57+
$this->assertTrue($this->session instanceof Session);
58+
}
59+
60+
/**
61+
* Test driver status
62+
*/
63+
public function testStatus()
64+
{
65+
try {
66+
$status = $this->driver->status();
67+
} catch (\Exception $e) {
68+
if ($this->isWebDriverDown($e)) {
69+
$this->markTestSkipped("{$this->testWebDriverName} server not running");
70+
71+
return;
72+
}
73+
74+
throw $e;
75+
}
76+
77+
$this->assertCount(2, $status);
78+
$this->assertTrue(isset($status['message']));
79+
$this->assertTrue(isset($status['ready']));
80+
}
81+
}

test/Test/WebDriver/SeleniumWebDriverTest.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
namespace Test\WebDriver;
2424

2525
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;
2631

2732
/**
2833
* Selenium WebDriver
@@ -35,4 +40,165 @@ class SeleniumWebDriverTest extends WebDriverTestBase
3540
{
3641
protected $testWebDriverRootUrl = 'http://localhost:4444/wd/hub';
3742
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+
}
38204
}

0 commit comments

Comments
 (0)