DesiredCapabilities.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. * @todo Remove side-effects - not change ie. ChromeOptions::CAPABILITY from instance of ChromeOptions to an array
  138. * @return array
  139. */
  140. public function toArray()
  141. {
  142. if (isset($this->capabilities[ChromeOptions::CAPABILITY]) &&
  143. $this->capabilities[ChromeOptions::CAPABILITY] instanceof ChromeOptions
  144. ) {
  145. $this->capabilities[ChromeOptions::CAPABILITY] =
  146. $this->capabilities[ChromeOptions::CAPABILITY]->toArray();
  147. }
  148. if (isset($this->capabilities[FirefoxDriver::PROFILE]) &&
  149. $this->capabilities[FirefoxDriver::PROFILE] instanceof FirefoxProfile
  150. ) {
  151. $this->capabilities[FirefoxDriver::PROFILE] =
  152. $this->capabilities[FirefoxDriver::PROFILE]->encode();
  153. }
  154. return $this->capabilities;
  155. }
  156. /**
  157. * @return static
  158. */
  159. public static function android()
  160. {
  161. return new static([
  162. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::ANDROID,
  163. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANDROID,
  164. ]);
  165. }
  166. /**
  167. * @return static
  168. */
  169. public static function chrome()
  170. {
  171. return new static([
  172. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::CHROME,
  173. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  174. ]);
  175. }
  176. /**
  177. * @return static
  178. */
  179. public static function firefox()
  180. {
  181. $caps = new static([
  182. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::FIREFOX,
  183. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  184. ]);
  185. // disable the "Reader View" help tooltip, which can hide elements in the window.document
  186. $profile = new FirefoxProfile();
  187. $profile->setPreference(FirefoxPreferences::READER_PARSE_ON_LOAD_ENABLED, false);
  188. $caps->setCapability(FirefoxDriver::PROFILE, $profile);
  189. return $caps;
  190. }
  191. /**
  192. * @return static
  193. */
  194. public static function htmlUnit()
  195. {
  196. return new static([
  197. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
  198. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  199. ]);
  200. }
  201. /**
  202. * @return static
  203. */
  204. public static function htmlUnitWithJS()
  205. {
  206. $caps = new static([
  207. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
  208. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  209. ]);
  210. return $caps->setJavascriptEnabled(true);
  211. }
  212. /**
  213. * @return static
  214. */
  215. public static function internetExplorer()
  216. {
  217. return new static([
  218. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IE,
  219. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
  220. ]);
  221. }
  222. /**
  223. * @return static
  224. */
  225. public static function microsoftEdge()
  226. {
  227. return new static([
  228. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::MICROSOFT_EDGE,
  229. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
  230. ]);
  231. }
  232. /**
  233. * @return static
  234. */
  235. public static function iphone()
  236. {
  237. return new static([
  238. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPHONE,
  239. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
  240. ]);
  241. }
  242. /**
  243. * @return static
  244. */
  245. public static function ipad()
  246. {
  247. return new static([
  248. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPAD,
  249. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
  250. ]);
  251. }
  252. /**
  253. * @return static
  254. */
  255. public static function opera()
  256. {
  257. return new static([
  258. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::OPERA,
  259. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  260. ]);
  261. }
  262. /**
  263. * @return static
  264. */
  265. public static function safari()
  266. {
  267. return new static([
  268. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::SAFARI,
  269. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  270. ]);
  271. }
  272. /**
  273. * @return static
  274. */
  275. public static function phantomjs()
  276. {
  277. return new static([
  278. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS,
  279. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  280. ]);
  281. }
  282. /**
  283. * @param string $key
  284. * @param mixed $value
  285. * @return DesiredCapabilities
  286. */
  287. private function set($key, $value)
  288. {
  289. $this->capabilities[$key] = $value;
  290. return $this;
  291. }
  292. /**
  293. * @param string $key
  294. * @param mixed $default
  295. * @return mixed
  296. */
  297. private function get($key, $default = null)
  298. {
  299. return isset($this->capabilities[$key])
  300. ? $this->capabilities[$key]
  301. : $default;
  302. }
  303. }