annotate vendor/instaclick/php-webdriver/lib/WebDriver/Container.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Copyright 2004-2017 Facebook. All Rights Reserved.
Chris@0 4 *
Chris@0 5 * Licensed under the Apache License, Version 2.0 (the "License");
Chris@0 6 * you may not use this file except in compliance with the License.
Chris@0 7 * You may obtain a copy of the License at
Chris@0 8 *
Chris@0 9 * http://www.apache.org/licenses/LICENSE-2.0
Chris@0 10 *
Chris@0 11 * Unless required by applicable law or agreed to in writing, software
Chris@0 12 * distributed under the License is distributed on an "AS IS" BASIS,
Chris@0 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Chris@0 14 * See the License for the specific language governing permissions and
Chris@0 15 * limitations under the License.
Chris@0 16 *
Chris@0 17 * @package WebDriver
Chris@0 18 *
Chris@0 19 * @author Justin Bishop <jubishop@gmail.com>
Chris@0 20 * @author Anthon Pang <apang@softwaredevelopment.ca>
Chris@0 21 * @author Fabrizio Branca <mail@fabrizio-branca.de>
Chris@0 22 */
Chris@0 23
Chris@0 24 namespace WebDriver;
Chris@0 25
Chris@0 26 use WebDriver\Exception as WebDriverException;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Abstract WebDriver\Container class
Chris@0 30 *
Chris@0 31 * @package WebDriver
Chris@0 32 */
Chris@0 33 abstract class Container extends AbstractWebDriver
Chris@0 34 {
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 public function __construct($url = 'http://localhost:4444/wd/hub')
Chris@0 39 {
Chris@0 40 parent::__construct($url);
Chris@0 41
Chris@0 42 $locatorStrategy = new \ReflectionClass('WebDriver\LocatorStrategy');
Chris@0 43 $this->strategies = $locatorStrategy->getConstants();
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Find element: /session/:sessionId/element (POST)
Chris@0 48 * Find child element: /session/:sessionId/element/:id/element (POST)
Chris@0 49 * Search for element on page, starting from the document root.
Chris@0 50 *
Chris@0 51 * @param string $using the locator strategy to use
Chris@0 52 * @param string $value the search target
Chris@0 53 *
Chris@0 54 * @return \WebDriver\Element
Chris@0 55 *
Chris@0 56 * @throws \WebDriver\Exception if element not found, or invalid XPath
Chris@0 57 */
Chris@0 58 public function element($using = null, $value = null)
Chris@0 59 {
Chris@0 60 $locatorJson = $this->parseArgs('element', func_get_args());
Chris@0 61
Chris@0 62 try {
Chris@0 63 $result = $this->curl(
Chris@0 64 'POST',
Chris@0 65 '/element',
Chris@0 66 $locatorJson
Chris@0 67 );
Chris@0 68 } catch (WebDriverException\NoSuchElement $e) {
Chris@0 69 throw WebDriverException::factory(
Chris@0 70 WebDriverException::NO_SUCH_ELEMENT,
Chris@0 71 sprintf(
Chris@0 72 "Element not found with %s, %s\n\n%s",
Chris@0 73 $locatorJson['using'],
Chris@0 74 $locatorJson['value'],
Chris@0 75 $e->getMessage()
Chris@0 76 ),
Chris@0 77 $e
Chris@0 78 );
Chris@0 79 }
Chris@0 80
Chris@0 81 $element = $this->webDriverElement($result['value']);
Chris@0 82
Chris@0 83 if ($element === null) {
Chris@0 84 throw WebDriverException::factory(
Chris@0 85 WebDriverException::NO_SUCH_ELEMENT,
Chris@0 86 sprintf(
Chris@0 87 "Element not found with %s, %s\n",
Chris@0 88 $locatorJson['using'],
Chris@0 89 $locatorJson['value']
Chris@0 90 )
Chris@0 91 );
Chris@0 92 }
Chris@0 93
Chris@0 94 return $element;
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Find elements: /session/:sessionId/elements (POST)
Chris@0 99 * Find child elements: /session/:sessionId/element/:id/elements (POST)
Chris@0 100 * Search for multiple elements on page, starting from the document root.
Chris@0 101 *
Chris@0 102 * @param string $using the locator strategy to use
Chris@0 103 * @param string $value the search target
Chris@0 104 *
Chris@0 105 * @return array
Chris@0 106 *
Chris@0 107 * @throws \WebDriver\Exception if invalid XPath
Chris@0 108 */
Chris@0 109 public function elements($using = null, $value = null)
Chris@0 110 {
Chris@0 111 $locatorJson = $this->parseArgs('elements', func_get_args());
Chris@0 112
Chris@0 113 $result = $this->curl(
Chris@0 114 'POST',
Chris@0 115 '/elements',
Chris@0 116 $locatorJson
Chris@0 117 );
Chris@0 118
Chris@0 119 if (!is_array($result['value'])) {
Chris@0 120 return array();
Chris@0 121 }
Chris@0 122
Chris@0 123 return array_filter(
Chris@0 124 array_map(
Chris@0 125 array($this, 'webDriverElement'),
Chris@0 126 $result['value']
Chris@0 127 )
Chris@0 128 );
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * Parse arguments allowing either separate $using and $value parameters, or
Chris@0 133 * as an array containing the JSON parameters
Chris@0 134 *
Chris@0 135 * @param string $method method name
Chris@0 136 * @param array $argv arguments
Chris@0 137 *
Chris@0 138 * @return array
Chris@0 139 *
Chris@0 140 * @throws \WebDriver\Exception if invalid number of arguments to the called method
Chris@0 141 */
Chris@0 142 private function parseArgs($method, $argv)
Chris@0 143 {
Chris@0 144 $argc = count($argv);
Chris@0 145
Chris@0 146 switch ($argc) {
Chris@0 147 case 2:
Chris@0 148 $using = $argv[0];
Chris@0 149 $value = $argv[1];
Chris@0 150 break;
Chris@0 151
Chris@0 152 case 1:
Chris@0 153 $arg = $argv[0];
Chris@0 154
Chris@0 155 if (is_array($arg)) {
Chris@0 156 $using = $arg['using'];
Chris@0 157 $value = $arg['value'];
Chris@0 158 break;
Chris@0 159 }
Chris@0 160
Chris@0 161 // fall through
Chris@0 162 default:
Chris@0 163 throw WebDriverException::factory(
Chris@0 164 WebDriverException::JSON_PARAMETERS_EXPECTED,
Chris@0 165 sprintf('Invalid arguments to %s method: %s', $method, print_r($argv, true))
Chris@0 166 );
Chris@0 167 }
Chris@0 168
Chris@0 169 return $this->locate($using, $value);
Chris@0 170 }
Chris@0 171
Chris@0 172 /**
Chris@0 173 * Return JSON parameter for element / elements command
Chris@0 174 *
Chris@0 175 * @param string $using locator strategy
Chris@0 176 * @param string $value search target
Chris@0 177 *
Chris@0 178 * @return array
Chris@0 179 *
Chris@0 180 * @throws \WebDriver\Exception if invalid locator strategy
Chris@0 181 */
Chris@0 182 public function locate($using, $value)
Chris@0 183 {
Chris@0 184 if (!in_array($using, $this->strategies)) {
Chris@0 185 throw WebDriverException::factory(
Chris@0 186 WebDriverException::UNKNOWN_LOCATOR_STRATEGY,
Chris@0 187 sprintf('Invalid locator strategy %s', $using)
Chris@0 188 );
Chris@0 189 }
Chris@0 190
Chris@0 191 return array(
Chris@0 192 'using' => $using,
Chris@0 193 'value' => $value,
Chris@0 194 );
Chris@0 195 }
Chris@0 196
Chris@0 197 /**
Chris@0 198 * Return WebDriver\Element wrapper for $value
Chris@0 199 *
Chris@0 200 * @param mixed $value
Chris@0 201 *
Chris@0 202 * @return \WebDriver\Element|null
Chris@0 203 */
Chris@0 204 protected function webDriverElement($value)
Chris@0 205 {
Chris@0 206 return array_key_exists('ELEMENT', (array) $value)
Chris@0 207 ? new Element(
Chris@0 208 $this->getElementPath($value['ELEMENT']), // url
Chris@0 209 $value['ELEMENT'] // id
Chris@0 210 )
Chris@0 211 : null;
Chris@0 212 }
Chris@0 213
Chris@0 214 /**
Chris@0 215 * {@inheritdoc}
Chris@0 216 */
Chris@0 217 public function __call($name, $arguments)
Chris@0 218 {
Chris@0 219 if (count($arguments) === 1 && in_array(str_replace('_', ' ', $name), $this->strategies)) {
Chris@0 220 return $this->locate($name, $arguments[0]);
Chris@0 221 }
Chris@0 222
Chris@0 223 // fallback to executing WebDriver commands
Chris@0 224 return parent::__call($name, $arguments);
Chris@0 225 }
Chris@0 226
Chris@0 227 /**
Chris@0 228 * Get wire protocol URL for an element
Chris@0 229 *
Chris@0 230 * @param string $elementId
Chris@0 231 *
Chris@0 232 * @return string
Chris@0 233 */
Chris@0 234 abstract protected function getElementPath($elementId);
Chris@0 235 }