WebDriverOptions.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * Here are the valid attributes of a cookie array.
  36. * 'name' : string The name of the cookie; may not be null or an empty string.
  37. * 'value' : string The cookie value; may not be null.
  38. * 'path' : string OPTIONAL The path the cookie is visible to. Defaults to "/" if omitted.
  39. * 'domain' : string OPTIONAL The domain the cookie is visible to. Defaults to the current browsing context's
  40. * document's URL domain if omitted.
  41. * 'secure' : bool OPTIONAL Whether this cookie requires a secure connection (https). Defaults to false if
  42. * omitted.
  43. * 'httpOnly': bool OPTIONAL Whether the cookie is an HTTP only cookie. Defaults to false if omitted.
  44. * 'expiry' : int OPTIONAL The cookie's expiration date, specified in seconds since Unix Epoch.
  45. *
  46. * @see https://w3c.github.io/webdriver/webdriver-spec.html#cookies
  47. * @param array $cookie An array with key as the attributes mentioned above.
  48. * @return WebDriverOptions The current instance.
  49. */
  50. public function addCookie(array $cookie)
  51. {
  52. $this->validate($cookie);
  53. $this->executor->execute(
  54. DriverCommand::ADD_COOKIE,
  55. ['cookie' => $cookie]
  56. );
  57. return $this;
  58. }
  59. /**
  60. * Delete all the cookies that are currently visible.
  61. *
  62. * @return WebDriverOptions The current instance.
  63. */
  64. public function deleteAllCookies()
  65. {
  66. $this->executor->execute(DriverCommand::DELETE_ALL_COOKIES);
  67. return $this;
  68. }
  69. /**
  70. * Delete the cookie with the give name.
  71. *
  72. * @param string $name
  73. * @return WebDriverOptions The current instance.
  74. */
  75. public function deleteCookieNamed($name)
  76. {
  77. $this->executor->execute(
  78. DriverCommand::DELETE_COOKIE,
  79. [':name' => $name]
  80. );
  81. return $this;
  82. }
  83. /**
  84. * Get the cookie with a given name.
  85. *
  86. * @param string $name
  87. * @return array The cookie, or null if no cookie with the given name is presented.
  88. */
  89. public function getCookieNamed($name)
  90. {
  91. $cookies = $this->getCookies();
  92. foreach ($cookies as $cookie) {
  93. if ($cookie['name'] === $name) {
  94. return $cookie;
  95. }
  96. }
  97. return null;
  98. }
  99. /**
  100. * Get all the cookies for the current domain.
  101. *
  102. * @return array The array of cookies presented.
  103. */
  104. public function getCookies()
  105. {
  106. return $this->executor->execute(DriverCommand::GET_ALL_COOKIES);
  107. }
  108. private function validate(array $cookie)
  109. {
  110. if (!isset($cookie['name']) ||
  111. $cookie['name'] === '' ||
  112. strpos($cookie['name'], ';') !== false
  113. ) {
  114. throw new InvalidArgumentException(
  115. '"name" should be non-empty and does not contain a ";"'
  116. );
  117. }
  118. if (!isset($cookie['value'])) {
  119. throw new InvalidArgumentException(
  120. '"value" is required when setting a cookie.'
  121. );
  122. }
  123. if (isset($cookie['domain']) && strpos($cookie['domain'], ':') !== false) {
  124. throw new InvalidArgumentException(
  125. '"domain" should not contain a port:' . (string) $cookie['domain']
  126. );
  127. }
  128. }
  129. /**
  130. * Return the interface for managing driver timeouts.
  131. *
  132. * @return WebDriverTimeouts
  133. */
  134. public function timeouts()
  135. {
  136. return new WebDriverTimeouts($this->executor);
  137. }
  138. /**
  139. * An abstraction allowing the driver to manipulate the browser's window
  140. *
  141. * @return WebDriverWindow
  142. * @see WebDriverWindow
  143. */
  144. public function window()
  145. {
  146. return new WebDriverWindow($this->executor);
  147. }
  148. /**
  149. * Get the log for a given log type. Log buffer is reset after each request.
  150. *
  151. * @param string $log_type The log type.
  152. * @return array The list of log entries.
  153. * @see https://code.google.com/p/selenium/wiki/JsonWireProtocol#Log_Type
  154. */
  155. public function getLog($log_type)
  156. {
  157. return $this->executor->execute(
  158. DriverCommand::GET_LOG,
  159. ['type' => $log_type]
  160. );
  161. }
  162. /**
  163. * Get available log types.
  164. *
  165. * @return array The list of available log types.
  166. * @see https://code.google.com/p/selenium/wiki/JsonWireProtocol#Log_Type
  167. */
  168. public function getAvailableLogTypes()
  169. {
  170. return $this->executor->execute(DriverCommand::GET_AVAILABLE_LOG_TYPES);
  171. }
  172. }