DesiredCapabilities.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\Remote;
  16. use Exception;
  17. use Facebook\WebDriver\Chrome\ChromeOptions;
  18. use Facebook\WebDriver\Firefox\FirefoxDriver;
  19. use Facebook\WebDriver\Firefox\FirefoxPreferences;
  20. use Facebook\WebDriver\Firefox\FirefoxProfile;
  21. use Facebook\WebDriver\WebDriverCapabilities;
  22. use Facebook\WebDriver\WebDriverPlatform;
  23. class DesiredCapabilities implements WebDriverCapabilities
  24. {
  25. /**
  26. * @var array
  27. */
  28. private $capabilities;
  29. public function __construct(array $capabilities = [])
  30. {
  31. $this->capabilities = $capabilities;
  32. }
  33. /**
  34. * @return string The name of the browser.
  35. */
  36. public function getBrowserName()
  37. {
  38. return $this->get(WebDriverCapabilityType::BROWSER_NAME, '');
  39. }
  40. /**
  41. * @param string $browser_name
  42. * @return DesiredCapabilities
  43. */
  44. public function setBrowserName($browser_name)
  45. {
  46. $this->set(WebDriverCapabilityType::BROWSER_NAME, $browser_name);
  47. return $this;
  48. }
  49. /**
  50. * @return string The version of the browser.
  51. */
  52. public function getVersion()
  53. {
  54. return $this->get(WebDriverCapabilityType::VERSION, '');
  55. }
  56. /**
  57. * @param string $version
  58. * @return DesiredCapabilities
  59. */
  60. public function setVersion($version)
  61. {
  62. $this->set(WebDriverCapabilityType::VERSION, $version);
  63. return $this;
  64. }
  65. /**
  66. * @param string $name
  67. * @return mixed The value of a capability.
  68. */
  69. public function getCapability($name)
  70. {
  71. return $this->get($name);
  72. }
  73. /**
  74. * @param string $name
  75. * @param mixed $value
  76. * @return DesiredCapabilities
  77. */
  78. public function setCapability($name, $value)
  79. {
  80. $this->set($name, $value);
  81. return $this;
  82. }
  83. /**
  84. * @return string The name of the platform.
  85. */
  86. public function getPlatform()
  87. {
  88. return $this->get(WebDriverCapabilityType::PLATFORM, '');
  89. }
  90. /**
  91. * @param string $platform
  92. * @return DesiredCapabilities
  93. */
  94. public function setPlatform($platform)
  95. {
  96. $this->set(WebDriverCapabilityType::PLATFORM, $platform);
  97. return $this;
  98. }
  99. /**
  100. * @param string $capability_name
  101. * @return bool Whether the value is not null and not false.
  102. */
  103. public function is($capability_name)
  104. {
  105. return (bool) $this->get($capability_name);
  106. }
  107. /**
  108. * @todo Remove in next major release (BC)
  109. * @deprecated All browsers are always JS enabled except HtmlUnit and it's not meaningful to disable JS execution.
  110. * @return bool Whether javascript is enabled.
  111. */
  112. public function isJavascriptEnabled()
  113. {
  114. return $this->get(WebDriverCapabilityType::JAVASCRIPT_ENABLED, false);
  115. }
  116. /**
  117. * This is a htmlUnit-only option.
  118. *
  119. * @param bool $enabled
  120. * @throws Exception
  121. * @return DesiredCapabilities
  122. * @see https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#read-write-capabilities
  123. */
  124. public function setJavascriptEnabled($enabled)
  125. {
  126. $browser = $this->getBrowserName();
  127. if ($browser && $browser !== WebDriverBrowserType::HTMLUNIT) {
  128. throw new Exception(
  129. 'isJavascriptEnabled() is a htmlunit-only option. ' .
  130. 'See https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#read-write-capabilities.'
  131. );
  132. }
  133. $this->set(WebDriverCapabilityType::JAVASCRIPT_ENABLED, $enabled);
  134. return $this;
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function toArray()
  140. {
  141. if (isset($this->capabilities[ChromeOptions::CAPABILITY]) &&
  142. $this->capabilities[ChromeOptions::CAPABILITY] instanceof ChromeOptions
  143. ) {
  144. $this->capabilities[ChromeOptions::CAPABILITY] =
  145. $this->capabilities[ChromeOptions::CAPABILITY]->toArray();
  146. }
  147. if (isset($this->capabilities[FirefoxDriver::PROFILE]) &&
  148. $this->capabilities[FirefoxDriver::PROFILE] instanceof FirefoxProfile
  149. ) {
  150. $this->capabilities[FirefoxDriver::PROFILE] =
  151. $this->capabilities[FirefoxDriver::PROFILE]->encode();
  152. }
  153. return $this->capabilities;
  154. }
  155. /**
  156. * @return static
  157. */
  158. public static function android()
  159. {
  160. return new static([
  161. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::ANDROID,
  162. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANDROID,
  163. ]);
  164. }
  165. /**
  166. * @return static
  167. */
  168. public static function chrome()
  169. {
  170. return new static([
  171. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::CHROME,
  172. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  173. ]);
  174. }
  175. /**
  176. * @return static
  177. */
  178. public static function firefox()
  179. {
  180. $caps = new static([
  181. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::FIREFOX,
  182. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  183. ]);
  184. // disable the "Reader View" help tooltip, which can hide elements in the window.document
  185. $profile = new FirefoxProfile();
  186. $profile->setPreference(FirefoxPreferences::READER_PARSE_ON_LOAD_ENABLED, false);
  187. $caps->setCapability(FirefoxDriver::PROFILE, $profile);
  188. return $caps;
  189. }
  190. /**
  191. * @return static
  192. */
  193. public static function htmlUnit()
  194. {
  195. return new static([
  196. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
  197. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  198. ]);
  199. }
  200. /**
  201. * @return static
  202. */
  203. public static function htmlUnitWithJS()
  204. {
  205. $caps = new static([
  206. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
  207. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  208. ]);
  209. return $caps->setJavascriptEnabled(true);
  210. }
  211. /**
  212. * @return static
  213. */
  214. public static function internetExplorer()
  215. {
  216. return new static([
  217. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IE,
  218. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
  219. ]);
  220. }
  221. /**
  222. * @return static
  223. */
  224. public static function microsoftEdge()
  225. {
  226. return new static([
  227. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::MICROSOFT_EDGE,
  228. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
  229. ]);
  230. }
  231. /**
  232. * @return static
  233. */
  234. public static function iphone()
  235. {
  236. return new static([
  237. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPHONE,
  238. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
  239. ]);
  240. }
  241. /**
  242. * @return static
  243. */
  244. public static function ipad()
  245. {
  246. return new static([
  247. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPAD,
  248. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
  249. ]);
  250. }
  251. /**
  252. * @return static
  253. */
  254. public static function opera()
  255. {
  256. return new static([
  257. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::OPERA,
  258. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  259. ]);
  260. }
  261. /**
  262. * @return static
  263. */
  264. public static function safari()
  265. {
  266. return new static([
  267. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::SAFARI,
  268. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  269. ]);
  270. }
  271. /**
  272. * @return static
  273. */
  274. public static function phantomjs()
  275. {
  276. return new static([
  277. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS,
  278. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  279. ]);
  280. }
  281. /**
  282. * @param string $key
  283. * @param mixed $value
  284. * @return DesiredCapabilities
  285. */
  286. private function set($key, $value)
  287. {
  288. $this->capabilities[$key] = $value;
  289. return $this;
  290. }
  291. /**
  292. * @param string $key
  293. * @param mixed $default
  294. * @return mixed
  295. */
  296. private function get($key, $default = null)
  297. {
  298. return isset($this->capabilities[$key])
  299. ? $this->capabilities[$key]
  300. : $default;
  301. }
  302. }