ChromeOptions.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Remote\DesiredCapabilities;
  17. /**
  18. * The class manages the capabilities in ChromeDriver.
  19. *
  20. * @see https://sites.google.com/a/chromium.org/chromedriver/capabilities
  21. */
  22. class ChromeOptions
  23. {
  24. /**
  25. * The key of chrome options in desired capabilities.
  26. */
  27. const CAPABILITY = 'chromeOptions';
  28. /**
  29. * @var array
  30. */
  31. private $arguments = [];
  32. /**
  33. * @var string
  34. */
  35. private $binary = '';
  36. /**
  37. * @var array
  38. */
  39. private $extensions = [];
  40. /**
  41. * @var array
  42. */
  43. private $experimentalOptions = [];
  44. /**
  45. * Sets the path of the Chrome executable. The path should be either absolute
  46. * or relative to the location running ChromeDriver server.
  47. *
  48. * @param string $path
  49. * @return ChromeOptions
  50. */
  51. public function setBinary($path)
  52. {
  53. $this->binary = $path;
  54. return $this;
  55. }
  56. /**
  57. * @param array $arguments
  58. * @return ChromeOptions
  59. */
  60. public function addArguments(array $arguments)
  61. {
  62. $this->arguments = array_merge($this->arguments, $arguments);
  63. return $this;
  64. }
  65. /**
  66. * Add a Chrome extension to install on browser startup. Each path should be
  67. * a packed Chrome extension.
  68. *
  69. * @param array $paths
  70. * @return ChromeOptions
  71. */
  72. public function addExtensions(array $paths)
  73. {
  74. foreach ($paths as $path) {
  75. $this->addExtension($path);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * @param array $encoded_extensions An array of base64 encoded of the extensions.
  81. * @return ChromeOptions
  82. */
  83. public function addEncodedExtensions(array $encoded_extensions)
  84. {
  85. foreach ($encoded_extensions as $encoded_extension) {
  86. $this->addEncodedExtension($encoded_extension);
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Sets an experimental option which has not exposed officially.
  92. *
  93. * @param string $name
  94. * @param mixed $value
  95. * @return ChromeOptions
  96. */
  97. public function setExperimentalOption($name, $value)
  98. {
  99. $this->experimentalOptions[$name] = $value;
  100. return $this;
  101. }
  102. /**
  103. * @return DesiredCapabilities The DesiredCapabilities for Chrome with this options.
  104. */
  105. public function toCapabilities()
  106. {
  107. $capabilities = DesiredCapabilities::chrome();
  108. $capabilities->setCapability(self::CAPABILITY, $this);
  109. return $capabilities;
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function toArray()
  115. {
  116. $options = $this->experimentalOptions;
  117. // The selenium server expects a 'dictionary' instead of a 'list' when
  118. // reading the chrome option. However, an empty array in PHP will be
  119. // converted to a 'list' instead of a 'dictionary'. To fix it, we always
  120. // set the 'binary' to avoid returning an empty array.
  121. $options['binary'] = $this->binary;
  122. if ($this->arguments) {
  123. $options['args'] = $this->arguments;
  124. }
  125. if ($this->extensions) {
  126. $options['extensions'] = $this->extensions;
  127. }
  128. return $options;
  129. }
  130. /**
  131. * Add a Chrome extension to install on browser startup. Each path should be a
  132. * packed Chrome extension.
  133. *
  134. * @param string $path
  135. * @return ChromeOptions
  136. */
  137. private function addExtension($path)
  138. {
  139. $this->addEncodedExtension(base64_encode(file_get_contents($path)));
  140. return $this;
  141. }
  142. /**
  143. * @param string $encoded_extension Base64 encoded of the extension.
  144. * @return ChromeOptions
  145. */
  146. private function addEncodedExtension($encoded_extension)
  147. {
  148. $this->extensions[] = $encoded_extension;
  149. return $this;
  150. }
  151. }