UNPKG

3.96 kBJavaScriptView Raw
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/**
19 * @fileoverview Defines functions for configuring a webdriver proxy:
20 *
21 * const Capabilities = require('./capabilities').Capabilities;
22 *
23 * var capabilities = new Capabilities();
24 * capabilities.setProxy(proxy.manual({http: 'host:1234'});
25 */
26
27'use strict';
28
29var ProxyConfig = require('./capabilities').ProxyConfig;
30
31
32// PUBLIC API
33
34
35/**
36 * Configures WebDriver to bypass all browser proxies.
37 * @return {!ProxyConfig} A new proxy configuration object.
38 */
39exports.direct = function() {
40 return {proxyType: 'direct'};
41};
42
43
44/**
45 * Manually configures the browser proxy. The following options are
46 * supported:
47 *
48 * - `ftp`: Proxy host to use for FTP requests
49 * - `http`: Proxy host to use for HTTP requests
50 * - `https`: Proxy host to use for HTTPS requests
51 * - `bypass`: A list of hosts requests should directly connect to,
52 * bypassing any other proxies for that request. May be specified as a
53 * comma separated string, or a list of strings.
54 *
55 * Behavior is undefined for FTP, HTTP, and HTTPS requests if the
56 * corresponding key is omitted from the configuration options.
57 *
58 * @param {{ftp: (string|undefined),
59 * http: (string|undefined),
60 * https: (string|undefined),
61 * bypass: (string|!Array.<string>|undefined)}} options Proxy
62 * configuration options.
63 * @return {!ProxyConfig} A new proxy configuration object.
64 */
65exports.manual = function(options) {
66 // TODO(jleyba): Figure out why the Closure compiler does not think this is
67 // a ProxyConfig record without the cast.
68 return /** @type {!ProxyConfig} */({
69 proxyType: 'manual',
70 ftpProxy: options.ftp,
71 httpProxy: options.http,
72 sslProxy: options.https,
73 noProxy: Array.isArray(options.bypass) ?
74 options.bypass.join(',') : options.bypass
75 });
76};
77
78
79/**
80 * Creates a proxy configuration for a socks proxy.
81 *
82 * __Example:__
83 *
84 * const {Capabilities} = require('selenium-webdriver');
85 * const proxy = require('selenium-webdriver/lib/proxy');
86 *
87 * let capabilities = new Capabilities();
88 * capabilities.setProxy(proxy.socks('localhost:1234', 'bob', 'password'));
89 *
90 *
91 * @param {string} host The proxy host, in the form `hostname:port`.
92 * @param {string} username The user name to authenticate as.
93 * @param {string} password The password to authenticate with.
94 * @return {!ProxyConfig} A new proxy configuration object.
95 * @see https://en.wikipedia.org/wiki/SOCKS
96 */
97exports.socks = function(host, username, password) {
98 return /** @type {!ProxyConfig} */({
99 proxyType: 'manual',
100 socksProxy: host,
101 socksUsername: username,
102 socksPassword: password
103 });
104};
105
106
107/**
108 * Configures WebDriver to configure the browser proxy using the PAC file at
109 * the given URL.
110 * @param {string} url URL for the PAC proxy to use.
111 * @return {!ProxyConfig} A new proxy configuration object.
112 */
113exports.pac = function(url) {
114 return {
115 proxyType: 'pac',
116 proxyAutoconfigUrl: url
117 };
118};
119
120
121/**
122 * Configures WebDriver to use the current system's proxy.
123 * @return {!ProxyConfig} A new proxy configuration object.
124 */
125exports.system = function() {
126 return {proxyType: 'system'};
127};