Path: blob/trunk/javascript/webdriver/capabilities.js
2867 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Defines the webdriver.Capabilities class.19*/2021goog.provide('webdriver.Browser');22goog.provide('webdriver.Capabilities');23goog.provide('webdriver.Capability');24goog.provide('webdriver.ProxyConfig');2526goog.require('webdriver.logging');27282930/**31* Recognized browser names.32* @enum {string}33* @suppress {lintChecks}34*/35webdriver.Browser = {36ANDROID: 'android',37CHROME: 'chrome',38FIREFOX: 'firefox',39IE: 'internet explorer',40INTERNET_EXPLORER: 'internet explorer',41EDGE: 'MicrosoftEdge',42IPAD: 'iPad',43IPHONE: 'iPhone',44OPERA: 'opera',45PHANTOM_JS: 'phantomjs',46SAFARI: 'safari',47HTMLUNIT: 'htmlunit'48};49505152/**53* Describes how a proxy should be configured for a WebDriver session.54* Proxy configuration object, as defined by the WebDriver wire protocol.55* @typedef {(56* {proxyType: string}|57* {proxyType: string,58* proxyAutoconfigUrl: string}|59* {proxyType: string,60* ftpProxy: string,61* httpProxy: string,62* sslProxy: string,63* noProxy: string})}64*/65webdriver.ProxyConfig;66676869/**70* Common webdriver capability keys.71* @enum {string}72*/73webdriver.Capability = {7475/**76* Indicates whether a driver should accept all SSL certs by default. This77* capability only applies when requesting a new session. To query whether78* a driver can handle insecure SSL certs, see {@link #SECURE_SSL}.79*/80ACCEPT_SSL_CERTS: 'acceptSslCerts',818283/**84* The browser name. Common browser names are defined in the85* {@link webdriver.Browser} enum.86*/87BROWSER_NAME: 'browserName',8889/**90* Defines how elements should be scrolled into the viewport for interaction.91* This capability will be set to zero (0) if elements are aligned with the92* top of the viewport, or one (1) if aligned with the bottom. The default93* behavior is to align with the top of the viewport.94*/95ELEMENT_SCROLL_BEHAVIOR: 'elementScrollBehavior',9697/**98* Whether the driver is capable of handling modal alerts (e.g. alert,99* confirm, prompt). To define how a driver <i>should</i> handle alerts,100* use {@link #UNEXPECTED_ALERT_BEHAVIOR}.101*/102HANDLES_ALERTS: 'handlesAlerts',103104/**105* Key for the logging driver logging preferences.106*/107LOGGING_PREFS: 'loggingPrefs',108109/**110* Whether this session generates native events when simulating user input.111*/112NATIVE_EVENTS: 'nativeEvents',113114/**115* Describes the platform the browser is running on. Will be one of116* ANDROID, IOS, LINUX, MAC, UNIX, or WINDOWS. When <i>requesting</i> a117* session, ANY may be used to indicate no platform preference (this is118* semantically equivalent to omitting the platform capability).119*/120PLATFORM: 'platform',121122/**123* Describes the proxy configuration to use for a new WebDriver session.124*/125PROXY: 'proxy',126127/** Whether the driver supports changing the browser's orientation. */128ROTATABLE: 'rotatable',129130/**131* Whether a driver is only capable of handling secure SSL certs. To request132* that a driver accept insecure SSL certs by default, use133* {@link #ACCEPT_SSL_CERTS}.134*/135SECURE_SSL: 'secureSsl',136137/** Whether the driver supports manipulating the app cache. */138SUPPORTS_APPLICATION_CACHE: 'applicationCacheEnabled',139140/** Whether the driver supports locating elements with CSS selectors. */141SUPPORTS_CSS_SELECTORS: 'cssSelectorsEnabled',142143/** Whether the browser supports JavaScript. */144SUPPORTS_JAVASCRIPT: 'javascriptEnabled',145146/** Whether the driver supports controlling the browser's location info. */147SUPPORTS_LOCATION_CONTEXT: 'locationContextEnabled',148149/** Whether the driver supports taking screenshots. */150TAKES_SCREENSHOT: 'takesScreenshot',151152/**153* Defines how the driver should handle unexpected alerts. The value should154* be one of "accept", "dismiss", or "ignore.155*/156UNEXPECTED_ALERT_BEHAVIOR: 'unexpectedAlertBehavior',157158/** Defines the browser version. */159VERSION: 'version'160};161162163164/**165* @param {(webdriver.Capabilities|Object)=} opt_other Another set of166* capabilities to merge into this instance.167* @constructor168*/169webdriver.Capabilities = function(opt_other) {170171/** @private {!Object.<string, ?>} */172this.caps_ = {};173174if (opt_other) {175this.merge(opt_other);176}177};178179180/**181* @return {!webdriver.Capabilities} A basic set of capabilities for Android.182*/183webdriver.Capabilities.android = function() {184return new webdriver.Capabilities().185set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.ANDROID).186set(webdriver.Capability.PLATFORM, 'ANDROID');187};188189190/**191* @return {!webdriver.Capabilities} A basic set of capabilities for Chrome.192*/193webdriver.Capabilities.chrome = function() {194return new webdriver.Capabilities().195set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.CHROME);196};197198199/**200* @return {!webdriver.Capabilities} A basic set of capabilities for Firefox.201*/202webdriver.Capabilities.firefox = function() {203return new webdriver.Capabilities().204set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.FIREFOX);205};206207208/**209* @return {!webdriver.Capabilities} A basic set of capabilities for210* Internet Explorer.211*/212webdriver.Capabilities.ie = function() {213return new webdriver.Capabilities().214set(webdriver.Capability.BROWSER_NAME,215webdriver.Browser.INTERNET_EXPLORER).216set(webdriver.Capability.PLATFORM, 'WINDOWS');217};218219/**220* @return {!webdriver.Capabilities} A basic set of capabilities for221* Microsoft Edge.222*/223webdriver.Capabilities.edge = function() {224return new webdriver.Capabilities().225set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.EDGE);226};227228229/**230* @return {!webdriver.Capabilities} A basic set of capabilities for iPad.231*/232webdriver.Capabilities.ipad = function() {233return new webdriver.Capabilities().234set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.IPAD).235set(webdriver.Capability.PLATFORM, 'MAC');236};237238239/**240* @return {!webdriver.Capabilities} A basic set of capabilities for iPhone.241*/242webdriver.Capabilities.iphone = function() {243return new webdriver.Capabilities().244set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.IPHONE).245set(webdriver.Capability.PLATFORM, 'MAC');246};247248249/**250* @return {!webdriver.Capabilities} A basic set of capabilities for Opera.251*/252webdriver.Capabilities.opera = function() {253return new webdriver.Capabilities().254set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.OPERA);255};256257/**258* @return {!webdriver.Capabilities} A basic set of capabilities for259* PhantomJS.260*/261webdriver.Capabilities.phantomjs = function() {262return new webdriver.Capabilities().263set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.PHANTOM_JS);264};265266267/**268* @return {!webdriver.Capabilities} A basic set of capabilities for Safari.269*/270webdriver.Capabilities.safari = function() {271return new webdriver.Capabilities().272set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.SAFARI).273set(webdriver.Capability.PLATFORM, 'MAC');274};275276277/**278* @return {!webdriver.Capabilities} A basic set of capabilities for HTMLUnit.279*/280webdriver.Capabilities.htmlunit = function() {281return new webdriver.Capabilities().282set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.HTMLUNIT);283};284285286/**287* @return {!webdriver.Capabilities} A basic set of capabilities for HTMLUnit288* with enabled Javascript.289*/290webdriver.Capabilities.htmlunitwithjs = function() {291return new webdriver.Capabilities().292set(webdriver.Capability.BROWSER_NAME, webdriver.Browser.HTMLUNIT).293set(webdriver.Capability.SUPPORTS_JAVASCRIPT, true);294};295296297/**298* @return {!Object.<string, ?>} The JSON representation of this instance. Note,299* the returned object may contain nested promises that are promised values.300*/301webdriver.Capabilities.prototype.serialize = function() {302return this.caps_;303};304305306/**307* Merges another set of capabilities into this instance. Any duplicates in308* the provided set will override those already set on this instance.309* @param {!(webdriver.Capabilities|Object)} other The capabilities to310* merge into this instance.311* @return {!webdriver.Capabilities} A self reference.312*/313webdriver.Capabilities.prototype.merge = function(other) {314var caps = other instanceof webdriver.Capabilities ?315other.caps_ : other;316for (var key in caps) {317if (caps.hasOwnProperty(key)) {318this.set(key, caps[key]);319}320}321return this;322};323324325/**326* @param {string} key The capability to set.327* @param {*} value The capability value. Capability values must be JSON328* serializable. Pass `null` to unset the capability.329* @return {!webdriver.Capabilities} A self reference.330*/331webdriver.Capabilities.prototype.set = function(key, value) {332if (goog.isDefAndNotNull(value)) {333this.caps_[key] = value;334} else {335delete this.caps_[key];336}337return this;338};339340341/**342* @param {string} key The capability to return.343* @return {*} The capability with the given key, or `null` if it has344* not been set.345*/346webdriver.Capabilities.prototype.get = function(key) {347var val = null;348if (this.caps_.hasOwnProperty(key)) {349val = this.caps_[key];350}351return goog.isDefAndNotNull(val) ? val : null;352};353354355/**356* @param {string} key The capability to check.357* @return {boolean} Whether the specified capability is set.358*/359webdriver.Capabilities.prototype.has = function(key) {360return !!this.get(key);361};362363364/**365* Sets the logging preferences. Preferences may be specified as a366* {@link webdriver.logging.Preferences} instance, or a as a map of log-type to367* log-level.368* @param {!(webdriver.logging.Preferences|Object.<string, string>)} prefs The369* logging preferences.370* @return {!webdriver.Capabilities} A self reference.371*/372webdriver.Capabilities.prototype.setLoggingPrefs = function(prefs) {373return this.set(webdriver.Capability.LOGGING_PREFS, prefs);374};375376377/**378* Sets the proxy configuration for this instance.379* @param {webdriver.ProxyConfig} proxy The desired proxy configuration.380* @return {!webdriver.Capabilities} A self reference.381*/382webdriver.Capabilities.prototype.setProxy = function(proxy) {383return this.set(webdriver.Capability.PROXY, proxy);384};385386387/**388* Sets whether native events should be used.389* @param {boolean} enabled Whether to enable native events.390* @return {!webdriver.Capabilities} A self reference.391*/392webdriver.Capabilities.prototype.setEnableNativeEvents = function(enabled) {393return this.set(webdriver.Capability.NATIVE_EVENTS, enabled);394};395396397/**398* Sets how elements should be scrolled into view for interaction.399* @param {number} behavior The desired scroll behavior: either 0 to align with400* the top of the viewport or 1 to align with the bottom.401* @return {!webdriver.Capabilities} A self reference.402*/403webdriver.Capabilities.prototype.setScrollBehavior = function(behavior) {404return this.set(webdriver.Capability.ELEMENT_SCROLL_BEHAVIOR, behavior);405};406407408/**409* Sets the default action to take with an unexpected alert before returning410* an error.411* @param {string} behavior The desired behavior; should be "accept", "dismiss",412* or "ignore". Defaults to "dismiss".413* @return {!webdriver.Capabilities} A self reference.414*/415webdriver.Capabilities.prototype.setAlertBehavior = function(behavior) {416return this.set(webdriver.Capability.UNEXPECTED_ALERT_BEHAVIOR, behavior);417};418419420