DesiredCapabilities.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. * @return bool Whether javascript is enabled.
  109. */
  110. public function isJavascriptEnabled()
  111. {
  112. return $this->get(WebDriverCapabilityType::JAVASCRIPT_ENABLED, false);
  113. }
  114. /**
  115. * This is a htmlUnit-only option.
  116. *
  117. * @param bool $enabled
  118. * @throws Exception
  119. * @return DesiredCapabilities
  120. * @see https://code.google.com/p/selenium/wiki/DesiredCapabilities#Read-write_capabilities
  121. */
  122. public function setJavascriptEnabled($enabled)
  123. {
  124. $browser = $this->getBrowserName();
  125. if ($browser && $browser !== WebDriverBrowserType::HTMLUNIT) {
  126. throw new Exception(
  127. 'isJavascriptEnable() is a htmlunit-only option. ' .
  128. 'See https://code.google.com/p/selenium/wiki/DesiredCapabilities#Read-write_capabilities.'
  129. );
  130. }
  131. $this->set(WebDriverCapabilityType::JAVASCRIPT_ENABLED, $enabled);
  132. return $this;
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function toArray()
  138. {
  139. if (isset($this->capabilities[ChromeOptions::CAPABILITY]) &&
  140. $this->capabilities[ChromeOptions::CAPABILITY] instanceof ChromeOptions
  141. ) {
  142. $this->capabilities[ChromeOptions::CAPABILITY] =
  143. $this->capabilities[ChromeOptions::CAPABILITY]->toArray();
  144. }
  145. if (isset($this->capabilities[FirefoxDriver::PROFILE]) &&
  146. $this->capabilities[FirefoxDriver::PROFILE] instanceof FirefoxProfile
  147. ) {
  148. $this->capabilities[FirefoxDriver::PROFILE] =
  149. $this->capabilities[FirefoxDriver::PROFILE]->encode();
  150. }
  151. return $this->capabilities;
  152. }
  153. /**
  154. * @param string $key
  155. * @param mixed $value
  156. * @return DesiredCapabilities
  157. */
  158. private function set($key, $value)
  159. {
  160. $this->capabilities[$key] = $value;
  161. return $this;
  162. }
  163. /**
  164. * @param string $key
  165. * @param mixed $default
  166. * @return mixed
  167. */
  168. private function get($key, $default = null)
  169. {
  170. return isset($this->capabilities[$key])
  171. ? $this->capabilities[$key]
  172. : $default;
  173. }
  174. /**
  175. * @return DesiredCapabilities
  176. */
  177. public static function android()
  178. {
  179. return new static([
  180. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::ANDROID,
  181. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANDROID,
  182. ]);
  183. }
  184. /**
  185. * @return DesiredCapabilities
  186. */
  187. public static function chrome()
  188. {
  189. return new static([
  190. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::CHROME,
  191. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  192. ]);
  193. }
  194. /**
  195. * @return DesiredCapabilities
  196. */
  197. public static function firefox()
  198. {
  199. $caps = new static([
  200. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::FIREFOX,
  201. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  202. ]);
  203. // disable the "Reader View" help tooltip, which can hide elements in the window.document
  204. $profile = new FirefoxProfile();
  205. $profile->setPreference(FirefoxPreferences::READER_PARSE_ON_LOAD_ENABLED, false);
  206. $caps->setCapability(FirefoxDriver::PROFILE, $profile);
  207. return $caps;
  208. }
  209. /**
  210. * @return DesiredCapabilities
  211. */
  212. public static function htmlUnit()
  213. {
  214. return new static([
  215. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
  216. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  217. ]);
  218. }
  219. /**
  220. * @return DesiredCapabilities
  221. */
  222. public static function htmlUnitWithJS()
  223. {
  224. $caps = new static([
  225. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
  226. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  227. ]);
  228. return $caps->setJavascriptEnabled(true);
  229. }
  230. /**
  231. * @return DesiredCapabilities
  232. */
  233. public static function internetExplorer()
  234. {
  235. return new static([
  236. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IE,
  237. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
  238. ]);
  239. }
  240. /**
  241. * @return DesiredCapabilities
  242. */
  243. public static function microsoftEdge()
  244. {
  245. return new static([
  246. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::MICROSOFT_EDGE,
  247. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
  248. ]);
  249. }
  250. /**
  251. * @return DesiredCapabilities
  252. */
  253. public static function iphone()
  254. {
  255. return new static([
  256. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPHONE,
  257. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
  258. ]);
  259. }
  260. /**
  261. * @return DesiredCapabilities
  262. */
  263. public static function ipad()
  264. {
  265. return new static([
  266. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPAD,
  267. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
  268. ]);
  269. }
  270. /**
  271. * @return DesiredCapabilities
  272. */
  273. public static function opera()
  274. {
  275. return new static([
  276. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::OPERA,
  277. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  278. ]);
  279. }
  280. /**
  281. * @return DesiredCapabilities
  282. */
  283. public static function safari()
  284. {
  285. return new static([
  286. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::SAFARI,
  287. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  288. ]);
  289. }
  290. /**
  291. * @return DesiredCapabilities
  292. */
  293. public static function phantomjs()
  294. {
  295. return new static([
  296. WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS,
  297. WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
  298. ]);
  299. }
  300. }