Container.php 6.5 KB

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