Path: blob/trunk/javascript/selenium-webdriver/lib/capabilities.js
2884 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'use strict'1819/**20* @fileoverview Defines types related to describing the capabilities of a21* WebDriver session.22*/2324const Symbols = require('./symbols')2526/**27* Recognized browser names.28* @enum {string}29*/30const Browser = {31CHROME: 'chrome',32EDGE: 'MicrosoftEdge',33FIREFOX: 'firefox',34INTERNET_EXPLORER: 'internet explorer',35SAFARI: 'safari',36}3738/**39* Strategies for waiting for [document readiness] after a navigation40* event.41*42* [document readiness]: https://html.spec.whatwg.org/#current-document-readiness43*44* @enum {string}45*/46const PageLoadStrategy = {47/**48* Indicates WebDriver should not wait on the document readiness state after a49* navigation event.50*/51NONE: 'none',5253/**54* Indicates WebDriver should wait for the document readiness state to55* become "interactive" after navigation.56*/57EAGER: 'eager',5859/**60* Indicates WebDriver should wait for the document readiness state to61* be "complete" after navigation. This is the default page loading strategy.62*/63NORMAL: 'normal',64}6566/**67* Common platform names. These platforms are not explicitly defined by the68* WebDriver spec, however, their use is encouraged for interoperability.69*70* @enum {string}71* @see <https://w3c.github.io/webdriver/webdriver-spec.html>72*/73const Platform = {74LINUX: 'linux',75MAC: 'mac',76WINDOWS: 'windows',77}7879/**80* Record object defining the timeouts that apply to certain WebDriver actions.81*82* @record83*/84function Timeouts() {}8586/**87* Defines when, in milliseconds, to interrupt a script that is being88* {@linkplain ./webdriver.IWebDriver#executeScript evaluated}.89* @type {number}90*/91Timeouts.prototype.script9293/**94* The timeout, in milliseconds, to apply to navigation events along with the95* {@link PageLoadStrategy}.96* @type {number}97*/98Timeouts.prototype.pageLoad99100/**101* The maximum amount of time, in milliseconds, to spend attempting to102* {@linkplain ./webdriver.IWebDriver#findElement locate} an element on the103* current page.104* @type {number}105*/106Timeouts.prototype.implicit107108/**109* The possible default actions a WebDriver session can take to respond to110* unhandled user prompts (`window.alert()`, `window.confirm()`, and111* `window.prompt()`).112*113* @enum {string}114*/115const UserPromptHandler = {116/** All prompts should be silently accepted. */117ACCEPT: 'accept',118/** All prompts should be silently dismissed. */119DISMISS: 'dismiss',120/**121* All prompts should be automatically accepted, but an error should be122* returned to the next (or currently executing) WebDriver command.123*/124ACCEPT_AND_NOTIFY: 'accept and notify',125/**126* All prompts should be automatically dismissed, but an error should be127* returned to the next (or currently executing) WebDriver command.128*/129DISMISS_AND_NOTIFY: 'dismiss and notify',130/** All prompts should be left unhandled. */131IGNORE: 'ignore',132}133134/**135* The standard WebDriver capability keys.136*137* @enum {string}138* @see <https://w3c.github.io/webdriver/webdriver-spec.html#capabilities>139*/140const Capability = {141/**142* Indicates whether a WebDriver session implicitly trusts otherwise untrusted143* and self-signed TLS certificates during navigation.144*/145ACCEPT_INSECURE_TLS_CERTS: 'acceptInsecureCerts',146147/**148* The browser name. Common browser names are defined in the149* {@link ./capabilities.Browser Browser} enum.150*/151BROWSER_NAME: 'browserName',152153/** Identifies the browser version. */154BROWSER_VERSION: 'browserVersion',155156/**157* Key for the logging driver logging preferences.158* The browser name. Common browser names are defined in the159* {@link ./capabilities.Browser Browser} enum.160*/161LOGGING_PREFS: 'goog:loggingPrefs',162163/**164* Defines the session's165* {@linkplain ./capabilities.PageLoadStrategy page loading strategy}.166*/167PAGE_LOAD_STRATEGY: 'pageLoadStrategy',168169/**170* Identifies the operating system of the endpoint node. Common values171* recognized by the most WebDriver server implementations are predefined in172* the {@link ./capabilities.Platform Platform} enum.173*/174PLATFORM_NAME: 'platformName',175176/**177* Describes the proxy configuration to use for a new WebDriver session.178*/179PROXY: 'proxy',180181/**182* Indicates whether the remote end supports all of the window resizing and183* positioning commands:184*185* - {@linkplain ./webdriver.Window#getRect Window.getRect()}186* - {@linkplain ./webdriver.Window#setRect Window.setRect()}187* - {@linkplain ./webdriver.Window#maximize Window.maximize()}188* - {@linkplain ./webdriver.Window#minimize Window.minimize()}189* - {@linkplain ./webdriver.Window#fullscreen Window.fullscreen()}190*/191SET_WINDOW_RECT: 'setWindowRect',192193/**194* Describes the {@linkplain ./capabilities.Timeouts timeouts} imposed on195* certain session operations.196*/197TIMEOUTS: 'timeouts',198199/**200* Defines how a WebDriver session should201* {@linkplain ./capabilities.UserPromptHandler respond} to unhandled user202* prompts.203*/204UNHANDLED_PROMPT_BEHAVIOR: 'unhandledPromptBehavior',205206/**207* Defines the current session’s strict file interactability.208* Used to upload a file when strict file interactability is on209*/210STRICT_FILE_INTERACTABILITY: 'strictFileInteractability',211212ENABLE_DOWNLOADS: 'se:downloadsEnabled',213}214215/**216* Converts a generic hash object to a map.217* @param {!Object<string, ?>} hash The hash object.218* @return {!Map<string, ?>} The converted map.219*/220function toMap(hash) {221let m = new Map()222for (let key in hash) {223if (Object.prototype.hasOwnProperty.call(hash, key)) {224m.set(key, hash[key])225}226}227return m228}229230/**231* Describes a set of capabilities for a WebDriver session.232*/233class Capabilities {234/**235* @param {(Capabilities|Map<string, ?>|Object)=} other Another set of236* capabilities to initialize this instance from.237*/238constructor(other = undefined) {239if (other instanceof Capabilities) {240other = other.map_241} else if (other && !(other instanceof Map)) {242other = toMap(other)243}244/** @private @const {!Map<string, ?>} */245this.map_ = new Map(other)246}247248/** @return {number} The number of capabilities set. */249get size() {250return this.map_.size251}252253/**254* @return {!Capabilities} A basic set of capabilities for Chrome.255*/256static chrome() {257return new Capabilities().setBrowserName(Browser.CHROME)258}259260/**261* @return {!Capabilities} A basic set of capabilities for Microsoft Edge.262*/263static edge() {264return new Capabilities().setBrowserName(Browser.EDGE)265}266267/**268* @return {!Capabilities} A basic set of capabilities for Firefox.269*/270static firefox() {271return new Capabilities().setBrowserName(Browser.FIREFOX).set('moz:debuggerAddress', true)272}273274/**275* @return {!Capabilities} A basic set of capabilities for Internet Explorer.276*/277static ie() {278return new Capabilities().setBrowserName(Browser.INTERNET_EXPLORER)279}280281/**282* @return {!Capabilities} A basic set of capabilities for Safari.283*/284static safari() {285return new Capabilities().setBrowserName(Browser.SAFARI)286}287288/**289* @return {!Object<string, ?>} The JSON representation of this instance.290* Note, the returned object may contain nested promised values.291* @suppress {checkTypes} Suppress [] access on a struct (state inherited from292* Map).293*/294[Symbols.serialize]() {295return serialize(this)296}297298/**299* @param {string} key the parameter key to get.300* @return {T} the stored parameter value.301* @template T302*/303get(key) {304return this.map_.get(key)305}306307/**308* @param {string} key the key to test.309* @return {boolean} whether this capability set has the specified key.310*/311has(key) {312return this.map_.has(key)313}314315/**316* @return {!Iterator<string>} an iterator of the keys set.317*/318keys() {319return this.map_.keys()320}321322/**323* Merges another set of capabilities into this instance.324* @param {!(Capabilities|Map<String, ?>|Object<string, ?>)} other The other325* set of capabilities to merge.326* @return {!Capabilities} A self reference.327*/328merge(other) {329if (other) {330let otherMap331if (other instanceof Capabilities) {332otherMap = other.map_333} else if (other instanceof Map) {334otherMap = other335} else {336otherMap = toMap(other)337}338otherMap.forEach((value, key) => {339this.set(key, value)340})341return this342} else {343throw new TypeError('no capabilities provided for merge')344}345}346347/**348* Deletes an entry from this set of capabilities.349*350* @param {string} key the capability key to delete.351*/352delete(key) {353this.map_.delete(key)354}355356/**357* @param {string} key The capability key.358* @param {*} value The capability value.359* @return {!Capabilities} A self reference.360* @throws {TypeError} If the `key` is not a string.361*/362set(key, value) {363if (typeof key !== 'string') {364throw new TypeError('Capability keys must be strings: ' + typeof key)365}366this.map_.set(key, value)367return this368}369370/**371* Sets whether a WebDriver session should implicitly accept self-signed, or372* other untrusted TLS certificates on navigation.373*374* @param {boolean} accept whether to accept insecure certs.375* @return {!Capabilities} a self reference.376*/377setAcceptInsecureCerts(accept) {378return this.set(Capability.ACCEPT_INSECURE_TLS_CERTS, accept)379}380381/**382* @return {boolean} whether the session is configured to accept insecure383* TLS certificates.384*/385getAcceptInsecureCerts() {386return this.get(Capability.ACCEPT_INSECURE_TLS_CERTS)387}388389/**390* Sets the name of the target browser.391*392* @param {(Browser|string)} name the browser name.393* @return {!Capabilities} a self reference.394*/395setBrowserName(name) {396return this.set(Capability.BROWSER_NAME, name)397}398399/**400* @return {(string|undefined)} the configured browser name, or undefined if401* not set.402*/403getBrowserName() {404return this.get(Capability.BROWSER_NAME)405}406407/**408* Sets the desired version of the target browser.409*410* @param {string} version the desired version.411* @return {!Capabilities} a self reference.412*/413setBrowserVersion(version) {414return this.set(Capability.BROWSER_VERSION, version)415}416417/**418* @return {(string|undefined)} the configured browser version, or undefined419* if not set.420*/421getBrowserVersion() {422return this.get(Capability.BROWSER_VERSION)423}424425/**426* Sets the desired page loading strategy for a new WebDriver session.427*428* @param {PageLoadStrategy} strategy the desired strategy.429* @return {!Capabilities} a self reference.430*/431setPageLoadStrategy(strategy) {432return this.set(Capability.PAGE_LOAD_STRATEGY, strategy)433}434435/**436* Returns the configured page load strategy.437*438* @return {(string|undefined)} the page load strategy.439*/440getPageLoadStrategy() {441return this.get(Capability.PAGE_LOAD_STRATEGY)442}443444/**445* Sets the target platform.446*447* @param {(Platform|string)} platform the target platform.448* @return {!Capabilities} a self reference.449*/450setPlatform(platform) {451return this.set(Capability.PLATFORM_NAME, platform)452}453454/**455* @return {(string|undefined)} the configured platform or undefined if not456* set.457*/458getPlatform() {459return this.get(Capability.PLATFORM_NAME)460}461462/**463* Sets the logging preferences. Preferences may be specified as a464* {@link ./logging.Preferences} instance, or as a map of log-type to465* log-level.466* @param {!(./logging.Preferences|Object<string>)} prefs The logging467* preferences.468* @return {!Capabilities} A self reference.469*/470setLoggingPrefs(prefs) {471return this.set(Capability.LOGGING_PREFS, prefs)472}473474/**475* Sets the proxy configuration for this instance.476* @param {proxy.Config} proxy The desired proxy configuration.477* @return {!Capabilities} A self reference.478*/479setProxy(proxy) {480return this.set(Capability.PROXY, proxy)481}482483/**484* @return {(proxy.Config|undefined)} the configured proxy settings, or485* undefined if not set.486*/487getProxy() {488return this.get(Capability.PROXY)489}490491/**492* Sets the default action to take with an unexpected alert before returning493* an error. If unspecified, WebDriver will default to494* {@link UserPromptHandler.DISMISS_AND_NOTIFY}.495*496* @param {?UserPromptHandler} behavior The way WebDriver should respond to497* unhandled user prompts.498* @return {!Capabilities} A self reference.499*/500setAlertBehavior(behavior) {501return this.set(Capability.UNHANDLED_PROMPT_BEHAVIOR, behavior)502}503504/**505* @return {(UserPromptHandler|undefined)} the behavior pattern for responding506* to unhandled user prompts, or undefined if not set.507*/508getAlertBehavior() {509return this.get(Capability.UNHANDLED_PROMPT_BEHAVIOR)510}511512/**513* Sets the boolean flag configuration for this instance.514*/515setStrictFileInteractability(strictFileInteractability) {516return this.set(Capability.STRICT_FILE_INTERACTABILITY, strictFileInteractability)517}518519enableDownloads() {520return this.set(Capability.ENABLE_DOWNLOADS, true)521}522}523524/**525* Serializes a capabilities object. This is defined as a standalone function526* so it may be type checked (where Capabilities[Symbols.serialize] has type527* checking disabled since it is defined with [] access on a struct).528*529* @param {!Capabilities} caps The capabilities to serialize.530* @return {!Object<string, ?>} The JSON representation of this instance.531* Note, the returned object may contain nested promised values.532*/533function serialize(caps) {534let ret = {}535for (let key of caps.keys()) {536let cap = caps.get(key)537if (cap !== undefined && cap !== null) {538ret[key] = cap539}540}541return ret542}543544// PUBLIC API545546module.exports = {547Browser,548Capabilities,549Capability,550PageLoadStrategy,551Platform,552Timeouts,553UserPromptHandler,554}555556557