ChromeDriver.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. public static function start(DesiredCapabilities $desired_capabilities = null, ChromeDriverService $service = null)
  25. {
  26. if ($desired_capabilities === null) {
  27. $desired_capabilities = DesiredCapabilities::chrome();
  28. }
  29. if ($service === null) {
  30. $service = ChromeDriverService::createDefaultService();
  31. }
  32. $executor = new DriverCommandExecutor($service);
  33. $driver = new static($executor, null, $desired_capabilities);
  34. $driver->startSession($desired_capabilities);
  35. return $driver;
  36. }
  37. public function startSession(DesiredCapabilities $desired_capabilities)
  38. {
  39. $command = new WebDriverCommand(
  40. null,
  41. DriverCommand::NEW_SESSION,
  42. [
  43. 'desiredCapabilities' => $desired_capabilities->toArray(),
  44. ]
  45. );
  46. $response = $this->executor->execute($command);
  47. $this->sessionID = $response->getSessionID();
  48. }
  49. /**
  50. * Always throws an exception. Use ChromeDriver::start() instead.
  51. *
  52. * @throws WebDriverException
  53. */
  54. public static function create(
  55. $selenium_server_url = 'http://localhost:4444/wd/hub',
  56. $desired_capabilities = null,
  57. $connection_timeout_in_ms = null,
  58. $request_timeout_in_ms = null,
  59. $http_proxy = null,
  60. $http_proxy_port = null,
  61. DesiredCapabilities $required_capabilities = null
  62. ) {
  63. throw new WebDriverException('Please use ChromeDriver::start() instead.');
  64. }
  65. /**
  66. * Always throws an exception. Use ChromeDriver::start() instead.
  67. *
  68. * @param string $session_id The existing session id
  69. * @param string $selenium_server_url The url of the remote Selenium WebDriver server
  70. *
  71. * @throws WebDriverException
  72. * @return RemoteWebDriver|void
  73. */
  74. public static function createBySessionID(
  75. $session_id,
  76. $selenium_server_url = 'http://localhost:4444/wd/hub'
  77. ) {
  78. throw new WebDriverException('Please use ChromeDriver::start() instead.');
  79. }
  80. }