├── .gitignore ├── README.md ├── composer.json ├── demo ├── Demo1.php └── Demo2.php ├── src └── Nearsoft │ └── SeleniumClient │ ├── Alert.php │ ├── BrowserType.php │ ├── By.php │ ├── COPYING.txt │ ├── CREDITS.txt │ ├── CapabilityType.php │ ├── Commands │ ├── Command.php │ └── Dictionary.php │ ├── Cookie.php │ ├── DesiredCapabilities.php │ ├── Exceptions │ ├── ElementIsNotSelectable.php │ ├── ElementNotVisible.php │ ├── IMEEngineActivationFailed.php │ ├── IMENotAvailable.php │ ├── InvalidCookieDomain.php │ ├── InvalidElementCoordinates.php │ ├── InvalidElementState.php │ ├── InvalidSelector.php │ ├── JavaScriptError.php │ ├── NoAlertOpenError.php │ ├── NoSuchElement.php │ ├── NoSuchFrame.php │ ├── NoSuchWindow.php │ ├── ScriptTimeout.php │ ├── StaleElementReference.php │ ├── Timeout.php │ ├── UnableToSetCookie.php │ ├── UnexpectedAlertOpen.php │ ├── UnknownCommand.php │ ├── UnknownError.php │ ├── WebDriverWaitTimeout.php │ └── XPathLookupError.php │ ├── Http │ ├── Exceptions │ │ ├── FailedCommand.php │ │ ├── InvalidCommandMethod.php │ │ ├── InvalidRequest.php │ │ ├── MissingCommandParameters.php │ │ └── UnimplementedCommand.php │ ├── HttpClient.php │ ├── HttpFactory.php │ └── SeleniumAdapter.php │ ├── NOTICE.txt │ ├── Navigation.php │ ├── Options.php │ ├── PlatformType.php │ ├── SelectElement.php │ ├── TargetLocator.php │ ├── Timeouts.php │ ├── WebDriver.php │ ├── WebDriverWait.php │ ├── WebElement.php │ └── Window.php └── test ├── Nearsoft └── SeleniumClient │ ├── AbstractTest.php │ ├── AlertTest.php │ ├── DesiredCapabilitiesTest.php │ ├── NavigationTest.php │ ├── OptionsTest.php │ ├── SelectElementTest.php │ ├── TargetLocatorTest.php │ ├── TimeoutTest.php │ ├── WebDriverTest.php │ ├── WebDriverWaitTest.php │ ├── WebElementTest.php │ └── WindowTest.php ├── bootstrap.php └── phpunit.xml.dist /.gitignore: -------------------------------------------------------------------------------- 1 | /SeleniumClientTest/phpunit.xml 2 | /build 3 | /.idea 4 | /*.project 5 | /*.buildpath 6 | /*.settings 7 | /SeleniumServer/ 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP-SeleniumClient 2 | ========================= 3 | 4 | PHP interaction with Selenium Webdriver API 5 | 6 | ##Description 7 | 8 | This library allows the interaction with Selenium Server V2 in PHP. It communicates with the WebDriver API through the official JsonWireProtocol. 9 | 10 | One of the goals is to provide a client usage as close as possible to the Selenium Official libraries (such as Java's, c#'s). Most methods are named as the same as of these libraries'. In this way, whenever a developer runs into ideas or documentation in Java Client, the same techniques can be implemented by using this library. 11 | 12 | ##Documentation 13 | 14 | [Version 2](http://nearsoft-php-seleniumclient.herokuapp.com/docs/v2/) 15 | 16 | [Version 1](http://nearsoft-php-seleniumclient.herokuapp.com/docs/v1/) 17 | 18 | ##Quickstart 19 | 20 | * Start a session by creating an instance of WebDriver. By default, the test will run in firefox 21 | 22 | $driver = new WebDriver(); 23 | 24 | * As an alternative you can define desired capabilities for the session 25 | 26 | $desiredCapabilities = new DesiredCapabilities("chrome"); 27 | $driver = new WebDriver($desiredCapabilities); 28 | 29 | * Navigate by using the WebDriver::get method from the WebDriver Class 30 | 31 | $driver->get("www.nearsoft.com"); 32 | 33 | * Get elements from the DOM in current location 34 | 35 | $textbox1 = $driver->findElement(By::id("someTextBoxId")); 36 | 37 | $button1 = $driver->findElement(By::cssSelector("html body div#content input#someButtonId")); 38 | 39 | * Manipulate located elements 40 | 41 | $textbox1->sendKeys("Some text to send"); 42 | 43 | $textbox1->getAttribute("value"); 44 | 45 | $button1->click(); 46 | 47 | * Find element within elements 48 | 49 | $modal1->findElement(By::id("someModalId")); 50 | $listItems = $modal1->findElements(By::tagName("li")); 51 | 52 | * Switch between windows 53 | 54 | $driver->switchTo()->window("windowName"); 55 | 56 | * Manage alerts 57 | $alert = $driver->switchTo()->alert(); 58 | $alert->getText(); 59 | $alert->accept(); 60 | $alert->dismiss(); 61 | 62 | * Wait for elements to be present 63 | 64 | $webElement = $driver->waitForElementUntilIsPresent(By::id("someElementId")); 65 | 66 | //or 67 | 68 | $wait = new WebDriverWait(8); 69 | $webElement = $wait->until($driver,"findElement",array(By::id("someElementId"),true)); 70 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nearsoft/php-selenium-client", 3 | "type": "library", 4 | "description": "This library allows creating Selenium Server V2 tests in PHP. It communicates with the WebDriver API through the official JsonWireProtocol.", 5 | "keywords": ["selenium","testing","automation","browser","nearsoft"], 6 | "license": "Apache 2", 7 | "minimum-stability": "stable", 8 | "authors": [ 9 | { 10 | "name": "Nearsoft, Inc", 11 | "homepage": "http://www.nearsoft.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.8" 16 | }, 17 | "autoload": { 18 | "psr-0": { 19 | "Nearsoft\\": "src/" 20 | } 21 | }, 22 | "archive": { 23 | "exclude": ["/demo", "/test"] 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /demo/Demo1.php: -------------------------------------------------------------------------------- 1 | _testUrl = "http://nearsoft-php-seleniumclient.herokuapp.com/sandbox/"; 21 | 22 | $desiredCapabilities = new DesiredCapabilities("firefox"); 23 | 24 | $this->_driver = new WebDriver($desiredCapabilities); 25 | } 26 | 27 | public function tearDown() 28 | { 29 | if($this->_driver != null) { $this->_driver->quit(); } 30 | } 31 | 32 | public function testDemo1() 33 | { 34 | //get url 35 | $this->_driver->get($this->_testUrl); 36 | sleep(4); 37 | //access text input 38 | $webElement = $this->_driver->findElement(By::id("txt1")); 39 | $webElement->clear(); 40 | $webElement->sendKeys("Text sent 1"); 41 | 42 | $this->assertEquals("Text sent 1", $webElement->getAttribute("value")); 43 | 44 | $webElement = $this->_driver->findElement(By::id("txt2")); 45 | $webElement->clear(); 46 | $webElement->sendKeys("Text sent 2"); 47 | 48 | $this->assertEquals("Text sent 2", $webElement->getAttribute("value")); 49 | 50 | //access listbox 51 | $selectElement = new SelectElement($this->_driver->findElement(By::id("sel1"))); 52 | $selectElement->selectByValue("4"); 53 | 54 | $this->assertTrue($selectElement->getElement()->findElement(By::xPath(".//option[@value = 4]"))->isSelected()); 55 | 56 | //access checkbox 57 | $webElement = $this->_driver->findElement(By::cssSelector("html body table tbody tr td fieldset form p input#chk3")); 58 | 59 | $webElement->click(); 60 | $this->assertTrue($webElement->isSelected()); 61 | 62 | //access radio 63 | $webElement = $this->_driver->findElement(By::id("rd3")); 64 | $webElement->click(); 65 | 66 | $this->assertTrue($webElement->isSelected()); 67 | 68 | //access button 69 | $webElement = $this->_driver->findElement(By::id("btnSubmit")); 70 | $webElement->click(); 71 | 72 | //access h2 73 | $webElement = $this->_driver->findElement(By::cssSelector("html body h2#h2FormReceptor")); 74 | $this->assertEquals("Form receptor", $webElement->getText()); 75 | 76 | //navigation 77 | $this->_driver->get('http://www.nearsoft.com'); 78 | 79 | $this->_driver->navigate()->refresh(); 80 | 81 | $this->_driver->navigate()->back(); 82 | 83 | $this->_driver->navigate()->forward(); 84 | 85 | sleep(5); 86 | } 87 | } -------------------------------------------------------------------------------- /demo/Demo2.php: -------------------------------------------------------------------------------- 1 | _testUrl = "http://nearsoft-php-seleniumclient.herokuapp.com/sandbox/"; 24 | 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); 29 | 30 | $this->_driver = new WebDriver($desiredCapabilities); 31 | //note that the actual capabilities supported may be different to the desired capabilities specified 32 | } 33 | 34 | public function tearDown() 35 | { 36 | if($this->_driver != null) { $this->_driver->quit(); } 37 | } 38 | 39 | public function testDemo2() 40 | { 41 | //click button/get alert text 42 | $this->_driver->get($this->_testUrl); 43 | $this->_driver->findElement(By::id("btnAlert"))->click(); 44 | $alert = $this->_driver->switchTo()->alert(); 45 | $this->assertEquals("Here is the alert", $alert->getText()); 46 | $alert->accept(); 47 | 48 | //get main window handle 49 | $mainWindowHandle = $this->_driver->getWindowHandle(); 50 | 51 | //open popup window / handle its elements 52 | $this->_driver->findElement(By::id("btnPopUp1"))->click(); 53 | 54 | $this->_driver->switchTo()->window("popup1"); 55 | 56 | $webElement = $this->_driver->waitForElementUntilIsPresent(By::id("txt1")); 57 | 58 | $webElement->sendKeys("test window"); 59 | 60 | $this->assertEquals("test window", $webElement->getAttribute("value")); 61 | 62 | $this->_driver->close(); 63 | 64 | $this->_driver->switchTo()->window($mainWindowHandle); 65 | 66 | //get iframe / handle its elements 67 | $this->_driver->switchTo()->frame("iframe1"); 68 | 69 | $webElement = $this->_driver->waitForElementUntilIsPresent(By::id("txt1")); 70 | $webElement->sendKeys("test iframe"); 71 | 72 | $this->assertEquals("test iframe", $webElement->getAttribute("value")); 73 | 74 | $this->_driver->switchTo()->window($mainWindowHandle); 75 | 76 | //wait for element to be present 77 | $this->_driver->findElement(By::id("btnAppendDiv"))->click(); 78 | $wait = new WebDriverWait(8); 79 | $label = $wait->until($this->_driver,"findElement",array(By::id("dDiv1-0"),true)); 80 | $this->assertEquals("Some content",$label->getText()); 81 | 82 | sleep(5); 83 | } 84 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Alert.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; } 27 | 28 | /** 29 | * Gets the text of the alert. 30 | * @return String 31 | */ 32 | public function getText() 33 | { 34 | $command = new Commands\Command($this->_driver, 'get_alert_text'); 35 | $results = $command->execute(); 36 | return $results['value']; 37 | } 38 | 39 | /** 40 | * Dismisses the alert. 41 | */ 42 | public function dismiss() 43 | { 44 | $command = new Commands\Command($this->_driver, 'dismiss_alert'); 45 | $command->execute(); 46 | } 47 | 48 | /** 49 | * Accepts the alert. 50 | */ 51 | public function accept() 52 | { 53 | $command = new Commands\Command($this->_driver, 'accept_alert'); 54 | $command->execute(); 55 | } 56 | 57 | /** 58 | * Sends keys to the alert. 59 | * @param String $string 60 | */ 61 | public function sendKeys($string) 62 | { 63 | if(is_string($string)){ 64 | $params = array ('text' => $string); 65 | $command = new Commands\Command($this->_driver, 'set_alert_text', $params); 66 | $command->execute(); 67 | } 68 | else{ 69 | throw new \Exception("Value must be a string"); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/BrowserType.php: -------------------------------------------------------------------------------- 1 | getConstants() as $constantName => $constantValue) 33 | { 34 | if ($constantValue == $browserType) { $validBrowserType = true; } 35 | } 36 | 37 | return $validBrowserType; 38 | } 39 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/By.php: -------------------------------------------------------------------------------- 1 | _strategy; } 23 | 24 | private $_selectorValue = ""; 25 | public function getSelectorValue() { return $this->_selectorValue; } 26 | 27 | /** 28 | * Locate by element's css class name 29 | * @param String $selectorValue 30 | * @return Nearsoft\SeleniumClient\By 31 | */ 32 | public static function className($selectorValue) { return new By("class name", $selectorValue); } 33 | 34 | /** 35 | * Locate by element's css selector path 36 | * @param String $selectorValue 37 | * @return Nearsoft\SeleniumClient\By 38 | */ 39 | public static function cssSelector($selectorValue) { return new By("css selector", $selectorValue); } 40 | 41 | /** 42 | * Locate by element's id 43 | * @param String $selectorValue 44 | * @return Nearsoft\SeleniumClient\By 45 | */ 46 | public static function id($selectorValue) { return new By("id", $selectorValue); } 47 | 48 | /** 49 | * Locate by JavaScript selector 50 | * @param string $selectorValue 51 | * @param string $selectorDefinition 52 | * @return Nearsoft\SeleniumClient\By 53 | */ 54 | public static function jsSelector($selectorValue, $selectorDefinition = '$') 55 | { 56 | return new By("js selector {$selectorDefinition}", $selectorValue); 57 | } 58 | 59 | /** 60 | * Locate by element's name 61 | * @param String $selectorValue 62 | * @return Nearsoft\SeleniumClient\By 63 | */ 64 | public static function name($selectorValue) { return new By("name", $selectorValue); } 65 | 66 | /** 67 | * Locate by element's link text 68 | * @param String $selectorValue 69 | * @return Nearsoft\SeleniumClient\By 70 | */ 71 | public static function linkText($selectorValue) { return new By("link text", $selectorValue); } 72 | 73 | /** 74 | * Locate by part of element's link text 75 | * @param String $selectorValue 76 | * @return Nearsoft\SeleniumClient\By 77 | */ 78 | public static function partialLinkText($selectorValue) { return new By("partial link text", $selectorValue); } 79 | 80 | /** 81 | * Locate by element's tag name 82 | * @param String $selectorValue 83 | * @return Nearsoft\SeleniumClient\By 84 | */ 85 | public static function tagName($selectorValue) { return new By("tag name", $selectorValue); } 86 | 87 | /** 88 | * Locate by element's xPath 89 | * @param String $selectorValue 90 | * @return Nearsoft\SeleniumClient\By 91 | */ 92 | public static function xPath($selectorValue) { return new By("xpath", $selectorValue); } 93 | 94 | public function __toString() { return "By strategy: '" . $this->_strategy . "', strategy factor '" . $this->_selectorValue."'"; } 95 | 96 | function __construct ($strategy, $selectorValue) 97 | { 98 | $this->_strategy = $strategy; 99 | $this->_selectorValue = $selectorValue; 100 | } 101 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/COPYING.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2007-2009 Google Inc. 191 | Copyright 2007-2009 WebDriver committers 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | 205 | -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/CREDITS.txt: -------------------------------------------------------------------------------- 1 | Credits 2 | ======= 3 | 4 | The following people have offered help, support and/or code to 5 | SeleniumClient. If you feel that you should be on this list but aren't, 6 | then please feel free to raise a ticket on the project site 7 | (https://github.com/Nearsoft/PHP-SeleniumClient) or send an email directly 8 | to one of the project's maintainers. 9 | 10 | Cast 11 | ==== 12 | 13 | Maldonado Bustamante Jesus Yair https://github.com/yair27 14 | Santiago Badillo Job Apolonio https://github.com/jobsantiago 15 | Mark Dexter https://github.com/dextercowley 16 | Miloslav Nenadál https://github.com/nenadalm 17 | ron-inbar https://github.com/ron-inbar 18 | Andrew Mackrodt https://github.com/andrewmackrodt 19 | Alexey Alexeev https://github.com/voodoo144 20 | Luis Barreras https://github.com/LuisRBarreras -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/CapabilityType.php: -------------------------------------------------------------------------------- 1 | getConstants() as $constantName => $constantValue) 45 | { 46 | if($constantValue == $capabilityType ) 47 | { 48 | $validCapabilityType = true; 49 | } 50 | } 51 | 52 | return $validCapabilityType; 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Commands/Command.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; 35 | $this->_name = $name; 36 | $this->_params = $params; 37 | $this->_urlParams = $urlParams; 38 | 39 | $this->setUrl(); 40 | $this->setHttpMethod(); 41 | 42 | return $this; 43 | } 44 | 45 | public function getUrl() { return $this->_url; } 46 | public function getParams() { return $this->_params; } 47 | public function getHttpMethod() { return $this->_httpMethod; } 48 | public function getPolling() { return $this->_polling; } 49 | public function getResponse() { return $this->_response; } 50 | 51 | private function setUrl() 52 | { 53 | $path = Dictionary::$commands[$this->_name]['path']; 54 | $path = str_replace('{session_id}', $this->_driver->getSessionId(), $path); 55 | 56 | if($this->_urlParams){ 57 | foreach($this->_urlParams as $param_name => $value) { 58 | $path = str_replace("{{$param_name}}", $value, $path); 59 | } 60 | } 61 | 62 | $this->_url = "{$this->_driver->getHubUrl()}/{$path}"; 63 | } 64 | 65 | private function setHttpMethod() 66 | { 67 | $this->_httpMethod = Dictionary::$commands[$this->_name]['http_method']; 68 | } 69 | 70 | public function setPolling($value) 71 | { 72 | $this->_polling = $value; 73 | } 74 | 75 | public function execute($trace = false) 76 | { 77 | $httpClient = $this->_driver->getHttpClient(); 78 | 79 | $this->_response = $httpClient->execute($this, $trace); 80 | 81 | return $this->_response['body']; 82 | } 83 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Commands/Dictionary.php: -------------------------------------------------------------------------------- 1 | array('http_method' => HttpClient::POST , 'path' => "session"), 23 | 'get_capabilities' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}"), 24 | 'quit' => array('http_method' => HttpClient::DELETE , 'path' => "session/{session_id}"), 25 | 26 | 'get_url' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/url"), 27 | 'get_current_url' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/url"), 28 | 29 | 'get_sessions' => array('http_method' => HttpClient::GET , 'path' => "sessions"), 30 | 31 | 'implicit_wait' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/timeouts/implicit_wait"), 32 | 'status' => array('http_method' => HttpClient::GET , 'path' => "status"), 33 | 34 | 'forward' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/forward"), 35 | 'back' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/back"), 36 | 'refresh' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/refresh"), 37 | 38 | 'source' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/source"), 39 | 'title' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/title"), 40 | 41 | 'screenshot' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/screenshot"), 42 | 43 | 'element' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element"), 44 | 'element_in_element' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element/{element_id}/element"), 45 | 'elements' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/elements"), 46 | 'elements_in_element' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element/{element_id}/elements"), 47 | 'active_element' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element/active"), 48 | 'element_value' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element/{element_id}/value"), 49 | 'element_text' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/text"), 50 | 'element_tag_name' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/name"), 51 | 'element_attribute' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/attribute/{attribute_name}"), 52 | 'element_property_name' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/css/{property_name}"), 53 | 'element_is_selected' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/selected"), 54 | 'element_is_displayed' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/displayed"), 55 | 'element_is_enabled' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/enabled"), 56 | 'element_size' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/size"), 57 | 'clear_element' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element/{element_id}/clear"), 58 | 'click_element' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element/{element_id}/click"), 59 | 'element_submit' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/element/{element_id}/submit"), 60 | 'describe_element' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}"), 61 | 'element_location' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/location"), 62 | 'element_location_view' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/location_in_view"), 63 | 'compare_to_other' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/element/{element_id}/equals/{element_id_compare}"), 64 | 65 | 'load_timeout' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/timeouts"), 66 | 'async_script_timeout' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/timeouts/async_script"), 67 | 68 | 'execute_script' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/execute"), 69 | 'execute_async_script' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/execute_async"), 70 | 71 | 'frame' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/frame"), 72 | 'window' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/window"), 73 | 'window_maximize' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/window/{window_handle}/maximize"), 74 | 'close_window' => array('http_method' => HttpClient::DELETE , 'path' => "session/{session_id}/window"), 75 | 'window_handle' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/window_handle"), 76 | 'window_handles' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/window_handles"), 77 | 'set_window_size' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/window/{window_handle}/size"), 78 | 'get_window_size' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/window/{window_handle}/size"), 79 | 'set_window_position' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/window/{window_handle}/position"), 80 | 'get_window_position' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/window/{window_handle}/position"), 81 | 82 | 'add_cookie' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/cookie"), 83 | 'get_cookies' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/cookie"), 84 | 'clear_cookie' => array('http_method' => HttpClient::DELETE , 'path' => "session/{session_id}/cookie/{cookie_name}"), 85 | 'clear_cookies' => array('http_method' => HttpClient::DELETE , 'path' => "session/{session_id}/cookie"), 86 | 87 | 'dismiss_alert' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/dismiss_alert"), 88 | 'accept_alert' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/accept_alert"), 89 | 'get_alert_text' => array('http_method' => HttpClient::GET , 'path' => "session/{session_id}/alert_text"), 90 | 'set_alert_text' => array('http_method' => HttpClient::POST , 'path' => "session/{session_id}/alert_text") 91 | ); 92 | } 93 | -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Cookie.php: -------------------------------------------------------------------------------- 1 | _name = $name; } 38 | 39 | if($value != null){ $this->_value = $value; } 40 | 41 | if($path != null){ $this->_path = $path; } 42 | 43 | if($domain != null){ $this->_domain = $domain; } 44 | 45 | if($secure != null){ $this->_secure = $secure; } 46 | 47 | if($expiry != null){ $this->_expiry = $expiry; } 48 | } 49 | 50 | public function getName() { return $this->_name; } 51 | public function getValue() { return $this->_value; } 52 | public function getPath() { return $this->_path; } 53 | public function getDomain() { return $this->_domain; } 54 | public function getSecure() { return $this->_secure; } 55 | public function getExpiry() { return $this->_expiry; } 56 | 57 | public static function buildFromArray($items) 58 | { 59 | $cookies = array(); 60 | 61 | foreach($items as $item) 62 | { 63 | $cookies[] = new Cookie($item['name'],$item['value'],$item['path'],$item['domain'],$item['secure'],$item['expiry']); 64 | } 65 | 66 | return $cookies; 67 | } 68 | 69 | public function getArray() { 70 | $array = array( 71 | "name" => $this->_name, 72 | "value" => $this->_value, 73 | "path" => $this->_path, 74 | "domain" => $this->_domain, 75 | "secure" => $this->_secure, 76 | "expiry" => $this->_expiry 77 | ); 78 | return $array; 79 | } 80 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/DesiredCapabilities.php: -------------------------------------------------------------------------------- 1 | setCapability(CapabilityType::BROWSER_NAME, $browser); 35 | } 36 | 37 | if(isset($version)) 38 | { 39 | $this->setCapability(CapabilityType::VERSION, $version); 40 | } 41 | 42 | if(isset($platform)) 43 | { 44 | $this->setCapability(CapabilityType::PLATFORM, $platform); 45 | } 46 | } 47 | 48 | /** 49 | * Gets current capabilities 50 | * @return Array 51 | */ 52 | public function getCapabilities() 53 | { 54 | return $this->_capabilities; 55 | } 56 | 57 | /** 58 | * Gets specified capability 59 | * @param String $capabilityType 60 | * @throws \Exception 61 | * @return String 62 | */ 63 | public function getCapability($capabilityType) 64 | { 65 | if(!CapabilityType::isValidCapabilityType($capabilityType)) 66 | { 67 | throw new \Exception("'".$capabilityType ."' is not an valid capability type"); 68 | } 69 | else if(!isset($this->_capabilities[$capabilityType])) 70 | { 71 | return null; 72 | } 73 | else 74 | { 75 | return $this->_capabilities[$capabilityType]; 76 | } 77 | } 78 | 79 | /** 80 | * Gets browser name 81 | * @return String 82 | */ 83 | public function getBrowserName() 84 | { 85 | return $this->getCapability(CapabilityType::BROWSER_NAME); 86 | } 87 | 88 | /** 89 | * Gets platform name 90 | * @return String 91 | */ 92 | public function getPlatform() 93 | { 94 | return $this->getCapability(CapabilityType::PLATFORM); 95 | } 96 | 97 | /** 98 | * Gets version 99 | * @return String 100 | */ 101 | public function getVersion() 102 | { 103 | return $this->getCapability(CapabilityType::VERSION); 104 | } 105 | 106 | /** 107 | * Gets whether javascript is enabled 108 | * @return String 109 | */ 110 | public function getIsJavaScriptEnabled() 111 | { 112 | return $this->getCapability(CapabilityType::JAVASCRIPT_ENABLED); 113 | } 114 | 115 | /** 116 | * Sets specified capability 117 | * @param String $capabilityType 118 | * @param String $value 119 | * @throws \Exception 120 | */ 121 | public function setCapability($capabilityType,$value) 122 | { 123 | if($this->isValidCapabilityAndValue($capabilityType,$value)) 124 | { 125 | $this->_capabilities[$capabilityType] = $value; 126 | } 127 | } 128 | 129 | private function isValidCapabilityAndValue($capabilityType,$value) 130 | { 131 | if(CapabilityType::isValidCapabilityType($capabilityType)) 132 | { 133 | switch($capabilityType) 134 | { 135 | case CapabilityType::BROWSER_NAME: 136 | if(!BrowserType::isValidBrowserType($value)) { throw new Exception("'{$value}' is not a valid browser type"); } 137 | break; 138 | case CapabilityType::PLATFORM: 139 | if(!PlatformType::isValidPlatformType($value)) { throw new Exception("'{$value}' is not a valid platform type"); } 140 | break; 141 | } 142 | } 143 | else 144 | { 145 | throw new Exception("'{$capabilityType}' is not a valid capability type"); 146 | } 147 | 148 | return true; 149 | } 150 | 151 | public function __toString() 152 | { 153 | $result = "DesiredCapabilities{BrowserName = " . $this->getBrowserName() ; 154 | 155 | if($this->getVersion()) 156 | { 157 | $result .= " Version = " . $this->getVersion(); 158 | } 159 | 160 | if($this->getPlatform()) 161 | { 162 | $result.= " Platform = " . $this->getPlatform(); 163 | } 164 | 165 | $result.= "}"; 166 | 167 | return $result; 168 | } 169 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Exceptions/ElementIsNotSelectable.php: -------------------------------------------------------------------------------- 1 | _traceAll; } 29 | 30 | public function setTraceAll($value) 31 | { 32 | $this->_traceAll = $value; 33 | return $this; 34 | } 35 | 36 | /** 37 | * @return string The response body 38 | * @throws \Exception 39 | */ 40 | public function execute(Commands\Command $command, $trace = false) 41 | { 42 | if ($command->getUrl() == "" || $command->getHttpMethod() == "") { throw new \Exception("Must specify URL and HTTP METHOD"); } 43 | 44 | $curl = curl_init($command->getUrl()); 45 | 46 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 47 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 48 | curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8','Accept: application/json')); 49 | 50 | if($command->getHttpMethod() == HttpClient::POST) 51 | { 52 | curl_setopt($curl, CURLOPT_POST, true); 53 | 54 | if ($command->getParams() && is_array($command->getParams())) 55 | { 56 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($command->getParams())); 57 | } 58 | else 59 | { 60 | curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 0')); 61 | } 62 | } 63 | else if ($command->getHttpMethod() == HttpClient::DELETE) { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); } 64 | else if ($command->getHttpMethod() == HttpClient::GET) { /*NO ACTION NECESSARY*/ } 65 | 66 | $rawResponse = trim(curl_exec($curl)); 67 | 68 | $responseBody = json_decode($rawResponse, true); 69 | 70 | $responseHeaders=curl_getinfo($curl); 71 | 72 | if ($this->_traceAll || $trace) 73 | { 74 | echo "\n***********************************************************************\n"; 75 | echo "URL: " . $command->getUrl() . "\n"; 76 | echo "METHOD: " . $command->getHttpMethod() . "\n"; 77 | 78 | echo "PARAMETERS: "; 79 | if (is_array($command->getParams())) { echo print_r($command->getParams()); } 80 | else echo "NONE"; { echo "\n"; } 81 | 82 | echo "RESULTS:" . print_r($responseBody); 83 | echo "\n"; 84 | echo "CURL INFO: "; 85 | echo print_r($responseHeaders); 86 | 87 | echo "\n***********************************************************************\n"; 88 | } 89 | 90 | curl_close($curl); 91 | 92 | $response = array( 93 | "headers" => $responseHeaders, 94 | "body" => $responseBody, 95 | ); 96 | 97 | return $response; 98 | } 99 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Http/HttpFactory.php: -------------------------------------------------------------------------------- 1 | validateSeleniumResponseCode($response,$command->getPolling()); 28 | $this->validateHttpCode($response,$command->getPolling()); 29 | return $response; 30 | } 31 | 32 | protected function validateHttpCode($response, $polling) 33 | { 34 | // Http response exceptions 35 | switch (intval(trim($response['headers']['http_code']))) 36 | { 37 | case 400: 38 | throw new HttpExceptions\MissingCommandParameters((string) $response['headers']['http_code'], $response['headers']['url']); 39 | break; 40 | case 405: 41 | throw new HttpExceptions\InvalidCommandMethod((string) $response['headers']['http_code'], $response['headers']['url']); 42 | break; 43 | case 500: 44 | if (!$polling) { throw new HttpExceptions\FailedCommand((string) $response['headers']['http_code'], $response['headers']['url']); } 45 | break; 46 | case 501: 47 | throw new HttpExceptions\UnimplementedCommand((string) $response['headers']['http_code'], $response['headers']['url']); 48 | break; 49 | default: 50 | // Looks for 4xx http codes 51 | if (preg_match("/^4[0-9][0-9]$/", $response['headers']['http_code'])) { throw new HttpExceptions\InvalidRequest((string) $response['headers']['http_code'], $response['headers']['url']); } 52 | break; 53 | } 54 | } 55 | 56 | protected function validateSeleniumResponseCode($response, $polling) 57 | { 58 | // Selenium response status exceptions 59 | if ($response['body'] != null) 60 | { 61 | if (isset($response['body']["value"]["localizedMessage"])) 62 | { 63 | $message = $response['body']["value"]["localizedMessage"]; 64 | } 65 | else 66 | { 67 | $message = ""; 68 | } 69 | switch (intval($response['body']["status"])) 70 | { 71 | case 7: 72 | if (!$polling) {throw new SeleniumExceptions\NoSuchElement($message);} 73 | break; 74 | case 8: 75 | throw new SeleniumExceptions\NoSuchFrame($message); 76 | break; 77 | case 9: 78 | throw new SeleniumExceptions\UnknownCommand($message); 79 | break; 80 | case 10: 81 | throw new SeleniumExceptions\StaleElementReference($message); 82 | break; 83 | case 11: 84 | throw new SeleniumExceptions\ElementNotVisible($message); 85 | break; 86 | case 12: 87 | throw new SeleniumExceptions\InvalidElementState($message); 88 | break; 89 | case 13: 90 | throw new SeleniumExceptions\UnknownError($message); 91 | break; 92 | case 15: 93 | throw new SeleniumExceptions\ElementIsNotSelectable($message); 94 | break; 95 | case 17: 96 | throw new SeleniumExceptions\JavaScriptError($message); 97 | break; 98 | case 19: 99 | throw new SeleniumExceptions\XPathLookupError($message); 100 | break; 101 | case 21: 102 | throw new SeleniumExceptions\Timeout($message); 103 | break; 104 | case 23: 105 | throw new SeleniumExceptions\NoSuchWindow($message); 106 | break; 107 | case 24: 108 | throw new SeleniumExceptions\InvalidCookieDomain($message); 109 | break; 110 | case 25: 111 | throw new SeleniumExceptions\UnableToSetCookie($message); 112 | break; 113 | case 26: 114 | throw new SeleniumExceptions\UnexpectedAlertOpen($message); 115 | break; 116 | case 27: 117 | throw new SeleniumExceptions\NoAlertOpenError($message); 118 | break; 119 | case 28: 120 | throw new SeleniumExceptions\ScriptTimeout($message); 121 | break; 122 | case 29: 123 | throw new SeleniumExceptions\InvalidElementCoordinates($message); 124 | break; 125 | case 30: 126 | throw new SeleniumExceptions\IMENotAvailable($message); 127 | break; 128 | case 31: 129 | throw new SeleniumExceptions\IMEEngineActivationFailed($message); 130 | break; 131 | case 32: 132 | throw new SeleniumExceptions\InvalidSelector($message); 133 | break; 134 | } 135 | } 136 | } 137 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/NOTICE.txt: -------------------------------------------------------------------------------- 1 | SeleniumClient 2 | Copyright 2012-present Nearsoft, Inc 3 | 4 | This product includes software developed at 5 | Nearsoft, Inc 6 | 7 | SeleniumClient interacts with Selenium WebDriver API through JsonWireProtocol -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Navigation.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; 25 | } 26 | 27 | /** 28 | * Navigate back in history 29 | */ 30 | public function back() 31 | { 32 | $command = new Commands\Command($this->_driver, 'back'); 33 | $command->execute(); 34 | } 35 | 36 | /** 37 | * Navigate forward in history 38 | */ 39 | public function forward() 40 | { 41 | $command = new Commands\Command($this->_driver, 'forward'); 42 | $command->execute(); 43 | } 44 | 45 | /** 46 | * Refreshes current page 47 | */ 48 | public function refresh() 49 | { 50 | $command = new Commands\Command($this->_driver, 'refresh'); 51 | $command->execute(); 52 | } 53 | 54 | /** 55 | * Navigates to specified url 56 | * @param String $url 57 | */ 58 | public function to($url) 59 | { 60 | $params = array ('url' => $url); 61 | $command = new Commands\Command($this->_driver, 'get_url', $params); 62 | $command->execute(); 63 | } 64 | 65 | 66 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Options.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; 27 | } 28 | 29 | /** 30 | * Gets Timeouts object 31 | * @return Nearsoft\SeleniumClient\Timeouts 32 | */ 33 | public function timeouts() 34 | { 35 | if(!$this->_timeouts) 36 | { 37 | $this->_timeouts = new Timeouts($this->_driver); 38 | } 39 | return $this->_timeouts; 40 | } 41 | 42 | /** 43 | * Gets Window object 44 | * @return Nearsoft\SeleniumClient\Window 45 | */ 46 | public function window() 47 | { 48 | isset($this->_window) ? : $this->setWindow(new Window($this->_driver)); 49 | return $this->_window; 50 | } 51 | 52 | /** Sets Window 53 | * @param $window 54 | */ 55 | public function setWindow($window) { $this->_window = $window; } 56 | 57 | /** 58 | * Sets cookie 59 | * @param String $name 60 | * @param String $value 61 | * @param String $path 62 | * @param String $domain 63 | * @param Boolean $secure 64 | * @param Integer $expiry 65 | */ 66 | public function addCookie($name, $value, $path = null, $domain = null, $secure = null, $expiry = null) 67 | { 68 | $cookie = new Cookie($name, $value, $path, $domain, $secure, $expiry); 69 | $params = array ('cookie' => $cookie->getArray()); 70 | $command = new Commands\Command($this->_driver, 'add_cookie', $params); 71 | $command->execute(); 72 | } 73 | 74 | /** 75 | * Gets current cookies 76 | * @return Array 77 | */ 78 | public function getCookies() 79 | { 80 | $command = new Commands\Command($this->_driver, 'get_cookies'); 81 | $results = $command->execute(); 82 | return Cookie::buildFromArray($results['value']); 83 | } 84 | 85 | /** 86 | * Gets a cookie by name 87 | * @return Array 88 | */ 89 | public function getCookieNamed($cookieName) 90 | { 91 | $cookies = $this->getCookies(); 92 | $matches = array_filter($cookies,function($cookie) use ($cookieName){ return $cookieName == $cookie->getName();}); 93 | if(count($matches) > 1){ throw new \Exception("For some reason there are more than 1 cookie named {$cookieName}");} 94 | $matches = array_values($matches); 95 | return count($matches) > 0 ? $matches[0] : null; 96 | } 97 | 98 | /** 99 | * Remove a cookie 100 | * @param Nearsoft\SeleniumClient\Cookie $cookie 101 | */ 102 | public function deleteCookie(Cookie $cookie) 103 | { 104 | $this->deleteCookieNamed($cookie->getName()); 105 | } 106 | 107 | /** 108 | * Remove a cookie 109 | * @param String $cookieName 110 | */ 111 | public function deleteCookieNamed($cookieName) 112 | { 113 | $command = new Commands\Command($this->_driver, 'clear_cookie', null, array('cookie_name' => $cookieName)); 114 | $command->execute(); 115 | } 116 | 117 | /** 118 | * Removes all current cookies 119 | */ 120 | public function deleteAllCookies() 121 | { 122 | $command = new Commands\Command($this->_driver, 'clear_cookies'); 123 | $command->execute(); 124 | } 125 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/PlatformType.php: -------------------------------------------------------------------------------- 1 | getConstants() as $constantName => $constantValue) 34 | { 35 | if ($constantValue == $platformType) { $validPlatformType = true; } 36 | } 37 | 38 | return $validPlatformType; 39 | } 40 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/SelectElement.php: -------------------------------------------------------------------------------- 1 | _element = $element; 25 | } 26 | 27 | /** 28 | * Gets related WebElement 29 | * @return WebElement 30 | */ 31 | public function getElement() 32 | { 33 | return $this->_element; 34 | } 35 | 36 | /** 37 | * Sets an option selected by its value 38 | * @param String $value 39 | * @throws \Exception 40 | */ 41 | public function selectByValue($value) 42 | { 43 | $options = $this->_element->findElements(By::xPath(".//option[@value = '" . $value . "']")); 44 | 45 | $matched = false; 46 | foreach($options as $option) 47 | { 48 | if(!$option->isSelected()) 49 | { 50 | $option->click(); 51 | } 52 | 53 | $matched = true; 54 | } 55 | 56 | if (!$matched) 57 | { 58 | throw new \Exception("Cannot locate option in select element with value: " . $value); 59 | } 60 | } 61 | 62 | /** 63 | * Sets an option selected by a partial text match 64 | * @param String $text 65 | * @throws \Exception 66 | */ 67 | public function selectByPartialText($text) 68 | { 69 | $options = $this->_element->findElements(By::xPath(".//option[contains(text(), '" . $text . "')]")); 70 | 71 | $matched = false; 72 | foreach($options as $option) 73 | { 74 | if(!$option->isSelected()) 75 | { 76 | $option->click(); 77 | } 78 | 79 | $matched = true; 80 | } 81 | 82 | if (!$matched) 83 | { 84 | throw new \Exception("Cannot locate option in select element with text: " . $text); 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/TargetLocator.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; 25 | } 26 | 27 | /** 28 | * Change to the Window by passing in the name 29 | * @param String $identifier either window name or window handle 30 | * @return Nearsoft\SeleniumClient\WebDriver The current webdriver 31 | */ 32 | public function window($identifier) 33 | { 34 | $params = array ('name' => $identifier); 35 | $command = new Commands\Command($this->_driver, 'window', $params); 36 | $command->execute(); 37 | return $this->_driver; 38 | } 39 | 40 | /** 41 | * Focus on specified frame 42 | * @param Mixed $identifier. Null will get default frame. String will get by frame name. Integer will get frame by index. WebElement will get by web element relation. 43 | * @return Nearsoft\SeleniumClient\WebDriver The current webdriver 44 | */ 45 | public function frame($identifier) 46 | { 47 | $idParam = null; 48 | $type = gettype($identifier); 49 | if($type == 'string' || $type == 'integer'){ 50 | $idParam = $identifier; 51 | } 52 | elseif($type == 'object' && $identifier instanceof WebElement){ 53 | $idParam = array('ELEMENT' => $identifier->getElementId()); 54 | } 55 | 56 | $params = array ('id' => $idParam); 57 | $command = new Commands\Command($this->_driver, 'frame',$params); 58 | $command->execute(); 59 | return $this->_driver; 60 | } 61 | 62 | /** 63 | * Finds the active element on the page and returns it 64 | * @return WebElement 65 | */ 66 | public function activeElement() 67 | { 68 | $command = new Commands\Command($this->_driver, 'active_element'); 69 | $results = $command->execute(); 70 | return new WebElement($this->_driver, $results['value']['ELEMENT']); 71 | } 72 | 73 | /** 74 | * Switches to the currently active modal dialog for this particular driver instance. 75 | * @return Nearsoft\SeleniumClient\Alert 76 | */ 77 | public function alert() 78 | { 79 | return new Alert($this->_driver); 80 | } 81 | 82 | /** 83 | * Opens a new tab for the given URL 84 | * @param string $url The URL to open 85 | * @return string The handle of the previously active window 86 | * @see http://stackoverflow.com/a/9122450/650329 87 | * @throws SeleniumJavaScriptErrorException If unable to open tab 88 | */ 89 | public function newTab($url) 90 | { 91 | $script = "var d=document,a=d.createElement('a');a.target='_blank';a.href='%s';a.innerHTML='.';d.body.appendChild(a);return a"; 92 | $element = $this->_driver->executeScript( sprintf( $script, $url ) ); 93 | 94 | if ( empty( $element ) ) { 95 | throw new Exceptions\JavaScriptError( 'Unable to open tab' ); 96 | } 97 | 98 | $existingHandles = $this->_driver->getWindowHandles(); 99 | $anchor = new WebElement( $this->_driver, $element['ELEMENT'] ); 100 | $anchor->click(); 101 | 102 | $this->_driver->executeScript( 'var d=document,a=arguments[0];a.parentNode.removeChild(a);', array( $element ) ); 103 | $newHandles = array_values( array_diff( $this->_driver->getWindowHandles(), $existingHandles ) ); 104 | $newHandle = $newHandles[0]; 105 | $oldHandle = $this->_driver->getWindowHandle(); 106 | 107 | $this->window( $newHandle ); 108 | 109 | return $oldHandle; 110 | } 111 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Timeouts.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; 25 | } 26 | 27 | /** 28 | * Sets default time for selenium to wait for an element to be present 29 | * @param Integer $milliseconds 30 | */ 31 | public function implicitWait($milliseconds) 32 | { 33 | $params = array ('ms' => $milliseconds ); 34 | $command = new Commands\Command($this->_driver, 'implicit_wait', $params); 35 | $command->execute(); 36 | } 37 | 38 | /** 39 | * Sets page_load timeout 40 | * @param int $milliseconds 41 | */ 42 | public function pageLoadTimeout($milliseconds) 43 | { 44 | $params = array ('type' => 'page load','ms' => $milliseconds ); 45 | $command = new Commands\Command($this->_driver, 'load_timeout', $params); 46 | $command->execute(); 47 | } 48 | 49 | /** 50 | * Set's Async Script timeout 51 | * @param Integer $milliseconds 52 | */ 53 | public function setScriptTimeout($milliseconds) 54 | { 55 | $params = array('ms' => $milliseconds); 56 | $command = new Commands\Command($this->_driver, 'async_script_timeout', $params); 57 | $command->execute(); 58 | } 59 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/WebDriver.php: -------------------------------------------------------------------------------- 1 | _hubUrl = "{$host}:{$port}/wd/hub"; 82 | isset($desiredCapabilities) ? : $desiredCapabilities = new DesiredCapabilities("firefox"); 83 | $this->_httpClient = HttpFactory::getClient($this->_environment); 84 | $this->startSession($desiredCapabilities); 85 | } 86 | 87 | /** 88 | * Enables Navigation's methods be invoked as navigationNameMethod 89 | * Example: navigationRefresh, navigationBack. 90 | * Enables window's methods be invoked 91 | * Example: windowMaximize, windowGetPosition. 92 | * Enables TargetLocator's methods 93 | * Examples: switchToWindow, switchToFrame 94 | * Enables findElement and findElements methods be invoked through method missing. 95 | * The methods should be invoked with the format 'findElementBy'. 96 | * Arguments should match those required by findElement and findElements methods. 97 | * i.e. findElementByCssSelector, findElementByTagName, findElementsByXPath 98 | * @param string $name 99 | * @param array $args 100 | * @return mixed 101 | * @throws \Exception 102 | */ 103 | public function __call( $name, array $args ) 104 | { 105 | if( strpos($name, "navigation") === 0 ) { 106 | $this->callNavigationMethods($name, $args); 107 | return; 108 | } 109 | 110 | else if( strpos($name, "window") === 0 ) { 111 | $values = $this->callWindowMethods($name, $args); 112 | return $values; 113 | } 114 | 115 | else if ( strpos($name, "switchTo") === 0 ) { 116 | $values = $this->callSwitchTo($name, $args); 117 | return $values; 118 | } 119 | 120 | else if( strpos($name, 'findElement') === 0 ){ 121 | return $this->callFindElement($name, $args); 122 | } 123 | 124 | else { 125 | throw new \Exception( 'Invalid magic call: '.$name ); 126 | } 127 | 128 | } 129 | 130 | /** 131 | * Call Navigation Methods 132 | * @param $name 133 | * @param $args 134 | * @throws \Exception 135 | */ 136 | private function callNavigationMethods($name, $args) 137 | { 138 | $method = lcfirst(substr($name, 10)); 139 | switch ( $method ) { 140 | case 'back': 141 | case 'forward': 142 | case 'refresh': 143 | call_user_func( array ($this->_navigate, $method) ); 144 | break; 145 | case 'to': 146 | call_user_func( array ($this->_navigate, $method),$args[0]); 147 | break; 148 | default: throw new \Exception( 'Invalid magic call: '.$name ); 149 | } 150 | return; 151 | } 152 | 153 | /** 154 | * Call Navigation Methods 155 | * @param $name 156 | * @param $args 157 | * @return array 158 | * @throws \Exception 159 | */ 160 | private function callWindowMethods($name, $args) 161 | { 162 | $method = lcfirst(substr($name, 6)); 163 | switch ($method) { 164 | case 'maximize': 165 | case 'getPosition': 166 | case 'getSize': 167 | $values = call_user_func( array($this->manage()->window(), $method)); 168 | break; 169 | case 'setPosition': 170 | case 'setSize': 171 | $values = call_user_func( array($this->manage()->window(),$method),$args[0],$args[1]); 172 | break; 173 | default: throw new \Exception ( 'Invalid magic call: '.$name); 174 | } 175 | return $values; 176 | } 177 | 178 | /** 179 | * Call Target Locator Methods 180 | * @param $name 181 | * @param array $args 182 | * @return mixed 183 | * @throws \Exception 184 | */ 185 | private function callSwitchTo($name, array $args) 186 | { 187 | $method = lcfirst(substr($name, 8)); 188 | switch($method){ 189 | case 'window': 190 | case 'frame': 191 | $values = call_user_func( array($this->switchTo(), $method),$args[0]); 192 | break; 193 | default: throw new \Exception ('Invalid magic call:'. $name); 194 | } 195 | return $values; 196 | } 197 | 198 | /**Call findElement and findElement methods 199 | * @param $name 200 | * @param array $args 201 | * @return mixed 202 | * @throws \Exception 203 | */ 204 | private function callFindElement($name, array $args) 205 | { 206 | $arr = explode( 'By', $name ); 207 | $call = $arr[0]; 208 | $by = count( $arr ) > 1 ? lcfirst( $arr[1] ) : ''; 209 | $valid = false; 210 | 211 | switch ( $call ) { 212 | case 'findElement': 213 | case 'findElements': 214 | if ( method_exists( 'Nearsoft\\SeleniumClient\\By', $by ) ) { 215 | $valid = true; 216 | } 217 | } 218 | 219 | if ( !$valid ) { 220 | throw new \Exception( 'Invalid magic call: '.$name ); 221 | } 222 | 223 | $method = new \ReflectionMethod( 'Nearsoft\\SeleniumClient\\By', $by ); 224 | $byArgs = array_splice( $args, 0, $method->getNumberOfParameters() ); 225 | array_unshift( $args, $method->invokeArgs( null, $byArgs ) ); 226 | $element = call_user_func_array( array( $this, $call ), $args ); 227 | return $element; 228 | } 229 | 230 | /** 231 | * Delegated addCookies to Class Options 232 | * @param $name 233 | * @param $value 234 | * @param null $path 235 | * @param null $domain 236 | * @param null $secure 237 | * @param null $expiry 238 | */ 239 | public function addCookie($name, $value, $path = null, $domain = null, $secure = null, $expiry = null) 240 | { 241 | $this->manage()->addCookie($name, $value, $path, $domain, $secure, $expiry); 242 | } 243 | 244 | /** 245 | * Delegated getCookies to Class Options 246 | * @return Array 247 | */ 248 | public function getCookies() { return $this->manage()->getCookies();} 249 | 250 | /**Delegated method getCookieNamed to Class Options 251 | * @param $cookieName 252 | * @return mixed 253 | */ 254 | public function getCookieNamed($cookieName) { return $this->manage()->getCookieNamed($cookieName); } 255 | 256 | /** 257 | * Delegated method deleteCookieName to Class Options 258 | * @param $cookieName 259 | */ 260 | public function deleteCookieNamed($cookieName) { $this->manage()->deleteCookieNamed($cookieName); } 261 | 262 | 263 | /** 264 | * Delegated method deleteAllCookies to Class Options 265 | */ 266 | public function deleteAllCookies() { $this->manage()->deleteAllCookies(); } 267 | 268 | 269 | /** 270 | * Set whether production or testing mode for library 271 | * @param String $value 272 | */ 273 | public function setEnvironment($value) { $this->_environment = $value; } 274 | 275 | /** 276 | * Get current Selenium environment 277 | * @return String 278 | */ 279 | public function getEnvironment() { return $this->_environment; } 280 | 281 | /** 282 | * Get HttpClient Object 283 | * @return String 284 | */ 285 | public function getHttpClient() { return $this->_httpClient; } 286 | 287 | /** 288 | * Get current Selenium Hub url 289 | * @return String 290 | */ 291 | public function getHubUrl() { return $this->_hubUrl; } 292 | 293 | 294 | /** 295 | * Get Navigation object 296 | * @return Nearsoft\Selenium\Navigation 297 | */ 298 | public function navigate() 299 | { 300 | isset($this->_navigate) ? : $this->setNavigate(new Navigation($this)); 301 | return $this->_navigate; 302 | } 303 | 304 | /**Set Navigation 305 | * @param $navigate 306 | */ 307 | public function setNavigate($navigate) { $this->_navigate = $navigate; } 308 | 309 | /** 310 | * Get assigned session id 311 | * @return Integer 312 | */ 313 | public function getSessionId() { return $this->_sessionId; } 314 | 315 | /** 316 | * Sets default screenshots directory for files to be stored in 317 | * @param String $value 318 | */ 319 | public function setScreenShotsDirectory($value) { $this->_screenshotsDirectory = $value; } 320 | 321 | /** 322 | * Get default screenshots directory 323 | * @return String 324 | */ 325 | public function getScreenShotsDirectory() { return $this->_screenshotsDirectory; } 326 | 327 | 328 | /** 329 | * Gets Options object 330 | * @return Nearsoft\SeleniumClient\Options 331 | */ 332 | public function manage() 333 | { 334 | isset($this->_options) ? : $this->_options = new Options($this); 335 | return $this->_options; 336 | } 337 | 338 | /** 339 | * Creates new target locator to be handled 340 | * @return Nearsoft\SeleniumClient\TargetLocator 341 | */ 342 | public function switchTo() 343 | { 344 | isset($this->_targetLocator) ? : $this->setSwitchTo(new TargetLocator($this)); 345 | return $this->_targetLocator; 346 | } 347 | 348 | /** 349 | * Set Target Locator 350 | * @param $targetLocator 351 | */ 352 | public function setSwitchTo($targetLocator) { $this->_targetLocator = $targetLocator; } 353 | 354 | 355 | 356 | /** 357 | * Starts new Selenium session 358 | * @param DesiredCapabilities $desiredCapabilities 359 | * @throws \Exception 360 | */ 361 | private function startSession(DesiredCapabilities $desiredCapabilities) 362 | { 363 | if($desiredCapabilities->getBrowserName() == null || trim($desiredCapabilities->getBrowserName()) == '') { 364 | throw new \Exception("Can not start session if browser name is not specified"); 365 | } 366 | 367 | $params = array ('desiredCapabilities' => $desiredCapabilities->getCapabilities()); 368 | $command = new Commands\Command($this, 'start_session', $params); 369 | $results = $command->execute(); 370 | $this->_sessionId = $results['sessionId']; 371 | $this->_capabilities = $results['value']; 372 | return $this->_capabilities; 373 | } 374 | 375 | /** 376 | * Gets actual capabilities 377 | * @return Array of actual capabilities 378 | */ 379 | public function getCapabilities() 380 | { 381 | $command = new Commands\Command($this, 'get_capabilities'); 382 | $results = $command->execute(); 383 | return $results['value']; 384 | } 385 | 386 | /** 387 | * Gets information on current selenium sessions 388 | * @return Array of current sessions in hub 389 | */ 390 | public function getCurrentSessions() 391 | { 392 | $command = new Commands\Command($this, 'get_sessions'); 393 | $results = $command->execute(); 394 | return $results['value']; 395 | } 396 | 397 | /** 398 | * Removes current session 399 | */ 400 | public function quit() 401 | { 402 | $command = new Commands\Command($this, 'quit'); 403 | $command->execute(); 404 | } 405 | 406 | /** 407 | * Closes current window 408 | */ 409 | public function close() 410 | { 411 | $command = new Commands\Command($this, 'close_window'); 412 | $command->execute(); 413 | } 414 | 415 | /** 416 | * Navigates to specified url 417 | * @param String $url 418 | */ 419 | public function get($url) 420 | { 421 | $navigate = $this->navigate(); 422 | $navigate->to($url); 423 | } 424 | 425 | /** 426 | * Gets current url 427 | * @return String 428 | */ 429 | public function getCurrentUrl() 430 | { 431 | $command = new Commands\Command($this, 'get_current_url'); 432 | $results = $command->execute(); 433 | return $results['value']; 434 | } 435 | 436 | /** 437 | * Get current server's status 438 | * @return Array 439 | */ 440 | public function status() 441 | { 442 | $command = new Commands\Command($this, 'status'); 443 | $results = $command->execute(); 444 | return $results; 445 | } 446 | 447 | /** 448 | * Gets current page source 449 | * @return String 450 | */ 451 | public function getPageSource() 452 | { 453 | $command = new Commands\Command($this, 'source'); 454 | $results = $command->execute(); 455 | return $results['value']; 456 | } 457 | 458 | /** 459 | * Gets current page title 460 | * @return String 461 | */ 462 | public function getTitle() 463 | { 464 | $command = new Commands\Command($this, 'title'); 465 | $results = $command->execute(); 466 | return $results['value']; 467 | } 468 | 469 | /** 470 | * Takes screenshot of current screen, saves it in specified default directory or as specified in parameter 471 | * @param String $overrideScreenshotsDirectory 472 | * @throws \Exception 473 | * @return string 474 | */ 475 | public function screenshot($overrideScreenshotsDirectory = null) 476 | { 477 | $screenshotsDirectory = null; 478 | if (isset($overrideScreenshotsDirectory)) { $screenshotsDirectory = $overrideScreenshotsDirectory; } 479 | else if (isset($this->_screenshotsDirectory)) { $screenshotsDirectory = $this->_screenshotsDirectory; } 480 | else { throw new \Exception("Must Specify Screenshot Directory"); } 481 | 482 | $command = new Commands\Command($this, 'screenshot'); 483 | 484 | $results = $command->execute(); 485 | 486 | if (isset($results['value']) && trim($results['value']) != "") { 487 | if (!file_exists($screenshotsDirectory . "/" . $this->_sessionId)) { mkdir($screenshotsDirectory . "/" . $this->_sessionId, 0777, true); } 488 | 489 | $fileName = date ("YmdHmsu") . "-" . (count(glob($screenshotsDirectory . "/" . $this->_sessionId . "/*.png")) + 1) .".png"; 490 | 491 | file_put_contents($screenshotsDirectory . "/" . $this->_sessionId . "/" .$fileName, base64_decode($results['value'])); 492 | 493 | return $fileName; 494 | } 495 | 496 | return null; 497 | } 498 | 499 | /** 500 | * Gets an element within current page 501 | * @param By $locator 502 | * @param bool $polling 503 | * @param int $elementId 504 | * @throws Http\SeleniumNoSuchElementException 505 | * @return Nearsoft\SeleniumClient\WebElement 506 | */ 507 | public function findElement(By $locator, $polling = false, $elementId = -1) 508 | { 509 | if (strpos($locator->getStrategy(), 'js selector ') === 0) { 510 | $result = $this->findElements($locator, $polling, $elementId); 511 | if (!$result) { 512 | throw new Exceptions\NoSuchElement(); 513 | } 514 | return $result[0]; 515 | } else { 516 | $params = array ('using' => $locator->getStrategy(), 'value' => $locator->getSelectorValue()); 517 | if ($elementId < 0) { 518 | $command = new Commands\Command($this, 'element', $params); 519 | } 520 | else 521 | { 522 | $command = new Commands\Command($this, 'element_in_element', $params, array('element_id' => $elementId)); 523 | } 524 | $command->setPolling($polling); 525 | $results = $command->execute(); 526 | return new WebElement($this, $results['value']['ELEMENT']); 527 | } 528 | } 529 | 530 | /** 531 | * Gets elements within current page 532 | * @param By $locator 533 | * @param bool $polling 534 | * @param int $elementId 535 | * @throws Exceptions\InvalidSelector 536 | * @return Nearsoft\SeleniumClient\WebElement[] 537 | */ 538 | public function findElements(By $locator, $polling = false, $elementId = -1) 539 | { 540 | if (strpos($locator->getStrategy(), 'js selector ') === 0) { 541 | $function = substr($locator->getStrategy(), 12); 542 | $script = "return typeof window.{$function};"; 543 | $valid = $this->executeScript($script) == 'function'; 544 | $selector = addslashes($locator->getSelectorValue()); 545 | 546 | if (!$valid) { 547 | throw new Exceptions\InvalidSelector('The selectorElement is not defined'); 548 | } 549 | 550 | if ($elementId >= 0) { 551 | // todo refactor child selection strategy to separate classes 552 | if (strpos($function, 'document.') === 0) { 553 | // assume child.$function($selector) 554 | $function = substr($function, 9); 555 | $script = sprintf('return arguments[0].%s("%s")', $function, $selector); 556 | } else { 557 | // assume $function($selector, child) 558 | $script = sprintf('return %s("%s", arguments[0])', $function, $selector); 559 | } 560 | $args = array(array('ELEMENT' => $elementId)); 561 | } else { 562 | $script = sprintf('return %s("%s")', $function, $selector); 563 | $args = array(); 564 | } 565 | 566 | $params = array('script' => $script, 'args' => $args); 567 | $command = new Commands\Command($this, 'execute_script', $params); 568 | $results = $command->execute(); 569 | } else { 570 | $params = array('using' => $locator->getStrategy(), 'value' => $locator->getSelectorValue()); 571 | 572 | if($elementId >= 0) 573 | { 574 | $command = new Commands\Command($this, 'elements_in_element', $params, array('element_id' => $elementId)); 575 | } 576 | else 577 | { 578 | $command = new Commands\Command($this, 'elements', $params); 579 | } 580 | 581 | $results = $command->execute(); 582 | } 583 | 584 | $webElements = array(); 585 | 586 | if (isset($results['value']) && is_array($results['value'])) { 587 | foreach ($results['value'] as $element) { 588 | $webElements[] = new WebElement($this, is_array($element) ? $element['ELEMENT'] : $element); 589 | } 590 | } 591 | 592 | return $webElements ?: null; 593 | } 594 | 595 | /** 596 | * Stops the process until an element is found 597 | * @param By $locator 598 | * @param Integer $timeOutSeconds 599 | * @return Nearsoft\SeleniumClient\WebElement 600 | */ 601 | public function waitForElementUntilIsPresent(By $locator, $timeOutSeconds = 5) 602 | { 603 | $wait = new WebDriverWait($timeOutSeconds); 604 | $dynamicElement = $wait->until($this, "findElement", array($locator, true)); 605 | return $dynamicElement; 606 | } 607 | 608 | /** 609 | * Stops the process until an element is not found 610 | * @param By $locator 611 | * @param Integer $timeOutSeconds 612 | * @return boolean true when element is gone, false if element is still there 613 | */ 614 | public function waitForElementUntilIsNotPresent(By $locator, $timeOutSeconds = 5) 615 | { 616 | for ($second = 0; ; $second++) 617 | { 618 | if ($second >= $timeOutSeconds) return false; 619 | $result = ($this->findElement($locator, true) === null); 620 | if ($result) 621 | { 622 | return true; 623 | } 624 | sleep(1); 625 | } 626 | return false; 627 | } 628 | 629 | /** 630 | * Executes javascript on page 631 | * @param String $script 632 | * @param Boolean $async 633 | * @param Array $args 634 | * @throws \Exception 635 | * @return String 636 | */ 637 | private function executeScriptInternal($script, $async, $args) 638 | { 639 | if (!isset($this->_capabilities['javascriptEnabled']) || trim($this->_capabilities['javascriptEnabled']) != "1" ) { throw new \Exception("You must be using an underlying instance of WebDriver that supports executing javascript"); } 640 | 641 | $params = array ('script' => $script, 'args' => array()); 642 | 643 | foreach ((array)$args as $arg) { 644 | if ($arg instanceof WebElement) { 645 | $arg = array('ELEMENT' => $arg->getElementId()); 646 | } 647 | $params['args'][] = $arg; 648 | } 649 | 650 | if($async === true) 651 | { 652 | $command = new Commands\Command($this, 'execute_async_script', $params); 653 | } 654 | else 655 | { 656 | $command = new Commands\Command($this, 'execute_script', $params); 657 | } 658 | 659 | $results = $command->execute(); 660 | return $results['value']; 661 | } 662 | 663 | /** 664 | * Executes javascript on page 665 | * @param String $script 666 | * @param Array $args 667 | * @return String 668 | */ 669 | public function executeScript($script, $args = null) { return $this->executeScriptInternal($script, false , $args); } 670 | 671 | /** 672 | * Execute async javascript on page 673 | * @param String $script 674 | * @param Array $args 675 | * @return String 676 | */ 677 | public function executeAsyncScript($script, $args = null) { return $this->executeScriptInternal($script, true , $args); } 678 | 679 | /** 680 | * Gets current window's identifier 681 | * @return String 682 | */ 683 | public function getWindowHandle() 684 | { 685 | $command = new Commands\Command($this, 'window_handle'); 686 | $results = $command->execute(); 687 | return $results['value']; 688 | } 689 | 690 | /** 691 | * Gets a list of available windows in current session 692 | * @return Array 693 | */ 694 | public function getWindowHandles() 695 | { 696 | $command = new Commands\Command($this, 'window_handles'); 697 | $results = $command->execute(); 698 | return $results['value']; 699 | } 700 | } 701 | -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/WebDriverWait.php: -------------------------------------------------------------------------------- 1 | _seconds = $seconds; 27 | } 28 | 29 | public function getSeconds() 30 | { 31 | return $this->_seconds; 32 | } 33 | 34 | /** 35 | * Stop current flow until specified condition is completed 36 | * @param SeleniumClient\WebDriver / SeleniumClient\WebElement $seleniumObject 37 | * @param String $method 38 | * @param Array $args 39 | * @throws \Exception 40 | * @throws Nearsoft\SeleniumClient\Exceptions\WebDriverWaitTimeout 41 | * @return mixed 42 | */ 43 | public function until($seleniumObject, $method, array $args) 44 | { 45 | if (!isset($seleniumObject)) { 46 | throw new \Exception("seleniumObject parameter has not been initialized"); 47 | } else if (!isset($method)) { 48 | throw new \Exception("method parameter has not been initialized"); 49 | } 50 | 51 | $seconds = $this->_seconds; 52 | $elapsed = -1; 53 | $start = microtime(true); 54 | 55 | while ($elapsed < $seconds) { 56 | // the amount of time to sleep until the next whole second 57 | if ($elapsed > 0 && ($sleep = 1000000 * (1 - ($elapsed - (int)$elapsed))) > 0) { 58 | usleep($sleep); 59 | } 60 | 61 | try { 62 | $resultObject = call_user_func_array(array ($seleniumObject, $method), $args); 63 | if ($resultObject !== null && $resultObject !== false) { 64 | return $resultObject; 65 | } 66 | } catch (\Exception $ex) { 67 | } 68 | 69 | $elapsed = microtime(true) - $start; 70 | } 71 | 72 | $exMessage = "Timeout for specified condition caused by object of class: " . get_class($seleniumObject) . ", method invoked: " . $method . "."; 73 | 74 | if ($args != null && count($args) > 0) { 75 | $stringArgs = Array(); 76 | foreach ($args as $arg) { 77 | if (is_object($arg) && method_exists($arg, '__toString')) { 78 | $stringArgs[] = $arg; 79 | } else if (is_object($arg) && !method_exists($arg, '__toString')) { 80 | $stringArgs[] = get_class($arg); 81 | } else { 82 | $stringArgs[] = $arg; 83 | } 84 | } 85 | 86 | $exMessage .= " Arguments: <" . implode(">,<", $stringArgs) . ">"; 87 | } 88 | 89 | throw new Exceptions\WebDriverWaitTimeout($exMessage); 90 | } 91 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/WebElement.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; 26 | $this->_elementId = $elementId; 27 | } 28 | 29 | /** 30 | * Enables setAttribute and getAttribute methods be invoked through method missing. 31 | * The methods should be invoked with the format 'set/get'. 32 | * Arguments should match those required by setAttribute and getAttribute methods. 33 | * i.e. setClassName, getInnerHTML, getClassName 34 | * @param string $name 35 | * @param array $args 36 | * @return mixed 37 | * @throws \Exception 38 | */ 39 | public function __call($name, array $args) 40 | { 41 | $whitelist = array('className', 'innerHTML', 'outerHTML', 'text', 'value'); 42 | $method = lcfirst(substr($name, 3)); 43 | $operation = in_array($method, $whitelist) ? substr($name, 0, 3) : ''; 44 | 45 | switch ($operation) { 46 | case 'set': 47 | $this->setAttribute($method, $args[0]); 48 | return ($method != 'outerHTML') ? $this : null; 49 | case 'get': 50 | return $this->getAttribute($method); 51 | break; 52 | default: 53 | throw new \Exception('Invalid magic call'); 54 | } 55 | } 56 | 57 | /** 58 | * Gets element's id 59 | * @return Integer 60 | */ 61 | public function getElementId() { return $this->_elementId; } 62 | 63 | /** 64 | * Send text to element 65 | * @param String $text 66 | */ 67 | public function sendKeys($text) 68 | { 69 | $params = array('value' => $this->getCharArray($text)); 70 | $command = new Commands\Command($this->_driver, 'element_value', $params , array('element_id' => $this->_elementId)); 71 | $command->execute(); 72 | } 73 | 74 | /** 75 | * Returns array of chars from String 76 | * @param String $text 77 | * @return array 78 | */ 79 | private function getCharArray($text) 80 | { 81 | $encoding = \mb_detect_encoding($text); 82 | $len = \mb_strlen($text, $encoding); 83 | $ret = array(); 84 | while($len) { 85 | $ret[] = \mb_substr($text, 0, 1, $encoding); 86 | $text = \mb_substr($text, 1, $len, $encoding); 87 | $len = \mb_strlen($text, $encoding); 88 | } 89 | return $ret; 90 | } 91 | 92 | /** 93 | * Gets element's visible text 94 | * @return String 95 | */ 96 | public function getText() 97 | { 98 | $command = new Commands\Command($this->_driver, 'element_text', null , array('element_id' => $this->_elementId)); 99 | $results = $command->execute(); 100 | return $results['value']; 101 | } 102 | 103 | /** 104 | * Gets element's tag name 105 | * @return String 106 | */ 107 | public function getTagName() 108 | { 109 | $command = new Commands\Command($this->_driver, 'element_tag_name', null , array('element_id' => $this->_elementId)); 110 | $results = $command->execute(); 111 | return $results['value']; 112 | } 113 | 114 | 115 | /** 116 | * Test if two element refer to the same DOM element. 117 | * @param WebElement $webElementCompare 118 | * @return boolean 119 | */ 120 | public function compareToOther(WebElement $webElementCompare) 121 | { 122 | $params = array('element_id' => $this->getElementId(), 'element_id_compare' => $webElementCompare->getElementId()); 123 | $command = new Commands\Command($this->_driver, 'compare_to_other', null , $params); 124 | $results = $command->execute(); 125 | return (trim($results['value']) == "1"); 126 | } 127 | 128 | /** 129 | * Sets element's specified attribute's value 130 | * @param string $attributeName The element's attribute name 131 | * @param string $value The value to set the attribute 132 | * @return $this 133 | */ 134 | public function setAttribute($attributeName, $value) 135 | { 136 | $key = $attributeName == 'text' 137 | ? 'var k=typeof arguments[0].textContent!="undefined"?"textContent":"innerText"' 138 | : sprintf('var k="%s"', addslashes($attributeName)); 139 | 140 | $script = sprintf("{$key};arguments[0][k]='%s';return true", addslashes($value)); 141 | $this->_driver->executeScript( $script, array( array( 'ELEMENT' => $this->_elementId ) ) ); 142 | return $this; 143 | } 144 | 145 | /** 146 | * Gets element's specified attribute's value 147 | * @param String $attributeName 148 | * @return String 149 | */ 150 | public function getAttribute($attributeName) 151 | { 152 | $command = new Commands\Command($this->_driver, 'element_attribute', null , array('element_id' => $this->_elementId, 'attribute_name' => $attributeName)); 153 | $results = $command->execute(); 154 | return $results['value']; 155 | } 156 | 157 | /** 158 | * Gets element's property CSS 159 | * @param string $propertyName 160 | * @return String 161 | */ 162 | public function getCSSProperty ($propertyName) { 163 | $params = array('element_id' => $this->getElementId(), 'property_name' => $propertyName); 164 | $command = new Commands\Command($this->_driver, 'element_property_name', null , $params); 165 | $results = $command->execute(); 166 | return $results['value']; 167 | } 168 | 169 | /** 170 | * Gets whether element is selected 171 | * @return Boolean 172 | */ 173 | public function isSelected() 174 | { 175 | $command = new Commands\Command($this->_driver, 'element_is_selected', null , array('element_id' => $this->_elementId)); 176 | $results = $command->execute(); 177 | return (trim($results['value']) == "1"); 178 | } 179 | 180 | /** 181 | * Gets whether element is displayed 182 | * @return Boolean 183 | */ 184 | public function isDisplayed() 185 | { 186 | $command = new Commands\Command($this->_driver, 'element_is_displayed', null , array('element_id' => $this->_elementId)); 187 | $results = $command->execute(); 188 | return (trim($results['value']) == "1"); 189 | } 190 | 191 | /** 192 | * Gets whether element is enabled 193 | * @return Boolean 194 | */ 195 | public function isEnabled() 196 | { 197 | $command = new Commands\Command($this->_driver, 'element_is_enabled', null , array('element_id' => $this->_elementId)); 198 | $results = $command->execute(); 199 | return (trim($results['value']) == "1"); 200 | } 201 | 202 | /** 203 | * Gets an element's size in pixels 204 | * @return array 205 | */ 206 | public function getElementSize() 207 | { 208 | $command = new Commands\Command($this->_driver, 'element_size', null , array('element_id' => $this->_elementId)); 209 | $results = $command->execute(); 210 | return $results['value']; 211 | } 212 | 213 | /** 214 | * Clear current element's text 215 | */ 216 | public function clear() 217 | { 218 | $command = new Commands\Command($this->_driver, 'clear_element', null , array('element_id' => $this->_elementId)); 219 | $command->execute(); 220 | } 221 | 222 | /** 223 | * Click on element 224 | */ 225 | public function click() 226 | { 227 | $command = new Commands\Command($this->_driver, 'click_element', null , array('element_id' => $this->_elementId)); 228 | $command->execute(); 229 | } 230 | 231 | /** 232 | * Submit form from element 233 | */ 234 | public function submit() 235 | { 236 | $command = new Commands\Command($this->_driver, 'element_submit', null , array('element_id' => $this->_elementId)); 237 | $command->execute(); 238 | } 239 | 240 | /** 241 | * Gets element's description 242 | * @return Array 243 | */ 244 | public function describe() 245 | { 246 | $command = new Commands\Command($this->_driver, 'describe_element', null , array('element_id' => $this->_elementId)); 247 | $results = $command->execute(); 248 | return $results['value']; 249 | } 250 | 251 | /** 252 | * @param string $class 253 | * @return $this 254 | */ 255 | public function addClass($class) 256 | { 257 | $script = sprintf("if(!arguments[0].className.match(/\b{$class}\b/))arguments[0].className+=' {$class}'"); 258 | $this->_driver->executeScript($script, array(array('ELEMENT' => $this->_elementId))); 259 | return $this; 260 | } 261 | 262 | /** 263 | * @param string $class 264 | * @return $this 265 | */ 266 | public function hasClass($class) 267 | { 268 | $script = sprintf("return (arguments[0].className.split(' ').indexOf('{$class}') > 0);"); 269 | $result =$this->_driver->executeScript($script, array(array('ELEMENT' => $this->_elementId))); 270 | return $result; 271 | } 272 | 273 | /** 274 | * @param string $class 275 | * @return $this 276 | */ 277 | public function removeClass($class) 278 | { 279 | $script = sprintf("arguments[0].className = arguments[0].className.replace(/\b{$class}\b/, '').replace(/\s{2,}/, ' ').replace(/^\s+|\s+$/, '')"); 280 | $this->_driver->executeScript($script, array(array('ELEMENT' => $this->_elementId))); 281 | return $this; 282 | } 283 | 284 | /** 285 | * Get element's coordinates 286 | * @return Array 287 | */ 288 | public function getCoordinates() 289 | { 290 | $command = new Commands\Command($this->_driver, 'element_location', null , array('element_id' => $this->_elementId)); 291 | $results = $command->execute(); 292 | return $results['value']; 293 | } 294 | 295 | /** 296 | * Get element's coordinates after scrolling 297 | * @return Array 298 | */ 299 | public function getLocationOnScreenOnceScrolledIntoView() 300 | { 301 | $command = new Commands\Command($this->_driver, 'element_location_view', null , array('element_id' => $this->_elementId)); 302 | $results = $command->execute(); 303 | return $results['value']; 304 | } 305 | 306 | /** 307 | * Find element within current element 308 | * @param By $locator 309 | * @param Boolean $polling 310 | * @return Nearsoft\SeleniumClient\WebElement 311 | */ 312 | public function findElement(By $locator, $polling = false) 313 | { 314 | return $this->_driver->findElement($locator, $polling, $this->_elementId); 315 | } 316 | 317 | /** 318 | * Find elements within current element 319 | * @param By $locator 320 | * @param Boolean $polling 321 | * @return Nearsoft\SeleniumClient\WebElement[] 322 | */ 323 | public function findElements(By $locator, $polling = false) 324 | { 325 | return $this->_driver->findElements($locator, $polling, $this->_elementId); 326 | } 327 | 328 | /** 329 | * Wait for expected element to be present within current element 330 | * @param By $locator 331 | * @param Integer $timeOutSeconds 332 | * @return mixed 333 | */ 334 | public function waitForElementUntilIsPresent(By $locator, $timeOutSeconds = 5) 335 | { 336 | $wait = new WebDriverWait($timeOutSeconds); 337 | $dynamicElement = $wait->until($this, "findElement", array($locator, true)); 338 | return $dynamicElement; 339 | } 340 | 341 | /** 342 | * Wait for current element to be displayed 343 | * @param Integer $timeOutSeconds 344 | * @return Nearsoft\SeleniumClient\WebElement 345 | */ 346 | public function waitForElementUntilIsDisplayed($timeOutSeconds = 5) 347 | { 348 | $wait = new WebDriverWait($timeOutSeconds); 349 | $element = $wait->until($this, "isDisplayed", array()); 350 | return $this; 351 | } 352 | 353 | /** 354 | * Wait for current element to be enabled 355 | * @param Integer $timeOutSeconds 356 | * @return Nearsoft\SeleniumClient\WebElement 357 | */ 358 | public function waitForElementUntilIsEnabled($timeOutSeconds = 5) 359 | { 360 | $wait = new WebDriverWait($timeOutSeconds); 361 | $element = $wait->until($this, "isEnabled", array()); 362 | return $this; 363 | } 364 | 365 | /** 366 | * Wait until current element's text has changed 367 | * @param String $targetText 368 | * @param Integer $timeOutSeconds 369 | * @throws Nearsoft\SeleniumClient\Exceptions\WebDriverWaitTimeout 370 | * @return Nearsoft\SeleniumClient\WebElement 371 | */ 372 | public function waitForElementUntilTextIsChanged($targetText, $timeOutSeconds = 5) 373 | { 374 | $wait = true; 375 | 376 | while ($wait) 377 | { 378 | $currentText = $this->getText(); 379 | 380 | if ($currentText == $targetText) { $wait = false; } 381 | else if ($timeOutSeconds <= 0) { throw new Exceptions\WebDriverWaitTimeout("Timeout for waitForElementUntilTextIsChange." ); } 382 | 383 | sleep(1); 384 | 385 | $timeOutSeconds = $timeOutSeconds - 1; 386 | } 387 | 388 | return $this; 389 | } 390 | 391 | /** 392 | * Wait until current element's text equals specified 393 | * @param By $locator 394 | * @param String $targetText 395 | * @param Integer $timeOutSeconds 396 | * @throws Nearsoft\SeleniumClient\Exceptions\WebDriverWaitTimeout 397 | * @return Nearsoft\SeleniumClient\WebElement 398 | */ 399 | public function waitForElementUntilIsPresentWithSpecificText(By $locator, $targetText, $timeOutSeconds = 5) 400 | { 401 | $dynamicElement = null; 402 | $wait = true; 403 | $attempts = $timeOutSeconds; 404 | 405 | while ($wait) 406 | { 407 | $currentText = null; 408 | 409 | $webDriverWait = new WebDriverWait($timeOutSeconds); 410 | $dynamicElement = $webDriverWait->until($this, "findElement", array($locator, true)); 411 | 412 | try 413 | { 414 | $currentText = $dynamicElement->getText(); 415 | } 416 | catch(SeleniumStaleElementReferenceException $ex) 417 | { 418 | //echo "\nError The Objet Disappear, Wait For Element Until Is Present With Specific Text\n"; 419 | } 420 | 421 | if ($currentText == $targetText) { $wait = false; } 422 | else if ($attempts <= 0) { throw new Exceptions\WebDriverWaitTimeout("Timeout for waitForElementUntilIsPresentAndTextIsChange." ); } 423 | 424 | sleep(1); 425 | 426 | $attempts = $attempts - 1; 427 | } 428 | return $dynamicElement; 429 | } 430 | } -------------------------------------------------------------------------------- /src/Nearsoft/SeleniumClient/Window.php: -------------------------------------------------------------------------------- 1 | _driver = $driver; 25 | } 26 | 27 | /** 28 | * Maximizes current Window 29 | */ 30 | public function maximize() { 31 | $commannd = new Commands\Command($this->_driver, 'window_maximize', null, array('window_handle' => 'current')); 32 | $commannd->execute(); 33 | } 34 | 35 | /** 36 | * Sets current window size 37 | * @param Integer $width 38 | * @param Integer $height 39 | */ 40 | public function setSize($width, $height) 41 | { 42 | $params = array ('width' => $width, 'height' => $height); 43 | $command = new Commands\Command($this->_driver,'set_window_size', $params, array('window_handle' => 'current')); 44 | $command->execute(); 45 | } 46 | 47 | /** 48 | * Gets current window's size 49 | * @return Array 50 | */ 51 | public function getSize() 52 | { 53 | $command = new Commands\Command($this->_driver, 'get_window_size', null, array('window_handle' => 'current')); 54 | $results = $command->execute(); 55 | return $results['value']; 56 | } 57 | 58 | /** 59 | * Sets current window's position 60 | * @param Integer $x 61 | * @param Integer $y 62 | */ 63 | public function setPosition($x, $y) 64 | { 65 | $params = array ('x' => $x, 'y' => $y); 66 | $command = new Commands\Command($this->_driver, 'set_window_position', $params, array('window_handle' => 'current')); 67 | $command->execute(); 68 | } 69 | 70 | /** 71 | * Gets current window's position 72 | * @return Array 73 | */ 74 | public function getPosition() 75 | { 76 | $command = new Commands\Command($this->_driver, 'get_window_position',null, array('window_handle' => 'current')); 77 | $results = $command->execute(); 78 | return $results['value']; 79 | } 80 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/AbstractTest.php: -------------------------------------------------------------------------------- 1 | quit(); 49 | } catch ( Exception $e ) { 50 | } 51 | } 52 | } ); 53 | 54 | $initialized = true; 55 | } 56 | 57 | public function setUp() 58 | { 59 | $this->_url = self::$_config['url']; 60 | $browser = self::$_config['browser']; 61 | 62 | if ( self::$_config['persist'] ) { 63 | if ( !self::$_handle ) { 64 | $capabilities = new DesiredCapabilities( $browser ); 65 | $driver = new WebDriver( $capabilities ); 66 | self::$_driverInstances[] = $driver; 67 | } else { 68 | $driver = end( self::$_driverInstances ); 69 | } 70 | } else { 71 | $capabilities = new DesiredCapabilities( $browser ); 72 | $driver = new WebDriver( $capabilities ); 73 | self::$_driverInstances[] = $driver; 74 | } 75 | 76 | $this->_driver = $driver; 77 | $this->_driver->get($this->_url); 78 | $this->_position = $this->_driver->manage()->window()->getPosition(); 79 | $this->_size = $this->_driver->manage()->window()->getSize(); 80 | self::$_handle = $this->_driver->getWindowHandle(); 81 | } 82 | 83 | public function tearDown() 84 | { 85 | // return if the driver wasn't initialized 86 | if ( !$this->_driver ) { 87 | return; 88 | } 89 | 90 | if ( self::$_config['persist'] ) { 91 | try { 92 | $alert = new Alert($this->_driver); 93 | $alert->dismiss(); 94 | } catch ( Exception $e ) { 95 | } 96 | try { 97 | foreach ( $this->_driver->getWindowHandles() as $handle ) { 98 | // skip the original window 99 | if ( $handle == self::$_handle ) { 100 | continue; 101 | } 102 | // try to close any other windows that were opened 103 | try { 104 | $this->_driver->switchTo()->window($handle); 105 | $this->_driver->close(); 106 | } catch ( Exception $e ) { 107 | } 108 | } 109 | $this->_driver->switchTo()->window( self::$_handle ); 110 | $this->_driver->switchTo()->activeElement(); 111 | } catch ( SeleniumUnknownErrorException $e ) { 112 | // test case may have closed the parent window 113 | self::$_handle = null; 114 | return; 115 | } 116 | $this->_driver->manage()->deleteAllCookies(); 117 | $this->_driver->manage()->timeouts()->implicitWait(0); 118 | $this->_driver->manage()->window()->setPosition($this->_position['x'], $this->_position['y'] ); 119 | $this->_driver->manage()->window()->setSize($this->_size['width'], $this->_size['height'] ); 120 | $this->_driver->manage()->timeouts()->pageLoadTimeout(10000); 121 | } else { 122 | try{ 123 | $this->_driver->quit(); 124 | } catch ( Exception $e ) { 125 | } 126 | } 127 | } 128 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/AlertTest.php: -------------------------------------------------------------------------------- 1 | _alert = new Alert($this->_driver); 16 | } 17 | 18 | public function testGetAlertShouldGetAlertText() 19 | { 20 | $this->_driver->findElement(By::id("btnAlert"))->click(); 21 | $alertText = $this->_alert->getText(); 22 | $this->assertEquals("Here is the alert", $alertText); 23 | } 24 | 25 | public function testGetAlertShouldDismissAlert() 26 | { 27 | $this->_driver->findElement(By::id("btnConfirm"))->click(); 28 | $this->_alert->dismiss(); 29 | $alertText = $this->_alert->getText(); 30 | $this->assertEquals("false", $alertText); 31 | } 32 | 33 | public function testDismissAlertShouldMakeAlertBeClosed() 34 | { 35 | $this->_driver->findElement(By::id("btnAlert"))->click(); 36 | $this->_alert->dismiss(); 37 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\NoAlertOpenError'); 38 | $this->_alert->getText(); 39 | } 40 | 41 | public function testGetAlertShouldAcceptAlert() 42 | { 43 | $this->_driver->findElement(By::id("btnConfirm"))->click(); 44 | $this->_alert->accept(); 45 | $alertText = $this->_alert->getText(); 46 | $this->assertEquals("true", $alertText); 47 | } 48 | 49 | public function testGetAlertShouldSendKeysToAlert() 50 | { 51 | $this->_driver->findElement(By::id("btnPrompt"))->click(); 52 | $this->_alert->sendKeys("alert text"); 53 | $this->_alert->accept(); 54 | $alertText = $this->_alert->getText(); 55 | $this->assertEquals("alert text", $alertText); 56 | } 57 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/DesiredCapabilitiesTest.php: -------------------------------------------------------------------------------- 1 | _capability = new DesiredCapabilities(BrowserType::FIREFOX,'3.6',PlatformType::WINDOWS); 16 | 17 | $this->assertEquals(3, count($this->_capability->getCapabilities())); 18 | 19 | $this->assertEquals(BrowserType::FIREFOX, $this->_capability->getBrowserName()); 20 | $this->assertEquals(BrowserType::FIREFOX, $this->_capability->getCapability(CapabilityType::BROWSER_NAME)); 21 | 22 | $this->assertEquals("3.6", $this->_capability->getVersion()); 23 | $this->assertEquals("3.6", $this->_capability->getCapability(CapabilityType::VERSION)); 24 | 25 | $this->assertEquals(PlatformType::WINDOWS, $this->_capability->getPlatform()); 26 | $this->assertEquals(PlatformType::WINDOWS, $this->_capability->getCapability(CapabilityType::PLATFORM)); 27 | 28 | $this->_capability->setCapability(CapabilityType::ACCEPT_SSL_CERTS,1); 29 | $this->assertEquals(1, $this->_capability->getCapability(CapabilityType::ACCEPT_SSL_CERTS)); 30 | 31 | $this->_capability->setCapability(CapabilityType::JAVASCRIPT_ENABLED,1); 32 | $this->assertEquals(1, $this->_capability->getCapability(CapabilityType::JAVASCRIPT_ENABLED)); 33 | 34 | $this->assertEquals(5, count($this->_capability->getCapabilities())); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/NavigationTest.php: -------------------------------------------------------------------------------- 1 | _url."/formReceptor.php"; 12 | 13 | $navigation =$this->_driver->navigate(); 14 | 15 | $navigation->to($url); 16 | 17 | $this->assertEquals($url, $this->_driver->getCurrentUrl()); 18 | } 19 | 20 | public function testBackShouldBackBrowserHistory() 21 | { 22 | $expectedTitle = $this->_driver->getTitle(); 23 | 24 | $navigation =$this->_driver->navigate(); 25 | 26 | $navigation->to("http://nearsoft.com"); 27 | 28 | $navigation->back(); 29 | 30 | $this->assertEquals($expectedTitle, $this->_driver->getTitle()); 31 | } 32 | 33 | public function testRefreshShouldRefreshPageAndEmptyElement() 34 | { 35 | $webElement = $this->_driver->findElement(By::id("txt1")); 36 | 37 | $webElement->sendKeys("9999"); 38 | 39 | $navigation = $this->_driver->navigate(); 40 | 41 | $navigation->refresh(); 42 | 43 | $webElement = $this->_driver->findElement(By::id("txt1")); 44 | 45 | $this->assertEquals("", $webElement->getAttribute("value")); 46 | } 47 | 48 | public function testForwardShouldGoForwardBrowserHistory() 49 | { 50 | $navigation =$this->_driver->navigate(); 51 | 52 | $navigation->to($this->_url."/formReceptor.php"); 53 | 54 | $expectedTitle = $this->_driver->getTitle(); 55 | 56 | $navigation->back(); 57 | 58 | $navigation->forward(); 59 | 60 | $this->assertEquals($expectedTitle, $this->_driver->getTitle()); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/OptionsTest.php: -------------------------------------------------------------------------------- 1 | _url ); 14 | $host = strpos( $url['host'], '.' ) !== false ? $url['host'] : null; 15 | 16 | $this->_driver->manage()->addCookie("test", "1"); 17 | 18 | $expiry = time() + 604800; 19 | 20 | $this->_driver->manage()->addCookie("test2", "2", "/", $host, false, $expiry); 21 | 22 | $cookies = $this->_driver->manage()->getCookies(); 23 | 24 | $this->assertEquals('test',$cookies[0]->getName()); 25 | $this->assertEquals('1',$cookies[0]->getValue()); 26 | 27 | $this->assertEquals('test2',$cookies[1]->getName()); 28 | $this->assertEquals('2',$cookies[1]->getValue()); 29 | $this->assertEquals('/',$cookies[1]->getPath()); 30 | $this->assertEquals($host,$cookies[1]->getDomain()); 31 | $this->assertEquals(false,$cookies[1]->getSecure()); 32 | $this->assertEquals($expiry,$cookies[1]->getExpiry()); 33 | } 34 | 35 | public function testGetCookies() 36 | { 37 | $this->_driver->manage()->addCookie("test", "1"); 38 | $this->_driver->manage()->addCookie("test2", "2"); 39 | 40 | $this->assertEquals(2, count($this->_driver->manage()->getCookies())); 41 | } 42 | 43 | public function testDeleteCookieNamedShouldDelete() 44 | { 45 | $url = parse_url( $this->_url ); 46 | $host = strpos( $url['host'], '.' ) !== false ? $url['host'] : null; 47 | 48 | $this->_driver->manage()->addCookie("test", "1"); 49 | $this->_driver->manage()->addCookie("test2","2", "/"); 50 | $this->_driver->manage()->addCookie("test3", "3", "/", $host, false, 0); 51 | 52 | $this->assertEquals(3, count($this->_driver->manage()->getCookies())); 53 | $this->_driver->manage()->deleteCookieNamed("test2"); 54 | $this->assertEquals(2, count($this->_driver->manage()->getCookies())); 55 | 56 | $cookie3 = $this->_driver->manage()->getCookieNamed("test3"); 57 | $this->assertEquals("test3", $cookie3->getName()); 58 | $this->_driver->manage()->deleteCookie($cookie3); 59 | $this->assertEquals(1, count($this->_driver->manage()->getCookies())); 60 | 61 | $this->_driver->manage()->deleteAllCookies(); 62 | } 63 | 64 | public function testDeleteAllCookiesCookiesShouldClear() 65 | { 66 | $url = parse_url( $this->_url ); 67 | $host = strpos( $url['host'], '.' ) !== false ? $url['host'] : null; 68 | 69 | $this->_driver->manage()->addCookie("test", "1"); 70 | $this->_driver->manage()->addCookie("test2", "2", "/"); 71 | $this->_driver->manage()->addCookie("test3", "3", "/", $host, false, 0); 72 | 73 | $this->assertEquals(3, count($this->_driver->manage()->getCookies())); 74 | $this->_driver->manage()->deleteAllCookies(); 75 | $this->assertEquals(0, count($this->_driver->manage()->getCookies())); 76 | $this->_driver->manage()->deleteAllCookies(); 77 | } 78 | 79 | public function testSetGetCookiesShouldSetGet() 80 | { 81 | $url = parse_url( $this->_url ); 82 | $host = strpos( $url['host'], '.' ) !== false ? $url['host'] : null; 83 | 84 | $this->_driver->manage()->addCookie("test", "1"); 85 | $this->_driver->manage()->addCookie("test2", "2", "/"); 86 | $this->_driver->manage()->addCookie("test3", "3", "/", $host, false, 0); 87 | 88 | $this->assertTrue(is_array($this->_driver->manage()->getCookies())); 89 | $this->assertEquals(3, count($this->_driver->manage()->getCookies())); 90 | $this->_driver->manage()->deleteAllCookies(); 91 | } 92 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/SelectElementTest.php: -------------------------------------------------------------------------------- 1 | _driver->findElement(By::id("sel1"))); 13 | 14 | $select->selectByValue("2"); 15 | 16 | $this->assertTrue($select->getElement()->findElement(By::xPath("option[@value = 2]"))->isSelected()); 17 | 18 | $select = new SelectElement($this->_driver->findElement(By::id("sel2"))); 19 | 20 | $select->selectByValue("onions"); 21 | 22 | $this->assertTrue($select->getElement()->findElement(By::xPath("option[@value = 'onions']"))->isSelected()); 23 | } 24 | 25 | public function testSelectByPartialTextShouldSelect() 26 | { 27 | $select = new SelectElement($this->_driver->findElement(By::id("sel1"))); 28 | 29 | $select->selectByPartialText("Red"); 30 | 31 | $this->assertTrue($select->getElement()->findElement(By::xPath("option[@value = 2]"))->isSelected()); 32 | 33 | $select = new SelectElement($this->_driver->findElement(By::id("sel2"))); 34 | 35 | $select->selectByPartialText("peppers"); 36 | 37 | $this->assertTrue($select->getElement()->findElement(By::xPath("option[@value = 'greenpeppers']"))->isSelected()); 38 | } 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/TargetLocatorTest.php: -------------------------------------------------------------------------------- 1 | _targetLocator = new TargetLocator($this->_driver); 18 | } 19 | 20 | public function testWindowShouldGetWindowWebElement() 21 | { 22 | $this->_driver->findElement(By::id("btnPopUp1"))->click(); 23 | $webElement = $this->_targetLocator->window("popup1")->waitForElementUntilIsPresent(By::id("txt1")); 24 | $webElement->sendKeys("test window"); 25 | $this->assertEquals("test window", $webElement->getAttribute("value")); 26 | } 27 | 28 | public function testWindowShouldGetWindowWebElementGetBackToParentWindow() 29 | { 30 | $window1Handle = $this->_driver->getWindowHandle(); 31 | 32 | $this->_driver->findElement(By::id("btnPopUp1"))->click(); 33 | $this->_targetLocator->window($window1Handle); 34 | $this->_driver->findElement(By::id("btnPopUp2"))->click(); 35 | 36 | $webElement = $this->_targetLocator->window("popup1")->waitForElementUntilIsPresent(By::id("txt1")); 37 | $webElement->sendKeys("test window 1"); 38 | 39 | $this->assertEquals("test window 1", $webElement->getAttribute("value")); 40 | 41 | $webElement = $this->_targetLocator->window("popup2")->waitForElementUntilIsPresent(By::id("txt1")); 42 | $webElement->sendKeys("test window 2"); 43 | 44 | $this->assertEquals("test window 2", $webElement->getAttribute("value")); 45 | 46 | $webElement = $this->_targetLocator->window($window1Handle)->waitForElementUntilIsPresent(By::id("txt1")); 47 | $webElement->sendKeys("test window default"); 48 | 49 | $this->assertEquals("test window default", $webElement->getAttribute("value")); 50 | 51 | $this->_targetLocator->window("popup1"); 52 | $this->_driver->close(); 53 | 54 | $this->_targetLocator->window("popup2"); 55 | $this->_driver->close(); 56 | 57 | $this->_targetLocator->window($window1Handle); 58 | } 59 | 60 | public function testFrameShouldGetDefaultframe() 61 | { 62 | $webElement = $this->_targetLocator->frame(null)->findElement(By::id("txt1")); 63 | $webElement->sendKeys("test iframe"); 64 | $this->assertEquals("test iframe", $webElement->getAttribute("value")); 65 | } 66 | 67 | public function testFrameShouldGetFrameByIndex() 68 | { 69 | $webElement = $this->_targetLocator->frame(0)->findElement(By::id("txt1")); 70 | $webElement->sendKeys("test iframe"); 71 | 72 | $this->assertEquals("test iframe", $webElement->getAttribute("value")); 73 | 74 | $webElement = $this->_targetLocator->frame(null)->findElement(By::id("txt1")); 75 | $webElement->sendKeys("test iframe default"); 76 | 77 | $this->assertEquals("test iframe default", $webElement->getAttribute("value")); 78 | 79 | $webElement = $this->_targetLocator->frame(1)->findElement(By::id("txt1")); 80 | $webElement->sendKeys("test iframe1"); 81 | 82 | $this->assertEquals("test iframe1", $webElement->getAttribute("value")); 83 | } 84 | 85 | public function testFrameShouldGetFrameByName() 86 | { 87 | $webElement = $this->_targetLocator->frame("iframe1")->findElement(By::id("txt1")); 88 | $webElement->sendKeys("test iframe"); 89 | $this->assertEquals("test iframe", $webElement->getAttribute("value")); 90 | } 91 | 92 | public function testFrameShouldGetFrameByNameShouldGetFrameWebElementGetBackToParentWindow() 93 | { 94 | $webElement = $this->_targetLocator->frame("iframe1")->findElement(By::id("txt1")); 95 | $webElement->sendKeys("test iframe"); 96 | 97 | $this->assertEquals("test iframe", $webElement->getAttribute("value")); 98 | 99 | $webElement = $this->_targetLocator->frame(null)->findElement(By::id("txt1")); 100 | $webElement->sendKeys("test iframe default"); 101 | 102 | $this->assertEquals("test iframe default", $webElement->getAttribute("value")); 103 | 104 | $webElement = $this->_targetLocator->frame("iframe2")->findElement(By::id("txt1")); 105 | $webElement->sendKeys("test iframe2"); 106 | 107 | $this->assertEquals("test iframe2", $webElement->getAttribute("value")); 108 | } 109 | 110 | public function testFrameShouldGetFrameByWebElementShouldGetFrameWebElement() 111 | { 112 | $webElement = $this->_driver->findElement(By::id("iframe1")); 113 | $webElement = $this->_targetLocator->frame($webElement)->findElement(By::id("txt1")); 114 | $webElement->sendKeys("test iframe"); 115 | $this->assertEquals("test iframe", $webElement->getAttribute("value")); 116 | } 117 | 118 | public function testFrameShouldGetFrameByWebElementShouldGetFrameWebElementGetBackToParentWindow() 119 | { 120 | $webElement = $this->_driver->findElement(By::id("iframe1")); 121 | $webElement = $this->_targetLocator->frame($webElement)->findElement(By::id("txt1")); 122 | $webElement->sendKeys("test iframe"); 123 | 124 | $this->assertEquals("test iframe", $webElement->getAttribute("value")); 125 | 126 | $webElement = $this->_targetLocator->frame(null)->findElement(By::id("txt1")); 127 | $webElement->sendKeys("test iframe default"); 128 | 129 | $this->assertEquals("test iframe default", $webElement->getAttribute("value")); 130 | 131 | $webElement = $this->_driver->findElement(By::id("iframe2")); 132 | $webElement = $this->_targetLocator->frame($webElement)->findElement(By::id("txt1")); 133 | $webElement->sendKeys("test iframe2"); 134 | 135 | $this->assertEquals("test iframe2", $webElement->getAttribute("value")); 136 | } 137 | 138 | public function testActiveElementShouldGetActiveElement() 139 | { 140 | $this->_driver->findElement(By::id("txt1"))->sendKeys("test"); 141 | $webElement = $this->_targetLocator->activeElement(); 142 | $this->assertInstanceOf('Nearsoft\SeleniumClient\WebElement', $webElement); 143 | $this->assertEquals("test", $webElement->getAttribute("value")); 144 | } 145 | 146 | public function testAlertShouldGetAlertInstance() 147 | { 148 | $this->_driver->findElement(By::id("btnAlert"))->click(); 149 | $alert = $this->_targetLocator->alert(); 150 | $this->assertTrue($alert instanceof Alert); 151 | } 152 | 153 | public function testNewTabShouldGetNewWindow() 154 | { 155 | $oldHandle1 = $this->_driver->getWindowHandle(); 156 | $numHandles = count($this->_driver->getWindowHandles()); 157 | $oldHandle2 = $this->_targetLocator->newTab($this->_url); 158 | $this->assertEquals($oldHandle1, $oldHandle2); 159 | $newHandle = $this->_driver->getWindowHandle(); 160 | $this->assertNotEquals($oldHandle1, $newHandle); 161 | $this->assertEquals($numHandles + 1, count($this->_driver->getWindowHandles())); 162 | } 163 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/TimeoutTest.php: -------------------------------------------------------------------------------- 1 | _driver->findElement(By::id("btnAppendDiv"))->click(); 12 | 13 | $timeouts = $this->_driver->manage()->timeouts(); 14 | 15 | $timeouts->implicitWait(5000); 16 | 17 | $webElement = $this->_driver->findElement(By::id("dDiv1-0")); // This takes 5 seconds to be present 18 | 19 | $this->assertInstanceOf('Nearsoft\SeleniumClient\WebElement', $webElement); 20 | } 21 | 22 | public function testPageLoadTimeout() 23 | { 24 | $timeOuts = $this->_driver->manage()->timeouts(); 25 | 26 | $timeOuts->pageLoadTimeout(1); 27 | 28 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\ScriptTimeout'); 29 | 30 | $this->_driver->get($this->_url."/formReceptor.php"); 31 | } 32 | 33 | public function testSetScriptTimeout() 34 | { 35 | $timeouts = $this->_driver->manage()->timeouts(); 36 | 37 | $timeouts->setScriptTimeout(1); 38 | 39 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\ScriptTimeout'); 40 | 41 | $this->_driver->executeAsyncScript("setTimeout('arguments[0]()',5000);"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/WebDriverTest.php: -------------------------------------------------------------------------------- 1 | _navigationMock = $this->getMock('Navigation', array('refresh','to','back','forward')); 23 | $this->_windowMock = $this->getMock('Window', array('maximize','close' ,'setSize', 'getSize', 'setPosition', 'getPosition')); 24 | $this->_targetLocatorMock = $this->getMock('TargetLocator', array('window','frame')); 25 | $this->_originalNavigate = $this->_driver->navigate(); 26 | $this->_originalWindow = $this->_driver->manage()->window(); 27 | $this->_originalTargetLocator = $this->_driver->switchTo(); 28 | } 29 | 30 | 31 | public function testScreenshotsShouldCreateFile() 32 | { 33 | $screenShotsDirectory = "/tmp/selenium screenshots"; 34 | 35 | if (!file_exists($screenShotsDirectory)) { mkdir($screenShotsDirectory, 0755, true); } 36 | $this->_driver->setScreenShotsDirectory($screenShotsDirectory); 37 | 38 | $generatedFileNames[] = $this->_driver->screenshot(); 39 | 40 | $webElementOnPopUp1 = $this->_driver->findElement(By::id("txt1")); 41 | $webElementOnPopUp1->sendKeys("test for screenshot"); 42 | 43 | $generatedFileNames[] = $this->_driver->screenshot(); 44 | 45 | $webElementOnPopUp1 = $this->_driver->findElement(By::id("txt2")); 46 | $webElementOnPopUp1->sendKeys("test for screenshot 2"); 47 | 48 | $generatedFileNames[] = $this->_driver->screenshot(); 49 | 50 | $this->assertEquals(3, count($generatedFileNames)); 51 | 52 | foreach($generatedFileNames as $generatedFileName) 53 | { 54 | $this->assertTrue(file_exists($this->_driver->getScreenShotsDirectory() . "/". $this->_driver->getSessionId() . "/". $generatedFileName)); 55 | } 56 | } 57 | 58 | public function testScreenshotsShouldCreateFile2() 59 | { 60 | $screenShotsDirectory = "/tmp/selenium screenshots"; 61 | 62 | if (!file_exists($screenShotsDirectory)) { 63 | mkdir($screenShotsDirectory, 0755, true); 64 | } 65 | $this->_driver->setScreenShotsDirectory($screenShotsDirectory); 66 | 67 | $generatedFileNames[] = $this->_driver->screenshot($screenShotsDirectory); 68 | 69 | $webElementOnPopUp1 = $this->_driver->findElement(By::id("txt1")); 70 | $webElementOnPopUp1->sendKeys("test for screenshot"); 71 | 72 | $generatedFileNames[] = $this->_driver->screenshot($screenShotsDirectory); 73 | 74 | $webElementOnPopUp1 = $this->_driver->findElement(By::id("txt2")); 75 | $webElementOnPopUp1->sendKeys("test for screenshot 2"); 76 | 77 | $generatedFileNames[] = $this->_driver->screenshot($screenShotsDirectory); 78 | 79 | $this->assertEquals(3, count($generatedFileNames)); 80 | 81 | foreach($generatedFileNames as $generatedFileName) 82 | { 83 | $this->assertTrue(file_exists($this->_driver->getScreenShotsDirectory() . "/". $this->_driver->getSessionId() . "/". $generatedFileName)); 84 | } 85 | } 86 | 87 | public function testGetWindowHandlesholdGetHandle() 88 | { 89 | $this->assertTrue(is_string($this->_driver->getWindowHandle())); 90 | } 91 | 92 | public function testGetWindowHandlesSholdGet3Handles() 93 | { 94 | $this->_driver->findElement(By::id("btnPopUp1"))->click(); 95 | $this->_driver->findElement(By::id("btnPopUp2"))->click(); 96 | 97 | $this->assertEquals(3, count($this->_driver->getWindowHandles())); 98 | } 99 | 100 | public function testGetCurrentSessionsShouldGetArray() 101 | { 102 | $sessions = $this->_driver->getCurrentSessions(); 103 | $this->assertTrue(is_array($sessions)); 104 | $this->assertTrue(count($sessions) > 0); 105 | } 106 | 107 | public function testNavigateShouldGetNavigationInstance() 108 | { 109 | $this->assertInstanceOf('Nearsoft\SeleniumClient\Navigation', $this->_driver->navigate()); 110 | } 111 | 112 | public function testSwitchToShouldGetTargetLocatorInstance() 113 | { 114 | $this->assertInstanceOf('Nearsoft\SeleniumClient\TargetLocator', $this->_driver->switchTo()); 115 | } 116 | 117 | public function testManageShouldGetOptionsInstance() 118 | { 119 | $this->assertInstanceOf('Nearsoft\SeleniumClient\Options', $this->_driver->manage()); 120 | } 121 | 122 | public function testExecuteScriptShouldSetInputText() 123 | { 124 | $this->_driver->executeScript("document.getElementById('txt2').value='TEST!';"); 125 | $webElement = $this->_driver->findElement(By::id("txt2")); 126 | 127 | $this->assertEquals("TEST!", $webElement->getAttribute("value")); 128 | } 129 | 130 | public function testExecuteScriptShouldGetPageTitle() 131 | { 132 | $this->assertEquals("Nearsoft SeleniumClient SandBox", $this->_driver->executeScript("return document.title")); 133 | } 134 | 135 | public function testExecuteScriptShouldSetInputTextUsingArguments() 136 | { 137 | $this->_driver->executeScript("document.getElementById(arguments[0]).value=arguments[1];", array("txt1", "TEST2!")); 138 | 139 | $webElement = $this->_driver->findElement(By::id("txt1")); 140 | $this->assertEquals("TEST2!", $webElement->getAttribute("value")); 141 | } 142 | 143 | public function testExecuteAsyncScriptShouldManipulateDom() 144 | { 145 | //https://code.google.com/p/selenium/wiki/JsonWireProtocol#POST_/session/:sessionId/execute_async 146 | //There is an implicit last argument being sent which MUST be invoked as callback by the end of the function 147 | $this->_driver->executeAsyncScript("console.log(arguments);document.getElementById('txt1').value='finished';arguments[arguments.length - 1]();", array('foo','var')); 148 | $this->assertEquals('finished',$this->_driver->findElement(By::id("txt1"))->getValue()); 149 | } 150 | 151 | public function testGetCapabilitiesShouldGetInfo() { 152 | $this->assertTrue(is_array($this->_driver->getCapabilities())); 153 | } 154 | 155 | public function testStartSessionShouldHaveSessionId() 156 | { 157 | $this->assertNotEquals(null, $this->_driver->getSessionId()); 158 | // SessionID appears to be a 36 character GUID 159 | $this->assertGreaterThan(0, strlen($this->_driver->getSessionId())); 160 | } 161 | 162 | public function testGetShouldNavigateToUrl() 163 | { 164 | $url = $this->_url."/formReceptor.php"; 165 | $this->_driver->get($url); 166 | $this->assertEquals($url, $this->_driver->getCurrentUrl()); 167 | } 168 | 169 | public function testGetCurrentUrl() 170 | { 171 | $this->assertEquals($this->_url, $this->_driver->getCurrentUrl()); 172 | } 173 | 174 | public function testStatus() 175 | { 176 | $status = $this->_driver->status(); 177 | $expectedKeys = array('status','sessionId','value','state','class','hCode'); 178 | $this->assertEquals(asort(array_keys($status)),asort($expectedKeys)); 179 | } 180 | 181 | public function testGetPageSource() 182 | { 183 | $this->assertTrue((bool)strpos($this->_driver->getPageSource(), 'Form elements')); 184 | } 185 | 186 | public function testGetTitle() 187 | { 188 | $this->assertTrue(is_string($this->_driver->getTitle())); 189 | $this->assertTrue(count($this->_driver->getTitle())>0); 190 | } 191 | 192 | public function testFindElement() 193 | { 194 | $webElement = $this->_driver->findElement(By::id("txt1")); 195 | $this->assertInstanceOf('Nearsoft\SeleniumClient\WebElement',$webElement); 196 | 197 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\NoSuchElement'); 198 | $webElement = $this->_driver->findElement(By::id("NOTEXISTING")); 199 | } 200 | 201 | public function testFindElementInElement() 202 | { 203 | $parentElement = $this->_driver->findElement(By::id("sel1")); 204 | $childElement = $this->_driver->findElement(By::xPath(".//option[@value = '3']"), false, $parentElement->getElementId()); 205 | $this->assertInstanceOf('Nearsoft\SeleniumClient\WebElement',$childElement); 206 | } 207 | 208 | public function testFindElementByJsSelector() 209 | { 210 | $input = $this->_driver->findElement(By::jsSelector('input','document.querySelectorAll')); 211 | $this->assertInstanceOf('Nearsoft\SeleniumClient\WebElement',$input); 212 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\InvalidSelector'); 213 | $this->_driver->findElement(By::jsSelector('input')); 214 | } 215 | 216 | public function testFindElements() 217 | { 218 | $webElements = $this->_driver->findElements(By::tagName("input")); 219 | 220 | foreach($webElements as $webElement) { $this->assertTrue($webElement instanceof WebElement); } 221 | 222 | $this->assertTrue(is_array($webElements)); 223 | $this->assertTrue(count($webElements)>0); 224 | } 225 | 226 | public function testFindElementsInElement() 227 | { 228 | $parentElement = $this->_driver->findElement(By::id("sel1")); 229 | $childElements = $this->_driver->findElements(By::xPath(".//option"), false, $parentElement->getElementId()); 230 | $this->assertInternalType('array',$childElements); 231 | $this->assertInstanceOf('Nearsoft\SeleniumClient\WebElement',$childElements[1]); 232 | } 233 | 234 | public function testFindElementsByJsSelector() 235 | { 236 | $inputs = $this->_driver->findElements(By::jsSelector('input','document.querySelectorAll')); 237 | $self = $this; 238 | array_walk($inputs, function($input) use ($self) { 239 | $self->assertTrue($input instanceof WebElement); 240 | }); 241 | $this->assertGreaterThan(0, count($inputs)); 242 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\InvalidSelector'); 243 | $this->_driver->findElements(By::jsSelector('input')); 244 | } 245 | 246 | public function testWaitForElementUntilIsPresent() 247 | { 248 | $this->_driver->findElement(By::id("btnAppendDiv"))->click(); 249 | 250 | $this->_driver->waitForElementUntilIsPresent(By::id("dDiv1-0"),10); 251 | 252 | $this->assertEquals("Some content", $this->_driver->findElement(By::id("dDiv1-0"))->getText()); 253 | } 254 | 255 | public function testWaitForElementUntilIsNotPresent() 256 | { 257 | $webElement = $this->_driver->findElement(By::id("btnHideThis")); 258 | 259 | $webElement->click(); 260 | 261 | $this->_driver->waitForElementUntilIsNotPresent(By::id("btnHideThis"),10); 262 | 263 | $this->assertFalse($webElement->isDisplayed()); 264 | } 265 | 266 | public function testQuit() 267 | { 268 | $anotherDriver = new Nearsoft\SeleniumClient\WebDriver(); 269 | $sessionId = $anotherDriver->getSessionId(); 270 | 271 | $containsSession = function($var) use ($sessionId) 272 | { 273 | return ($var['id'] == $sessionId); 274 | }; 275 | 276 | $anotherDriver->quit(); 277 | $sessions = $this->_driver->getCurrentSessions(); 278 | $matchedSessions = array_filter($sessions,$containsSession); 279 | $this->assertEquals(0, count($matchedSessions)); 280 | } 281 | 282 | public function testCloseWindowShouldClose() 283 | { 284 | $this->_driver->findElement(By::id("btnPopUp1"))->click(); 285 | $this->_driver->switchTo()->window("popup1"); 286 | $this->_driver->close(); 287 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\NoSuchWindow'); 288 | $this->_driver->getCurrentUrl(); 289 | } 290 | 291 | public function testMagicNavigationBackShouldCallMethodBack() 292 | { 293 | $this->_navigationMock->expects($this->exactly(1)) 294 | ->method('back'); 295 | 296 | $this->_driver->setNavigate($this->_navigationMock); 297 | 298 | $this->_driver->navigationBack(); 299 | 300 | $this->_driver->setNavigate($this->_originalNavigate); 301 | } 302 | 303 | public function testMagicNavigationForwardShouldCallMethodForward() 304 | { 305 | $this->_navigationMock->expects($this->exactly(1)) 306 | ->method('forward'); 307 | 308 | $this->_driver->setNavigate($this->_navigationMock); 309 | 310 | $this->_driver->navigationForward(); 311 | 312 | $this->_driver->setNavigate($this->_originalNavigate); 313 | } 314 | 315 | public function testMagicNavigationRefreshShouldCallMethodRefresh() 316 | { 317 | $this->_navigationMock->expects($this->exactly(1)) 318 | ->method('refresh'); 319 | 320 | $this->_driver->setNavigate($this->_navigationMock); 321 | 322 | $this->_driver->navigationRefresh(); 323 | 324 | $this->_driver->setNavigate($this->_originalNavigate); 325 | } 326 | 327 | public function testMagicNavigationToShouldCallMethodTo() 328 | { 329 | $this->_navigationMock->expects($this->exactly(1)) 330 | ->method('to') 331 | ->with($this->equalTo('google.com')); 332 | 333 | $this->_driver->setNavigate($this->_navigationMock); 334 | 335 | $this->_driver->navigationTo('google.com'); 336 | 337 | $this->_driver->setNavigate($this->_originalNavigate); 338 | } 339 | 340 | public function testMagicWindowMaximizeShouldCallMethodMaximize() 341 | { 342 | $this->_windowMock->expects($this->exactly(1)) 343 | ->method('maximize'); 344 | 345 | $this->_driver->manage()->setWindow($this->_windowMock); 346 | 347 | $this->_driver->windowMaximize(); 348 | 349 | $this->_driver->manage()->setWindow($this->_originalWindow); 350 | } 351 | 352 | public function testMagicWindowGetPositionShouldCallMethodGetPosition() 353 | { 354 | $this->_windowMock->expects($this->exactly(1)) 355 | ->method('getPosition'); 356 | 357 | $this->_driver->manage()->setWindow($this->_windowMock); 358 | 359 | $this->_driver->windowGetPosition(); 360 | 361 | $this->_driver->manage()->setWindow($this->_originalWindow); 362 | } 363 | 364 | public function testMagicWindowGetSizeShouldCallMethodGetSize() 365 | { 366 | $this->_windowMock->expects($this->exactly(1)) 367 | ->method('getSize'); 368 | 369 | $this->_driver->manage()->setWindow($this->_windowMock); 370 | 371 | $this->_driver->windowGetSize(); 372 | 373 | $this->_driver->manage()->setWindow($this->_originalWindow); 374 | 375 | } 376 | 377 | public function testMagicWindowSetSizeShouldCallMethodSetSize() 378 | { 379 | $this->_windowMock->expects($this->exactly(1)) 380 | ->method('setSize') 381 | ->with($this->equalTo(100), $this->equalTo(100)); 382 | 383 | $this->_driver->manage()->setWindow($this->_windowMock); 384 | 385 | $this->_driver->windowSetSize(100,100); 386 | 387 | $this->_driver->manage()->setWindow($this->_originalWindow); 388 | 389 | } 390 | 391 | public function testMagicWindowSetPositionShouldCallMethodSetPosition() 392 | { 393 | $this->_windowMock->expects($this->exactly(1)) 394 | ->method('setPosition') 395 | ->with($this->equalTo(200), $this->equalTo(300)); 396 | 397 | $this->_driver->manage()->setWindow($this->_windowMock); 398 | 399 | $this->_driver->windowSetPosition(200,300); 400 | 401 | $this->_driver->manage()->setWindow($this->_originalWindow); 402 | 403 | } 404 | 405 | public function testMagicSwitchToWindowShouldCallMethodWindow() 406 | { 407 | $this->_targetLocatorMock->expects($this->exactly(1)) 408 | ->method('window'); 409 | 410 | $this->_driver->setSwitchTo($this->_targetLocatorMock); 411 | 412 | $this->_driver->switchToWindow('popup1'); 413 | 414 | $this->_driver->setSwitchTo($this->_originalTargetLocator); 415 | 416 | } 417 | 418 | public function testMagicSwitchToFrameShouldCallMethodFrame() 419 | { 420 | $this->_targetLocatorMock->expects($this->exactly(1)) 421 | ->method('frame'); 422 | 423 | $this->_driver->setSwitchTo($this->_targetLocatorMock); 424 | 425 | $this->_driver->switchToFrame(null); 426 | 427 | $this->_driver->setSwitchTo($this->_originalTargetLocator); 428 | } 429 | 430 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/WebDriverWaitTest.php: -------------------------------------------------------------------------------- 1 | _driver->findElement(By::id("btnAppendDiv"))->click(); 13 | $wait = new WebDriverWait(8); 14 | $label = $wait->until($this->_driver,"findElement",array(By::id("dDiv1-0"),true)); 15 | $this->assertEquals("Some content",$label->getText()); 16 | } 17 | 18 | public function testUntilShouldWaitShouldThrowException() 19 | { 20 | 21 | $this->setExpectedException('Nearsoft\SeleniumClient\Exceptions\WebDriverWaitTimeout'); 22 | $this->_driver->findElement(By::id("btnAppendDiv"))->click(); 23 | $wait = new WebDriverWait(3); 24 | $label = $wait->until($this->_driver,"findElement",array(By::id("dDiv1-0"),true)); 25 | } 26 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/WebElementTest.php: -------------------------------------------------------------------------------- 1 | _driver->findElement(By::id("txt1")); 13 | $this->assertTrue(is_numeric($element->getElementId())); 14 | } 15 | 16 | public function testGetCoordinatesInViewShouldGetLocationOnScreenOnceScrolledIntoView() 17 | { 18 | $element = $this->_driver->findElement(By::id("txt1")); 19 | $coordinates = $element->getLocationOnScreenOnceScrolledIntoView(); 20 | 21 | $this->assertTrue(is_numeric($coordinates["x"]) ); 22 | $this->assertTrue(is_numeric($coordinates["y"]) ); 23 | } 24 | 25 | public function testGetCoordinatesShouldGetCoordinates() 26 | { 27 | $element = $this->_driver->findElement(By::id("txt1")); 28 | $coordinates = $element->getCoordinates(); 29 | 30 | $this->assertTrue(is_numeric($coordinates["x"]) ); 31 | $this->assertTrue(is_numeric($coordinates["y"]) ); 32 | } 33 | 34 | public function testGetCSSPropertyShouldReturnValueOfCssProperty() { 35 | $element = $this->_driver->findElement(By::xPath('/html/body/table/tbody/tr/td[2]')); 36 | $property = $element->getCSSProperty('vertical-align'); 37 | $this->assertEquals('top', $property); 38 | } 39 | 40 | public function testIsDisplayedShouldDetermineIfDisplayed() 41 | { 42 | $button1 = $this->_driver->findElement(By::id("btnNoAction")); 43 | $this->assertEquals( true, $button1->isDisplayed()); 44 | $this->_driver->executeScript("document.getElementById('btnNoAction').style.display = 'none';"); 45 | $this->assertEquals( false, $button1->isDisplayed()); 46 | } 47 | 48 | public function testGetAttributeShouldGetData() 49 | { 50 | $chk = $this->_driver->findElement(By::id("chk3")); 51 | $this->assertEquals( "chk3",strtolower($chk->getAttribute("name"))); 52 | } 53 | 54 | public function testSetAttributeShouldSet() 55 | { 56 | $webElement = $this->_driver->findElement(By::id("txt1")); 57 | $webElement->setAttribute('value','123456'); 58 | $webElement->setAttribute('type','hidden'); 59 | $this->assertEquals("123456", $webElement->getAttribute("value")); 60 | $this->assertEquals("hidden", $webElement->getAttribute("type")); 61 | } 62 | 63 | public function testIsEnabledShouldDetermineIfEnabled() 64 | { 65 | $button1 = $this->_driver->findElement(By::id("btnNoAction")); 66 | $this->assertEquals( true, $button1->isEnabled()); 67 | 68 | $this->_driver->executeScript("document.getElementById('btnNoAction').disabled = true;"); 69 | 70 | $this->assertEquals( false, $button1->isEnabled()); 71 | } 72 | 73 | public function testIsSelectedShouldDetermineIfSelected() 74 | { 75 | $selectBox = $this->_driver->findElement(By::id("sel1")); 76 | 77 | $selectBoxOption = $selectBox->findElement(By::xPath("/html/body/table/tbody/tr/td/fieldset/form/p[3]/select/option[1]")); 78 | 79 | $this->assertEquals( false, $selectBoxOption->isSelected()); 80 | 81 | $selectBoxOption->click(); 82 | 83 | $this->assertEquals( true, $selectBoxOption->isSelected()); 84 | } 85 | 86 | public function testElementSizeShouldGetElementSizeInPixels () 87 | { 88 | $webElement = $this->_driver->findElement(By::id("txtArea1")); 89 | $dimensions = $webElement->getElementSize(); 90 | 91 | $this->assertTrue(is_numeric($dimensions['width'])); 92 | $this->assertTrue(is_numeric($dimensions['height'])); 93 | } 94 | 95 | public function testClassMethodsAffectElementClassName() 96 | { 97 | $element = $this->_driver->findElement(By::id("sel1")); 98 | $this->assertEmpty($element->getClassName()); 99 | $element->setClassName("select x-small"); 100 | $this->assertEquals("select x-small", $element->getClassName()); 101 | $this->assertContains('class="select x-small"', $element->getOuterHTML()); 102 | // test method chaining 103 | $element->addClass("foo")->addClass("bar"); 104 | $this->assertEquals("select x-small foo bar", $element->getClassName()); 105 | // we shouldn't be able to add a duplicate class 106 | $element->addClass("foo"); 107 | $this->assertEquals("select x-small foo bar", $element->getClassName()); 108 | $element->removeClass("foo"); 109 | $this->assertEquals("select x-small bar", $element->getClassName()); 110 | } 111 | 112 | public function testHasClass() 113 | { 114 | $element = $this->_driver->findElement(By::id("txt1")); 115 | $element->addClass("foo"); 116 | $this->assertTrue($element->hasClass("foo")); 117 | $this->assertFalse($element->hasClass("someotherclass")); 118 | } 119 | 120 | public function testClearShouldSetValueEmpty() 121 | { 122 | $webElement = $this->_driver->findElement(By::id("txt1")); 123 | 124 | $webElement->sendKeys("test text"); 125 | 126 | $webElement->clear(); 127 | 128 | $this->assertEquals("", trim($webElement->getAttribute("value"))); 129 | } 130 | 131 | public function testGetInnerHTMLShouldGetInnerHTML() 132 | { 133 | $selectBox = $this->_driver->findElement(By::id("sel1")); 134 | $text = $selectBox->getInnerHTML(); 135 | $this->assertContains('assertStringEndsNotWith('', $text); 137 | } 138 | 139 | public function testGetOuterHTMLShouldGetOuterHTML() 140 | { 141 | $selectBox = $this->_driver->findElement(By::id("sel1")); 142 | $text = $selectBox->getOuterHTML(); 143 | $this->assertContains('assertStringEndsWith('', $text); 145 | } 146 | 147 | public function testGetTagNameShouldGetTagName() 148 | { 149 | $webElement = $this->_driver->findElement(By::id("txt1")); 150 | $this->assertEquals("input",strtolower($webElement->getTagName())); 151 | } 152 | 153 | public function testCompareToShouldCompareElementWithID() 154 | { 155 | $webElement1 = $this->_driver->findElement(By::id("txt1")); 156 | $webElementOther = $this->_driver->findElement(By::xPath("//*[@id='txt1']")); 157 | $webElement2 = $this->_driver->findElement(By::id("txt2")); 158 | 159 | 160 | $this->assertFalse($webElement1->compareToOther($webElement2)); 161 | $this->assertTrue ($webElement1->compareToOther($webElementOther)); 162 | } 163 | 164 | 165 | 166 | public function testDescribeShouldGetElementId() 167 | { 168 | $webElement = $this->_driver->findElement(By::id("btnSubmit")); 169 | $expectedKeys = array('id','enabled','selected','text','displayed','tagName','class','hCode'); 170 | $descriptionData = $webElement->describe(); 171 | $this->assertTrue(array_intersect(array_keys($descriptionData),$expectedKeys) === $expectedKeys); 172 | } 173 | 174 | public function testFindElementByJsSelectorShouldGetChildElement() 175 | { 176 | $selectBox = $this->_driver->findElement(By::id("sel1")); 177 | $option = $selectBox->findElement(By::jsSelector('option[selected="selected"]', 'document.querySelector')); 178 | $this->assertEquals('Orange', $option->getText()); 179 | } 180 | 181 | public function testFindElementShouldGetFoundElementText() 182 | { 183 | 184 | $selectBox = $this->_driver->findElement(By::id("sel1")); 185 | 186 | $selectBoxOption = $selectBox->findElement(By::xPath("/html/body/table/tbody/tr/td/fieldset/form/p[3]/select/option[2]")); 187 | 188 | $this->assertTrue($selectBoxOption instanceof WebElement); 189 | 190 | $this->assertEquals( "Red", $selectBoxOption->getText() ); 191 | } 192 | 193 | public function testFindElementsShouldGetOneOfFoundElementsText() 194 | { 195 | 196 | $selectBox = $this->_driver->findElement(By::id("sel1")); 197 | 198 | $selectBoxOptions = $selectBox->findElements(By::tagName("option")); 199 | 200 | foreach($selectBoxOptions as $selectBoxOption) 201 | { 202 | 203 | $this->assertTrue($selectBoxOption instanceof WebElement); 204 | if($selectBoxOption->getAttribute("value") == "4") 205 | { 206 | 207 | $selectBoxOption->click(); 208 | } 209 | } 210 | 211 | foreach($selectBoxOptions as $selectBoxOption) 212 | { 213 | 214 | if($selectBoxOption->getAttribute("selected") == "true") 215 | { 216 | $this->assertEquals("Black", $selectBoxOption->getText()); 217 | } 218 | } 219 | } 220 | 221 | public function testClickShouldSubmitForm() 222 | { 223 | $button = $this->_driver->findElement(By::id("btnSubmit")); 224 | 225 | $button->click(); 226 | 227 | $this->assertTrue(strstr($this->_driver->getCurrentUrl(), "formReceptor") >= 0); 228 | } 229 | 230 | public function testClickShouldGetAlert() 231 | { 232 | $webElement = $this->_driver->findElement(By::id("btnAlert")); 233 | $webElement->click(); 234 | $this->assertEquals('Here is the alert',$this->_driver->switchTo()->alert()->getText()); 235 | } 236 | 237 | public function testSubmitShouldSubmitForm() 238 | { 239 | $form = $this->_driver->findElement(By::xPath("/html/body/table/tbody/tr/td[1]/fieldset/form")); 240 | $form->submit(); 241 | $this->assertTrue(strstr($this->_driver->getCurrentUrl(), "formReceptor") >= 0); 242 | } 243 | 244 | public function testSubmitShouldSubmitFormFromButton() 245 | { 246 | $button = $this->_driver->findElement(By::id("btnSubmit")); 247 | 248 | $button->submit(); 249 | 250 | $this->assertTrue(strstr($this->_driver->getCurrentUrl(), "formReceptor") >= 0); 251 | } 252 | 253 | public function testGetTextShouldGetText() 254 | { 255 | $label = $this->_driver->findElement(By::xPath("/html/body/table/tbody/tr/td[2]/fieldset/p")); 256 | $this->assertEquals("Simple paragraph", $label->getText()); 257 | } 258 | 259 | public function testSendKeysShouldRetreiveText() 260 | { 261 | $textarea1 = $this->_driver->findElement(By::id("txtArea1")); 262 | $textarea1->sendKeys("TEST"); 263 | $this->assertEquals("TEST", $textarea1->getAttribute("value")); 264 | } 265 | 266 | public function testSendKeysShouldRetrieveHebrewText() 267 | { 268 | $textarea1 = $this->_driver->findElement(By::id("txtArea1")); 269 | $textarea1->sendKeys("יאיר 34557"); 270 | $this->assertEquals("יאיר 34557", $textarea1->getAttribute("value")); 271 | } 272 | } -------------------------------------------------------------------------------- /test/Nearsoft/SeleniumClient/WindowTest.php: -------------------------------------------------------------------------------- 1 | _driver->manage()->window()->getSize(); 15 | $this->_driver->manage()->window()->maximize(); 16 | $dimensionsAfter = $this->_driver->manage()->window()->getSize(); 17 | $this->assertTrue($dimensionsAfter['height'] > $dimensionsBefore['height']); 18 | $this->assertTrue($dimensionsAfter['width'] > $dimensionsBefore['width']); 19 | } 20 | 21 | public function testSizeShouldSetGet() 22 | { 23 | $width = 235; $height = 318; 24 | $this->_driver->manage()->window()->setSize($width, $height); 25 | $dimensions = $this->_driver->manage()->window()->getSize(); 26 | $this->assertEquals($width, $dimensions["width"]); 27 | $this->assertEquals($height, $dimensions["height"]); 28 | } 29 | 30 | public function testPositionShouldSetGet() 31 | { 32 | $x = 55; $y = 66; 33 | $this->_driver->manage()->window()->setPosition($x, $y); 34 | $dimensions = $this->_driver->manage()->window()->getPosition(); 35 | $this->assertEquals($x, $dimensions["x"]); 36 | $this->assertEquals($y, $dimensions["y"]); 37 | } 38 | } -------------------------------------------------------------------------------- /test/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Nearsoft/SeleniumClient 13 | Nearsoft/SeleniumClient/AbstractTest.php 14 | 15 | 16 | 17 | 18 | ../SeleniumClient 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | --------------------------------------------------------------------------------