Path: blob/trunk/javascript/selenium-webdriver/lib/proxy.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/**18* @fileoverview Defines functions for configuring a webdriver proxy:19*20* const proxy = require('selenium-webdriver/proxy');21* const {Capabilities} = require('selenium-webdriver');22*23* let capabilities = new Capabilities();24* capabilities.setProxy(proxy.manual({http: 'host:1234'});25*/2627'use strict'2829/**30* Supported {@linkplain Config proxy configuration} types.31*32* @enum {string}33*/34const Type = {35AUTODETECT: 'autodetect',36DIRECT: 'direct',37MANUAL: 'manual',38PAC: 'pac',39SYSTEM: 'system',40}4142/**43* Describes how a proxy should be configured for a WebDriver session.44* @record45*/46function Config() {}4748/**49* The proxy type.50* @type {Type}51*/52Config.prototype.proxyType5354/**55* Describes how to configure a PAC proxy.56* @record57* @extends {Config}58*/59function PacConfig() {}6061/**62* URL for the PAC file to use.63*64* @type {string}65*/66PacConfig.prototype.proxyAutoconfigUrl6768/**69* Record object that defines a manual proxy configuration. Manual70* configurations can be easily created using either the71* {@link ./proxy.manual proxy.manual()} or {@link ./proxy.socks proxy.socks()}72* factory method.73*74* @record75* @extends {Config}76*/77function ManualConfig() {}7879/**80* The proxy host for FTP requests.81*82* @type {(string|undefined)}83*/84ManualConfig.prototype.ftpProxy8586/**87* The proxy host for HTTP requests.88*89* @type {(string|undefined)}90*/91ManualConfig.prototype.httpProxy9293/**94* An array of hosts which should bypass all proxies.95*96* @type {(Array<string>|undefined)}97*/98ManualConfig.prototype.noProxy99100/**101* The proxy host for HTTPS requests.102*103* @type {(string|undefined)}104*/105ManualConfig.prototype.sslProxy106107/**108* Defines the host and port for the SOCKS proxy to use.109*110* @type {(number|undefined)}111*/112ManualConfig.prototype.socksProxy113114/**115* Defines the SOCKS proxy version. Must be a number in the range [0, 255].116*117* @type {(number|undefined)}118*/119ManualConfig.prototype.socksVersion120121// PUBLIC API122123/** @const */ exports.Config = Config124/** @const */ exports.ManualConfig = ManualConfig125/** @const */ exports.PacConfig = PacConfig126/** @const */ exports.Type = Type127128/**129* Configures WebDriver to bypass all browser proxies.130* @return {!Config} A new proxy configuration object.131*/132function direct() {133return { proxyType: Type.DIRECT }134}135136/**137* Manually configures the browser proxy. The following options are138* supported:139*140* - `ftp`: Proxy host to use for FTP requests141* - `http`: Proxy host to use for HTTP requests142* - `https`: Proxy host to use for HTTPS requests143* - `bypass`: A list of hosts requests should directly connect to,144* bypassing any other proxies for that request. May be specified as a145* comma separated string, or a list of strings.146*147* Behavior is undefined for FTP, HTTP, and HTTPS requests if the148* corresponding key is omitted from the configuration options.149*150* @param {{ftp: (string|undefined),151* http: (string|undefined),152* https: (string|undefined),153* bypass: (Array<string>|undefined)}} options Proxy154* configuration options.155* @return {!ManualConfig} A new proxy configuration object.156*/157function manual({ ftp, http, https, bypass }) {158if (ftp !== undefined) {159console.warn('ftpProxy is deprecated and will be removed in the future')160}161return {162proxyType: Type.MANUAL,163ftpProxy: ftp,164httpProxy: http,165sslProxy: https,166noProxy: bypass,167}168}169170/**171* Creates a proxy configuration for a socks proxy.172*173* __Example:__174*175* const {Capabilities} = require('selenium-webdriver');176* const proxy = require('selenium-webdriver/lib/proxy');177*178* let capabilities = new Capabilities();179* capabilities.setProxy(proxy.socks('localhost:1234'));180*181* // Or, to include authentication.182* capabilities.setProxy(proxy.socks('bob:password@localhost:1234'));183*184*185* @param {string} socksProxy The proxy host, in the form `hostname:port`.186* @param {number=} socksVersion The SOCKS proxy version.187* @return {!ManualConfig} A new proxy configuration object.188* @see https://en.wikipedia.org/wiki/SOCKS189*/190function socks(socksProxy, socksVersion = undefined) {191return /** @type {!Config} */ ({192proxyType: Type.MANUAL,193socksProxy,194socksVersion,195})196}197198/**199* Configures WebDriver to configure the browser proxy using the PAC file at200* the given URL.201* @param {string} proxyAutoconfigUrl URL for the PAC proxy to use.202* @return {!PacConfig} A new proxy configuration object.203*/204function pac(proxyAutoconfigUrl) {205return { proxyType: Type.PAC, proxyAutoconfigUrl }206}207208/**209* Configures WebDriver to use the current system's proxy.210* @return {!Config} A new proxy configuration object.211*/212function system() {213return { proxyType: Type.SYSTEM }214}215216// PUBLIC API217218module.exports = {219system,220pac,221socks,222manual,223direct,224}225226227