|
| 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 | + |
| 16 | +class DesiredCapabilities implements WebDriverCapabilities { |
| 17 | + |
| 18 | + private $capabilities; |
| 19 | + |
| 20 | + public function __construct(array $capabilities = array()) { |
| 21 | + $this->capabilities = $capabilities; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @return string The name of the browser. |
| 26 | + */ |
| 27 | + public function getBrowserName() { |
| 28 | + return $this->get(WebDriverCapabilityType::BROWSER_NAME, ''); |
| 29 | + } |
| 30 | + |
| 31 | + public function setBrowserName($browser_name) { |
| 32 | + $this->set(WebDriverCapabilityType::BROWSER_NAME, $browser_name); |
| 33 | + return $this; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return string The version of the browser. |
| 38 | + */ |
| 39 | + public function getVersion() { |
| 40 | + return $this->get(WebDriverCapabilityType::VERSION, ''); |
| 41 | + } |
| 42 | + |
| 43 | + public function setVersion($version) { |
| 44 | + $this->set(WebDriverCapabilityType::VERSION, $version); |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return mixed The value of a capability. |
| 50 | + */ |
| 51 | + public function getCapability($name) { |
| 52 | + $this->get($name); |
| 53 | + } |
| 54 | + |
| 55 | + public function setCapability($name, $value) { |
| 56 | + $this->set($name, $value); |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return string The name of the platform. |
| 62 | + */ |
| 63 | + public function getPlatform() { |
| 64 | + return $this->get(WebDriverCapabilityType::PLATFORM, ''); |
| 65 | + } |
| 66 | + |
| 67 | + public function setPlatform($platform) { |
| 68 | + $this->set(WebDriverCapabilityType::PLATFORM, $platform); |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * @return bool Whether the value is not null and not false. |
| 75 | + */ |
| 76 | + public function is($capability_name) { |
| 77 | + return (bool)$this->get($capability_name); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return bool Whether javascript is enabled. |
| 82 | + */ |
| 83 | + public function isJavascriptEnabled() { |
| 84 | + return $this->get(WebDriverCapabilityType::JAVASCRIPT_ENABLED, false); |
| 85 | + } |
| 86 | + |
| 87 | + public function setJavascriptEnabled($enabled) { |
| 88 | + $this->set(WebDriverCapabilityType::JAVASCRIPT_ENABLED, $enabled); |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + public function toArray() { |
| 93 | + return $this->capabilities; |
| 94 | + } |
| 95 | + |
| 96 | + private function set($key, $value) { |
| 97 | + $this->capabilities[$key] = $value; |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + private function get($key, $default = null) { |
| 102 | + return isset($this->capabilities[$key]) |
| 103 | + ? $this->capabilities[$key] |
| 104 | + : $default; |
| 105 | + } |
| 106 | + |
| 107 | + public static function android() { |
| 108 | + return new DesiredCapabilities(array( |
| 109 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::ANDROID, |
| 110 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANDROID, |
| 111 | + )); |
| 112 | + } |
| 113 | + |
| 114 | + public static function chrome() { |
| 115 | + return new DesiredCapabilities(array( |
| 116 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::CHROME, |
| 117 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY, |
| 118 | + )); |
| 119 | + } |
| 120 | + |
| 121 | + public static function firefox() { |
| 122 | + return new DesiredCapabilities(array( |
| 123 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::FIREFOX, |
| 124 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY, |
| 125 | + )); |
| 126 | + } |
| 127 | + |
| 128 | + public static function htmlUnit() { |
| 129 | + return new DesiredCapabilities(array( |
| 130 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT, |
| 131 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY, |
| 132 | + )); |
| 133 | + } |
| 134 | + |
| 135 | + public static function htmlUnitWithJS() { |
| 136 | + $caps = new DesiredCapabilities(array( |
| 137 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT, |
| 138 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY, |
| 139 | + )); |
| 140 | + return $caps->setJavascriptEnabled(true); |
| 141 | + } |
| 142 | + |
| 143 | + public static function internetExplorer() { |
| 144 | + return new DesiredCapabilities(array( |
| 145 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IE, |
| 146 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS, |
| 147 | + )); |
| 148 | + } |
| 149 | + |
| 150 | + public static function iphone() { |
| 151 | + return new DesiredCapabilities(array( |
| 152 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPHONE, |
| 153 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC, |
| 154 | + )); |
| 155 | + } |
| 156 | + |
| 157 | + public static function ipad() { |
| 158 | + return new DesiredCapabilities(array( |
| 159 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPAD, |
| 160 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC, |
| 161 | + )); |
| 162 | + } |
| 163 | + |
| 164 | + public static function opera() { |
| 165 | + return new DesiredCapabilities(array( |
| 166 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::OPERA, |
| 167 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY, |
| 168 | + )); |
| 169 | + } |
| 170 | + |
| 171 | + public static function safari() { |
| 172 | + return new DesiredCapabilities(array( |
| 173 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::SAFARI, |
| 174 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY, |
| 175 | + )); |
| 176 | + } |
| 177 | + |
| 178 | + public static function phantomjs() { |
| 179 | + return new DesiredCapabilities(array( |
| 180 | + WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS, |
| 181 | + WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY, |
| 182 | + )); |
| 183 | + } |
| 184 | +} |
0 commit comments