123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
- // Copyright 2004-present Facebook. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- namespace Facebook\WebDriver\Support\Events;
- use Facebook\WebDriver\Exception\UnsupportedOperationException;
- use Facebook\WebDriver\Exception\WebDriverException;
- use Facebook\WebDriver\Interactions\Touch\WebDriverTouchScreen;
- use Facebook\WebDriver\JavaScriptExecutor;
- use Facebook\WebDriver\WebDriver;
- use Facebook\WebDriver\WebDriverBy;
- use Facebook\WebDriver\WebDriverDispatcher;
- use Facebook\WebDriver\WebDriverElement;
- use Facebook\WebDriver\WebDriverOptions;
- use Facebook\WebDriver\WebDriverTargetLocator;
- use Facebook\WebDriver\WebDriverWait;
- class EventFiringWebDriver implements WebDriver, JavaScriptExecutor
- {
- /**
- * @var WebDriver
- */
- protected $driver;
- /**
- * @var WebDriverDispatcher
- */
- protected $dispatcher;
- /**
- * @param WebDriver $driver
- * @param WebDriverDispatcher $dispatcher
- */
- public function __construct(WebDriver $driver, WebDriverDispatcher $dispatcher = null)
- {
- $this->dispatcher = $dispatcher ?: new WebDriverDispatcher();
- if (!$this->dispatcher->getDefaultDriver()) {
- $this->dispatcher->setDefaultDriver($this);
- }
- $this->driver = $driver;
- }
- /**
- * @return WebDriverDispatcher
- */
- public function getDispatcher()
- {
- return $this->dispatcher;
- }
- /**
- * @return WebDriver
- */
- public function getWebDriver()
- {
- return $this->driver;
- }
- /**
- * @param mixed $url
- * @throws WebDriverException
- * @return $this
- */
- public function get($url)
- {
- $this->dispatch('beforeNavigateTo', $url, $this);
- try {
- $this->driver->get($url);
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- $this->dispatch('afterNavigateTo', $url, $this);
- return $this;
- }
- /**
- * @param WebDriverBy $by
- * @throws WebDriverException
- * @return array
- */
- public function findElements(WebDriverBy $by)
- {
- $this->dispatch('beforeFindBy', $by, null, $this);
- $elements = [];
- try {
- foreach ($this->driver->findElements($by) as $element) {
- $elements[] = $this->newElement($element);
- }
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- $this->dispatch('afterFindBy', $by, null, $this);
- return $elements;
- }
- /**
- * @param WebDriverBy $by
- * @throws WebDriverException
- * @return EventFiringWebElement
- */
- public function findElement(WebDriverBy $by)
- {
- $this->dispatch('beforeFindBy', $by, null, $this);
- try {
- $element = $this->newElement($this->driver->findElement($by));
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- $this->dispatch('afterFindBy', $by, null, $this);
- return $element;
- }
- /**
- * @param string $script
- * @param array $arguments
- * @throws WebDriverException
- * @return mixed
- */
- public function executeScript($script, array $arguments = [])
- {
- if (!$this->driver instanceof JavaScriptExecutor) {
- throw new UnsupportedOperationException(
- 'driver does not implement JavaScriptExecutor'
- );
- }
- $this->dispatch('beforeScript', $script, $this);
- try {
- $result = $this->driver->executeScript($script, $arguments);
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- $this->dispatch('afterScript', $script, $this);
- return $result;
- }
- /**
- * @param string $script
- * @param array $arguments
- * @throws WebDriverException
- * @return mixed
- */
- public function executeAsyncScript($script, array $arguments = [])
- {
- if (!$this->driver instanceof JavaScriptExecutor) {
- throw new UnsupportedOperationException(
- 'driver does not implement JavaScriptExecutor'
- );
- }
- $this->dispatch('beforeScript', $script, $this);
- try {
- $result = $this->driver->executeAsyncScript($script, $arguments);
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- $this->dispatch('afterScript', $script, $this);
- return $result;
- }
- /**
- * @throws WebDriverException
- * @return $this
- */
- public function close()
- {
- try {
- $this->driver->close();
- return $this;
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return string
- */
- public function getCurrentURL()
- {
- try {
- return $this->driver->getCurrentURL();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return string
- */
- public function getPageSource()
- {
- try {
- return $this->driver->getPageSource();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return string
- */
- public function getTitle()
- {
- try {
- return $this->driver->getTitle();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return string
- */
- public function getWindowHandle()
- {
- try {
- return $this->driver->getWindowHandle();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return array
- */
- public function getWindowHandles()
- {
- try {
- return $this->driver->getWindowHandles();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- */
- public function quit()
- {
- try {
- $this->driver->quit();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @param null|string $save_as
- * @throws WebDriverException
- * @return string
- */
- public function takeScreenshot($save_as = null)
- {
- try {
- return $this->driver->takeScreenshot($save_as);
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @param int $timeout_in_second
- * @param int $interval_in_millisecond
- * @throws WebDriverException
- * @return WebDriverWait
- */
- public function wait($timeout_in_second = 30, $interval_in_millisecond = 250)
- {
- try {
- return $this->driver->wait($timeout_in_second, $interval_in_millisecond);
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return WebDriverOptions
- */
- public function manage()
- {
- try {
- return $this->driver->manage();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return EventFiringWebDriverNavigation
- */
- public function navigate()
- {
- try {
- return new EventFiringWebDriverNavigation(
- $this->driver->navigate(),
- $this->getDispatcher()
- );
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return WebDriverTargetLocator
- */
- public function switchTo()
- {
- try {
- return $this->driver->switchTo();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @throws WebDriverException
- * @return WebDriverTouchScreen
- */
- public function getTouch()
- {
- try {
- return $this->driver->getTouch();
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- public function execute($name, $params)
- {
- try {
- return $this->driver->execute($name, $params);
- } catch (WebDriverException $exception) {
- $this->dispatchOnException($exception);
- throw $exception;
- }
- }
- /**
- * @param WebDriverElement $element
- * @return EventFiringWebElement
- */
- protected function newElement(WebDriverElement $element)
- {
- return new EventFiringWebElement($element, $this->getDispatcher());
- }
- /**
- * @param mixed $method
- * @param mixed ...$arguments
- */
- protected function dispatch($method, ...$arguments)
- {
- if (!$this->dispatcher) {
- return;
- }
- $this->dispatcher->dispatch($method, $arguments);
- }
- /**
- * @param WebDriverException $exception
- */
- protected function dispatchOnException(WebDriverException $exception)
- {
- $this->dispatch('onException', $exception, $this);
- }
- }
|