WebDriverOptions.php 4.4 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;
  16. use Facebook\WebDriver\Remote\DriverCommand;
  17. use Facebook\WebDriver\Remote\ExecuteMethod;
  18. use InvalidArgumentException;
  19. /**
  20. * Managing stuff you would do in a browser.
  21. */
  22. class WebDriverOptions
  23. {
  24. /**
  25. * @var ExecuteMethod
  26. */
  27. protected $executor;
  28. public function __construct(ExecuteMethod $executor)
  29. {
  30. $this->executor = $executor;
  31. }
  32. /**
  33. * Add a specific cookie.
  34. *
  35. * @see Cookie for description of possible cookie properties
  36. * @param Cookie|array $cookie Cookie object. May be also created from array for compatibility reasons.
  37. * @return WebDriverOptions The current instance.
  38. */
  39. public function addCookie($cookie)
  40. {
  41. if (is_array($cookie)) {
  42. $cookie = Cookie::createFromArray($cookie);
  43. }
  44. if (!$cookie instanceof Cookie) {
  45. throw new InvalidArgumentException('Cookie must be set from instance of Cookie class or from array.');
  46. }
  47. $this->executor->execute(
  48. DriverCommand::ADD_COOKIE,
  49. ['cookie' => $cookie->toArray()]
  50. );
  51. return $this;
  52. }
  53. /**
  54. * Delete all the cookies that are currently visible.
  55. *
  56. * @return WebDriverOptions The current instance.
  57. */
  58. public function deleteAllCookies()
  59. {
  60. $this->executor->execute(DriverCommand::DELETE_ALL_COOKIES);
  61. return $this;
  62. }
  63. /**
  64. * Delete the cookie with the give name.
  65. *
  66. * @param string $name
  67. * @return WebDriverOptions The current instance.
  68. */
  69. public function deleteCookieNamed($name)
  70. {
  71. $this->executor->execute(
  72. DriverCommand::DELETE_COOKIE,
  73. [':name' => $name]
  74. );
  75. return $this;
  76. }
  77. /**
  78. * Get the cookie with a given name.
  79. *
  80. * @param string $name
  81. * @return Cookie|null The cookie, or null if no cookie with the given name is presented.
  82. */
  83. public function getCookieNamed($name)
  84. {
  85. $cookies = $this->getCookies();
  86. foreach ($cookies as $cookie) {
  87. if ($cookie['name'] === $name) {
  88. return $cookie;
  89. }
  90. }
  91. return null;
  92. }
  93. /**
  94. * Get all the cookies for the current domain.
  95. *
  96. * @return Cookie[] The array of cookies presented.
  97. */
  98. public function getCookies()
  99. {
  100. $cookieArrays = $this->executor->execute(DriverCommand::GET_ALL_COOKIES);
  101. $cookies = [];
  102. foreach ($cookieArrays as $cookieArray) {
  103. $cookies[] = Cookie::createFromArray($cookieArray);
  104. }
  105. return $cookies;
  106. }
  107. /**
  108. * Return the interface for managing driver timeouts.
  109. *
  110. * @return WebDriverTimeouts
  111. */
  112. public function timeouts()
  113. {
  114. return new WebDriverTimeouts($this->executor);
  115. }
  116. /**
  117. * An abstraction allowing the driver to manipulate the browser's window
  118. *
  119. * @return WebDriverWindow
  120. * @see WebDriverWindow
  121. */
  122. public function window()
  123. {
  124. return new WebDriverWindow($this->executor);
  125. }
  126. /**
  127. * Get the log for a given log type. Log buffer is reset after each request.
  128. *
  129. * @param string $log_type The log type.
  130. * @return array The list of log entries.
  131. * @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#log-type
  132. */
  133. public function getLog($log_type)
  134. {
  135. return $this->executor->execute(
  136. DriverCommand::GET_LOG,
  137. ['type' => $log_type]
  138. );
  139. }
  140. /**
  141. * Get available log types.
  142. *
  143. * @return array The list of available log types.
  144. * @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#log-type
  145. */
  146. public function getAvailableLogTypes()
  147. {
  148. return $this->executor->execute(DriverCommand::GET_AVAILABLE_LOG_TYPES);
  149. }
  150. }