Container.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Copyright 2004-2014 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(WebDriverException::NO_SUCH_ELEMENT,
  77. sprintf(
  78. "Element not found with %s, %s\n",
  79. $locatorJson['using'],
  80. $locatorJson['value']
  81. )
  82. );
  83. }
  84. return $element;
  85. }
  86. /**
  87. * Find elements: /session/:sessionId/elements (POST)
  88. * Find child elements: /session/:sessionId/element/:id/elements (POST)
  89. * Search for multiple elements on page, starting from the document root.
  90. *
  91. * @param string $using the locator strategy to use
  92. * @param string $value the search target
  93. *
  94. * @return array
  95. *
  96. * @throws \WebDriver\Exception if invalid XPath
  97. */
  98. public function elements($using = null, $value = null)
  99. {
  100. $locatorJson = $this->parseArgs('elements', func_get_args());
  101. $result = $this->curl(
  102. 'POST',
  103. '/elements',
  104. $locatorJson
  105. );
  106. if (!is_array($result['value'])) {
  107. return array();
  108. }
  109. return array_filter(array_map(
  110. array($this, 'webDriverElement'), $result['value']
  111. ));
  112. }
  113. /**
  114. * Parse arguments allowing either separate $using and $value parameters, or
  115. * as an array containing the JSON parameters
  116. *
  117. * @param string $method method name
  118. * @param array $argv arguments
  119. *
  120. * @return array
  121. *
  122. * @throws \WebDriver\Exception if invalid number of arguments to the called method
  123. */
  124. private function parseArgs($method, $argv)
  125. {
  126. $argc = count($argv);
  127. switch ($argc) {
  128. case 2:
  129. $using = $argv[0];
  130. $value = $argv[1];
  131. break;
  132. case 1:
  133. $arg = $argv[0];
  134. if (is_array($arg)) {
  135. $using = $arg['using'];
  136. $value = $arg['value'];
  137. break;
  138. }
  139. default:
  140. throw WebDriverException::factory(
  141. WebDriverException::JSON_PARAMETERS_EXPECTED,
  142. sprintf('Invalid arguments to %s method: %s', $method, print_r($argv, true))
  143. );
  144. }
  145. return $this->locate($using, $value);
  146. }
  147. /**
  148. * Return JSON parameter for element / elements command
  149. *
  150. * @param string $using locator strategy
  151. * @param string $value search target
  152. *
  153. * @return array
  154. *
  155. * @throws \WebDriver\Exception if invalid locator strategy
  156. */
  157. public function locate($using, $value)
  158. {
  159. if (!in_array($using, $this->strategies)) {
  160. throw WebDriverException::factory(
  161. WebDriverException::UNKNOWN_LOCATOR_STRATEGY,
  162. sprintf('Invalid locator strategy %s', $using)
  163. );
  164. }
  165. return array(
  166. 'using' => $using,
  167. 'value' => $value,
  168. );
  169. }
  170. /**
  171. * Return WebDriver\Element wrapper for $value
  172. *
  173. * @param mixed $value
  174. *
  175. * @return \WebDriver\Element|null
  176. */
  177. protected function webDriverElement($value)
  178. {
  179. return array_key_exists('ELEMENT', (array) $value)
  180. ? new Element(
  181. $this->getElementPath($value['ELEMENT']), // url
  182. $value['ELEMENT'] // id
  183. )
  184. : null;
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function __call($name, $arguments)
  190. {
  191. if (count($arguments) === 1 && in_array(str_replace('_', ' ', $name), $this->strategies)) {
  192. return $this->locate($name, $arguments[0]);
  193. }
  194. // fallback to executing WebDriver commands
  195. return parent::__call($name, $arguments);
  196. }
  197. /**
  198. * Get wire protocol URL for an element
  199. *
  200. * @param string $elementId
  201. *
  202. * @return string
  203. */
  204. abstract protected function getElementPath($elementId);
  205. }