EventFiringWebDriverNavigation.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // Copyright 2004-present Facebook. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. namespace Facebook\WebDriver\Support\Events;
  16. use Facebook\WebDriver\Exception\WebDriverException;
  17. use Facebook\WebDriver\WebDriverDispatcher;
  18. use Facebook\WebDriver\WebDriverNavigation;
  19. class EventFiringWebDriverNavigation
  20. {
  21. /**
  22. * @var WebDriverNavigation
  23. */
  24. protected $navigator;
  25. /**
  26. * @var WebDriverDispatcher
  27. */
  28. protected $dispatcher;
  29. /**
  30. * @param WebDriverNavigation $navigator
  31. * @param WebDriverDispatcher $dispatcher
  32. */
  33. public function __construct(WebDriverNavigation $navigator, WebDriverDispatcher $dispatcher)
  34. {
  35. $this->navigator = $navigator;
  36. $this->dispatcher = $dispatcher;
  37. }
  38. /**
  39. * @return WebDriverDispatcher
  40. */
  41. public function getDispatcher()
  42. {
  43. return $this->dispatcher;
  44. }
  45. /**
  46. * @return WebDriverNavigation
  47. */
  48. public function getNavigator()
  49. {
  50. return $this->navigator;
  51. }
  52. /**
  53. * @throws WebDriverException
  54. * @return $this
  55. */
  56. public function back()
  57. {
  58. $this->dispatch(
  59. 'beforeNavigateBack',
  60. $this->getDispatcher()->getDefaultDriver()
  61. );
  62. try {
  63. $this->navigator->back();
  64. } catch (WebDriverException $exception) {
  65. $this->dispatchOnException($exception);
  66. }
  67. $this->dispatch(
  68. 'afterNavigateBack',
  69. $this->getDispatcher()->getDefaultDriver()
  70. );
  71. return $this;
  72. }
  73. /**
  74. * @throws WebDriverException
  75. * @return $this
  76. */
  77. public function forward()
  78. {
  79. $this->dispatch(
  80. 'beforeNavigateForward',
  81. $this->getDispatcher()->getDefaultDriver()
  82. );
  83. try {
  84. $this->navigator->forward();
  85. } catch (WebDriverException $exception) {
  86. $this->dispatchOnException($exception);
  87. }
  88. $this->dispatch(
  89. 'afterNavigateForward',
  90. $this->getDispatcher()->getDefaultDriver()
  91. );
  92. return $this;
  93. }
  94. /**
  95. * @throws WebDriverException
  96. * @return $this
  97. */
  98. public function refresh()
  99. {
  100. try {
  101. $this->navigator->refresh();
  102. return $this;
  103. } catch (WebDriverException $exception) {
  104. $this->dispatchOnException($exception);
  105. throw $exception;
  106. }
  107. }
  108. /**
  109. * @param mixed $url
  110. * @throws WebDriverException
  111. * @return $this
  112. */
  113. public function to($url)
  114. {
  115. $this->dispatch(
  116. 'beforeNavigateTo',
  117. $url,
  118. $this->getDispatcher()->getDefaultDriver()
  119. );
  120. try {
  121. $this->navigator->to($url);
  122. } catch (WebDriverException $exception) {
  123. $this->dispatchOnException($exception);
  124. throw $exception;
  125. }
  126. $this->dispatch(
  127. 'afterNavigateTo',
  128. $url,
  129. $this->getDispatcher()->getDefaultDriver()
  130. );
  131. return $this;
  132. }
  133. /**
  134. * @param mixed $method
  135. * @param mixed ...$arguments
  136. */
  137. protected function dispatch($method, ...$arguments)
  138. {
  139. if (!$this->dispatcher) {
  140. return;
  141. }
  142. $this->dispatcher->dispatch($method, $arguments);
  143. }
  144. /**
  145. * @param WebDriverException $exception
  146. */
  147. protected function dispatchOnException(WebDriverException $exception)
  148. {
  149. $this->dispatch('onException', $exception);
  150. }
  151. }