123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace WebDriver;
- use WebDriver\Exception as WebDriverException;
- abstract class Container extends AbstractWebDriver
- {
-
- public function __construct($url = 'http://localhost:4444/wd/hub')
- {
- parent::__construct($url);
- $locatorStrategy = new \ReflectionClass('WebDriver\LocatorStrategy');
- $this->strategies = $locatorStrategy->getConstants();
- }
-
- public function element($using = null, $value = null)
- {
- $locatorJson = $this->parseArgs('element', func_get_args());
- try {
- $result = $this->curl(
- 'POST',
- '/element',
- $locatorJson
- );
- } catch (WebDriverException\NoSuchElement $e) {
- throw WebDriverException::factory(
- WebDriverException::NO_SUCH_ELEMENT,
- sprintf(
- "Element not found with %s, %s\n\n%s",
- $locatorJson['using'],
- $locatorJson['value'],
- $e->getMessage()
- ),
- $e
- );
- }
- $element = $this->webDriverElement($result['value']);
- if ($element === null) {
- throw WebDriverException::factory(
- WebDriverException::NO_SUCH_ELEMENT,
- sprintf(
- "Element not found with %s, %s\n",
- $locatorJson['using'],
- $locatorJson['value']
- )
- );
- }
- return $element;
- }
-
- public function elements($using = null, $value = null)
- {
- $locatorJson = $this->parseArgs('elements', func_get_args());
- $result = $this->curl(
- 'POST',
- '/elements',
- $locatorJson
- );
- if (!is_array($result['value'])) {
- return array();
- }
- return array_filter(
- array_map(
- array($this, 'webDriverElement'),
- $result['value']
- )
- );
- }
-
- private function parseArgs($method, $argv)
- {
- $argc = count($argv);
- switch ($argc) {
- case 2:
- $using = $argv[0];
- $value = $argv[1];
- break;
- case 1:
- $arg = $argv[0];
- if (is_array($arg)) {
- $using = $arg['using'];
- $value = $arg['value'];
- break;
- }
-
- default:
- throw WebDriverException::factory(
- WebDriverException::JSON_PARAMETERS_EXPECTED,
- sprintf('Invalid arguments to %s method: %s', $method, print_r($argv, true))
- );
- }
- return $this->locate($using, $value);
- }
-
- public function locate($using, $value)
- {
- if (!in_array($using, $this->strategies)) {
- throw WebDriverException::factory(
- WebDriverException::UNKNOWN_LOCATOR_STRATEGY,
- sprintf('Invalid locator strategy %s', $using)
- );
- }
- return array(
- 'using' => $using,
- 'value' => $value,
- );
- }
-
- protected function webDriverElement($value)
- {
- return array_key_exists('ELEMENT', (array) $value)
- ? new Element(
- $this->getElementPath($value['ELEMENT']),
- $value['ELEMENT']
- )
- : null;
- }
-
- public function __call($name, $arguments)
- {
- if (count($arguments) === 1 && in_array(str_replace('_', ' ', $name), $this->strategies)) {
- return $this->locate($name, $arguments[0]);
- }
-
- return parent::__call($name, $arguments);
- }
-
- abstract protected function getElementPath($elementId);
- }
|