Skip to content

Commit 468e2fd

Browse files
author
LuisRBarreras
committed
Magiic Methods Navigation and Window, Test Navigation
2 parents 91fdea1 + fe86d1e commit 468e2fd

File tree

12 files changed

+168
-111
lines changed

12 files changed

+168
-111
lines changed

SeleniumClient/CapabilityType.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818
abstract class CapabilityType {
1919

20-
const browserName = "browserName";
21-
const version = "version";
22-
const platform = "platform";
23-
const javascriptEnabled = "javascriptEnabled";
24-
const takesScreenshot = "takesScreenshot";
25-
const handlesAlerts = "handlesAlerts";
26-
const databaseEnabled = "databaseEnabled";
27-
const locationContextEnabled = "locationContextEnabled";
28-
const applicationCacheEnabled = "applicationCacheEnabled";
29-
const browserConnectionEnabled = "browserConnectionEnabled";
30-
const cssSelectorsEnabled = "cssSelectorsEnabled";
31-
const webStorageEnabled = "webStorageEnabled";
32-
const rotatable = "rotatable";
33-
const acceptSslCerts = "acceptSslCerts";
34-
const nativeEvents = "nativeEvents";
35-
const proxy = "proxy";
20+
const BROWSER_NAME = "browserName";
21+
const VERSION = "version";
22+
const PLATFORM = "platform";
23+
const JAVASCRIPT_ENABLED = "javascriptEnabled";
24+
const TAKES_SCREENSHOT = "takesScreenshot";
25+
const HANDLES_ALERT = "handlesAlerts";
26+
const DATABASE_ENABLED = "databaseEnabled";
27+
const LOCATION_CONTEXT_ENABLED = "locationContextEnabled";
28+
const APPLICATION_CACHE_ENABLED = "applicationCacheEnabled";
29+
const BROWSER_CONNECTION_ENABLED = "browserConnectionEnabled";
30+
const CSS_SELECTORS_ENABLED = "cssSelectorsEnabled";
31+
const WEB_STORAGE_ENABLED = "webStorageEnabled";
32+
const ROTATABLE = "rotatable";
33+
const ACCEPT_SSL_CERTS = "acceptSslCerts";
34+
const NATIVE_EVENTS = "nativeEvents";
35+
const PROXY = "proxy";
3636

3737
public static function isValidCapabilityType($capabilityType)
3838
{

SeleniumClient/DesiredCapabilities.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
use SeleniumClient\CapabilityType;
1919

20+
require_once __DIR__ . '/Exceptions.php';
21+
2022
class DesiredCapabilities {
2123

2224
private $_capabilities = null;
@@ -31,17 +33,17 @@ public function __construct($browser = null, $version = null, $platform = null)
3133
{
3234
if(isset($browser))
3335
{
34-
$this->setCapability(CapabilityType::browserName, $browser);
36+
$this->setCapability(CapabilityType::BROWSER_NAME, $browser);
3537
}
3638

3739
if(isset($version))
3840
{
39-
$this->setCapability(CapabilityType::version, $version);
41+
$this->setCapability(CapabilityType::VERSION, $version);
4042
}
4143

4244
if(isset($platform))
4345
{
44-
$this->setCapability(CapabilityType::platform, $platform);
46+
$this->setCapability(CapabilityType::PLATFORM, $platform);
4547
}
4648
}
4749

@@ -82,7 +84,7 @@ public function getCapability($capabilityType)
8284
*/
8385
public function getBrowserName()
8486
{
85-
return $this->getCapability(CapabilityType::browserName);
87+
return $this->getCapability(CapabilityType::BROWSER_NAME);
8688
}
8789

8890
/**
@@ -91,7 +93,7 @@ public function getBrowserName()
9193
*/
9294
public function getPlatform()
9395
{
94-
return $this->getCapability(CapabilityType::platform);
96+
return $this->getCapability(CapabilityType::PLATFORM);
9597
}
9698

9799
/**
@@ -100,7 +102,7 @@ public function getPlatform()
100102
*/
101103
public function getVersion()
102104
{
103-
return $this->getCapability(CapabilityType::version);
105+
return $this->getCapability(CapabilityType::VERSION);
104106
}
105107

106108
/**
@@ -109,7 +111,7 @@ public function getVersion()
109111
*/
110112
public function getIsJavaScriptEnabled()
111113
{
112-
return $this->getCapability(CapabilityType::javascriptEnabled);
114+
return $this->getCapability(CapabilityType::JAVASCRIPT_ENABLED);
113115
}
114116

115117
/**
@@ -120,14 +122,32 @@ public function getIsJavaScriptEnabled()
120122
*/
121123
public function setCapability($capabilityType,$value)
122124
{
123-
if(CapabilityType::isValidCapabilityType($capabilityType))
125+
if($this->isValidCapabilityAndValue($capabilityType,$value))
124126
{
125127
$this->_capabilities[$capabilityType] = $value;
126128
}
129+
}
130+
131+
private function isValidCapabilityAndValue($capabilityType,$value)
132+
{
133+
if(CapabilityType::isValidCapabilityType($capabilityType))
134+
{
135+
switch($capabilityType)
136+
{
137+
case CapabilityType::BROWSER_NAME:
138+
if(!BrowserType::isValidBrowserType($value)) { throw new InvalidBrowserTypeException("'{$value}' is not a valid browser type"); }
139+
break;
140+
case CapabilityType::PLATFORM:
141+
if(!PlatformType::isValidPlatformType($value)) { throw new InvalidPlatformTypeException("'{$value}' is not a valid platform type"); }
142+
break;
143+
}
144+
}
127145
else
128146
{
129-
throw new \Exception("'".$capabilityType ."' is not an valid capability type");
147+
throw new InvalidCapabilityTypeException("'{$capabilityType}' is not a valid capability type");
130148
}
149+
150+
return true;
131151
}
132152

133153
public function __toString()

SeleniumClient/Exceptions.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
2-
// Copyright 2012-present Nearsoft, Inc
3-
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
2+
// Copyright 2012-present Nearsoft, Inc
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

1616
namespace SeleniumClient;
@@ -19,6 +19,12 @@ class WebDriverWaitTimeoutException extends \Exception {}
1919

2020
class EmptyValueException extends \Exception { }
2121

22+
class InvalidCapabilityTypeException extends \Exception { }
23+
24+
class InvalidBrowserTypeException extends \Exception { }
25+
26+
class InvalidPlatformTypeException extends \Exception { }
27+
2228
class NotStringException extends \Exception
2329
{
2430
public function __construct($functionName, $paramName) { parent::__construct("function {$functionName}: {$paramName} is not String."); }

SeleniumClient/WebDriver.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929

3030
class WebDriver
3131
{
32-
private $_hubUrl = null;
33-
private $_sessionId = null;
32+
private $_hubUrl = null;
33+
private $_sessionId = null;
3434
private $_screenshotsDirectory = null;
35-
private $_environment = HttpFactory::PRODUCTIONMODE;
36-
private $_capabilities = null;
37-
private $_httpClient = null;
38-
private $_options = null;
39-
private $_navigate = null;
35+
private $_environment = HttpFactory::PRODUCTIONMODE;
36+
private $_capabilities = null;
37+
private $_httpClient = null;
38+
private $_options = null;
39+
private $_navigate = null;
40+
private $_targetLocator = null;
4041

4142
/**
4243
* @param DesiredCapabilities $desiredCapabilities
@@ -45,8 +46,8 @@ class WebDriver
4546
*/
4647
public function __construct(DesiredCapabilities $desiredCapabilities = null, $host = "http://localhost", $port = 4444)
4748
{
48-
$this->_hubUrl = $host . ":" . strval($port) . "/wd/hub";
49-
if(!isset($desiredCapabilities)) { $desiredCapabilities = new DesiredCapabilities("firefox"); }
49+
$this->_hubUrl = "{$host}:{$port}/wd/hub";
50+
isset($desiredCapabilities) ? : $desiredCapabilities = new DesiredCapabilities("firefox");
5051
$this->_httpClient = HttpFactory::getClient($this->_environment);
5152
$this->startSession($desiredCapabilities);
5253
}
@@ -231,6 +232,7 @@ public function getHttpClient() { return $this->_httpClient; }
231232
*/
232233
public function getHubUrl() { return $this->_hubUrl; }
233234

235+
234236
/**
235237
* Get Navigation object
236238
* @return Selenium\Navigation
@@ -254,35 +256,51 @@ public function setNavigate($navigate)
254256
*/
255257
public function getSessionId() { return $this->_sessionId; }
256258

257-
/**
258-
* Get default screenshots directory
259-
* @return String
260-
*/
261-
public function getScreenShotsDirectory() { return $this->_screenshotsDirectory; }
262-
263259
/**
264260
* Sets default screenshots directory for files to be stored in
265261
* @param String $value
266262
*/
267263
public function setScreenShotsDirectory($value) { $this->_screenshotsDirectory = $value; }
264+
265+
/**
266+
* Get default screenshots directory
267+
* @return String
268+
*/
269+
public function getScreenShotsDirectory() { return $this->_screenshotsDirectory; }
268270

271+
/**
272+
* Get Navigation object
273+
* @return Selenium\Navigation
274+
*/
275+
public function navigate()
276+
{
277+
isset($this->_navigate) ? : $this->_navigate = new Navigation($this);
278+
return $this->_navigate;
279+
}
280+
269281
/**
270282
* Gets Options object
271283
* @return SeleniumClient\Options
272284
*/
273285
public function manage()
274286
{
287+
275288
if(!$this->_options) {
276289
$this->_options = new Options($this);
277290
}
291+
isset($this->_options) ? : $this->_options = new Options($this);
278292
return $this->_options;
279293
}
280294

281295
/**
282296
* Creates new target locator to be handled
283297
* @return \SeleniumClient\TargetLocator
284298
*/
285-
public function switchTo() { return new TargetLocator($this); }
299+
public function switchTo()
300+
{
301+
isset($this->_targetLocator) ? : $this->_targetLocator = new TargetLocator($this);
302+
return $this->_targetLocator;
303+
}
286304

287305
/**
288306
* Starts new Selenium session
@@ -299,7 +317,8 @@ private function startSession(DesiredCapabilities $desiredCapabilities)
299317
$command = new Commands\StartSession($this, $params);
300318
$results = $command->execute();
301319
$this->_sessionId = $results['sessionId'];
302-
$this->_capabilities = $this->getCapabilities();
320+
$this->_capabilities = $results['value'];
321+
return $this->_capabilities;
303322
}
304323

305324
/**
@@ -332,7 +351,16 @@ public function quit()
332351
$command = new Commands\Quit($this);
333352
$command->execute();
334353
}
335-
354+
355+
/**
356+
* Closes current window
357+
*/
358+
public function close()
359+
{
360+
$command = new Commands\CloseWindow($this);
361+
$command->execute();
362+
}
363+
336364
/**
337365
* Navigates to specified url
338366
* @param String $url
@@ -369,7 +397,7 @@ public function status()
369397
* Gets current page source
370398
* @return String
371399
*/
372-
public function pageSource()
400+
public function getPageSource()
373401
{
374402
$command = new Commands\Source($this);
375403
$results = $command->execute();
@@ -380,7 +408,7 @@ public function pageSource()
380408
* Gets current page title
381409
* @return String
382410
*/
383-
public function title()
411+
public function getTitle()
384412
{
385413
$command = new Commands\Title($this);
386414
$results = $command->execute();

SeleniumClient/Window.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,7 @@ public function maximize() {
3131
$commannd = new Commands\WindowMaximize($this->_driver, null, array('window_handle' => 'current'));
3232
$commannd->execute();
3333
}
34-
35-
/**
36-
* Closes current window
37-
*/
38-
public function close()
39-
{
40-
$command = new Commands\CloseWindow($this->_driver);
41-
$command->execute();
42-
}
43-
34+
4435
/**
4536
* Sets current window size
4637
* @param Integer $width

SeleniumClientTest/Demo/Demo2.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use SeleniumClient\WebDriver;
77
use SeleniumClient\WebDriverWait;
88
use SeleniumClient\DesiredCapabilities;
9+
use SeleniumClient\CapabilityType;
10+
use SeleniumClient\BrowserType;
11+
use SeleniumClient\PlatformType;
912

1013
class Demo2Test extends PHPUnit_Framework_TestCase
1114
{
@@ -19,9 +22,13 @@ public function setUp()
1922
{
2023
$this->_testUrl = "http://nearsoft-php-seleniumclient.herokuapp.com/SeleniumClientTest/SandBox/";
2124

22-
$desiredCapabilities = new DesiredCapabilities("firefox");
25+
$desiredCapabilities = new DesiredCapabilities();
26+
$desiredCapabilities->setCapability(CapabilityType::BROWSER_NAME, BrowserType::FIREFOX);
27+
$desiredCapabilities->setCapability(CapabilityType::VERSION, "24.0");
28+
$desiredCapabilities->setCapability(CapabilityType::PLATFORM, PlatformType::WINDOWS);
2329

2430
$this->_driver = new WebDriver($desiredCapabilities);
31+
//note that the actual capabilities supported may be different to the desired capabilities specified
2532
}
2633

2734
public function tearDown()
@@ -52,7 +59,7 @@ public function testDemo2()
5259

5360
$this->assertEquals("test window", $webElement->getAttribute("value"));
5461

55-
$this->_driver->manage()->window()->close();
62+
$this->_driver->close();
5663

5764
$this->_driver->switchTo()->window($mainWindowHandle);
5865

SeleniumClientTest/Tests/AbstractTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function tearDown()
102102
// try to close any other windows that were opened
103103
try {
104104
$this->_driver->switchTo()->window($handle);
105-
$this->_driver->manage()->window()->close();
105+
$this->_driver->close();
106106
} catch ( Exception $e ) {
107107
}
108108
}

0 commit comments

Comments
 (0)