ChromeDriver.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Chrome;
  16. use Facebook\WebDriver\Exception\WebDriverException;
  17. use Facebook\WebDriver\Remote\DesiredCapabilities;
  18. use Facebook\WebDriver\Remote\DriverCommand;
  19. use Facebook\WebDriver\Remote\RemoteWebDriver;
  20. use Facebook\WebDriver\Remote\Service\DriverCommandExecutor;
  21. use Facebook\WebDriver\Remote\WebDriverCommand;
  22. class ChromeDriver extends RemoteWebDriver
  23. {
  24. /**
  25. * @return static
  26. */
  27. public static function start(DesiredCapabilities $desired_capabilities = null, ChromeDriverService $service = null)
  28. {
  29. if ($desired_capabilities === null) {
  30. $desired_capabilities = DesiredCapabilities::chrome();
  31. }
  32. if ($service === null) {
  33. $service = ChromeDriverService::createDefaultService();
  34. }
  35. $executor = new DriverCommandExecutor($service);
  36. $driver = new static($executor, null, $desired_capabilities);
  37. $driver->startSession($desired_capabilities);
  38. return $driver;
  39. }
  40. public function startSession(DesiredCapabilities $desired_capabilities)
  41. {
  42. $command = new WebDriverCommand(
  43. null,
  44. DriverCommand::NEW_SESSION,
  45. [
  46. 'desiredCapabilities' => $desired_capabilities->toArray(),
  47. ]
  48. );
  49. $response = $this->executor->execute($command);
  50. $this->sessionID = $response->getSessionID();
  51. }
  52. /**
  53. * Always throws an exception. Use ChromeDriver::start() instead.
  54. *
  55. * @param string $selenium_server_url
  56. * @param DesiredCapabilities|array $desired_capabilities
  57. * @param int|null $connection_timeout_in_ms
  58. * @param int|null $request_timeout_in_ms
  59. * @param string|null $http_proxy
  60. * @param int|null $http_proxy_port
  61. * @param DesiredCapabilities $required_capabilities
  62. * @throws WebDriverException
  63. * @return RemoteWebDriver
  64. */
  65. public static function create(
  66. $selenium_server_url = 'http://localhost:4444/wd/hub',
  67. $desired_capabilities = null,
  68. $connection_timeout_in_ms = null,
  69. $request_timeout_in_ms = null,
  70. $http_proxy = null,
  71. $http_proxy_port = null,
  72. DesiredCapabilities $required_capabilities = null
  73. ) {
  74. throw new WebDriverException('Please use ChromeDriver::start() instead.');
  75. }
  76. /**
  77. * Always throws an exception. Use ChromeDriver::start() instead.
  78. *
  79. * @param string $session_id The existing session id
  80. * @param string $selenium_server_url The url of the remote Selenium WebDriver server
  81. * @param int|null $connection_timeout_in_ms Set timeout for the connect phase to remote Selenium WebDriver server
  82. * @param int|null $request_timeout_in_ms Set the maximum time of a request to remote Selenium WebDriver server
  83. * @throws WebDriverException
  84. * @return RemoteWebDriver|void
  85. */
  86. public static function createBySessionID(
  87. $session_id,
  88. $selenium_server_url = 'http://localhost:4444/wd/hub',
  89. $connection_timeout_in_ms = null,
  90. $request_timeout_in_ms = null
  91. ) {
  92. throw new WebDriverException('Please use ChromeDriver::start() instead.');
  93. }
  94. }