Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/selenium-webdriver/chrome.js
1864 views
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 a {@linkplain Driver WebDriver} client for the Chrome
20
* web browser. Before using this module, you must download the latest
21
* [ChromeDriver release] and ensure it can be found on your system [PATH].
22
*
23
* There are three primary classes exported by this module:
24
*
25
* 1. {@linkplain ServiceBuilder}: configures the
26
* {@link selenium-webdriver/remote.DriverService remote.DriverService}
27
* that manages the [ChromeDriver] child process.
28
*
29
* 2. {@linkplain Options}: defines configuration options for each new Chrome
30
* session, such as which {@linkplain Options#setProxy proxy} to use,
31
* what {@linkplain Options#addExtensions extensions} to install, or
32
* what {@linkplain Options#addArguments command-line switches} to use when
33
* starting the browser.
34
*
35
* 3. {@linkplain Driver}: the WebDriver client; each new instance will control
36
* a unique browser session with a clean user profile (unless otherwise
37
* configured through the {@link Options} class).
38
*
39
* let chrome = require('selenium-webdriver/chrome');
40
* let {Builder} = require('selenium-webdriver');
41
*
42
* let driver = new Builder()
43
* .forBrowser('chrome')
44
* .setChromeOptions(new chrome.Options())
45
* .build();
46
*
47
* __Customizing the ChromeDriver Server__ <a id="custom-server"></a>
48
*
49
* By default, every Chrome session will use a single driver service, which is
50
* started the first time a {@link Driver} instance is created and terminated
51
* when this process exits. The default service will inherit its environment
52
* from the current process and direct all output to /dev/null. You may obtain
53
* a handle to this default service using
54
* {@link #getDefaultService getDefaultService()} and change its configuration
55
* with {@link #setDefaultService setDefaultService()}.
56
*
57
* You may also create a {@link Driver} with its own driver service. This is
58
* useful if you need to capture the server's log output for a specific session:
59
*
60
* let chrome = require('selenium-webdriver/chrome');
61
*
62
* let service = new chrome.ServiceBuilder()
63
* .loggingTo('/my/log/file.txt')
64
* .enableVerboseLogging()
65
* .build();
66
*
67
* let options = new chrome.Options();
68
* // configure browser options ...
69
*
70
* let driver = chrome.Driver.createSession(options, service);
71
*
72
* Users should only instantiate the {@link Driver} class directly when they
73
* need a custom driver service configuration (as shown above). For normal
74
* operation, users should start Chrome using the
75
* {@link selenium-webdriver.Builder}.
76
*
77
* __Working with Android__ <a id="android"></a>
78
*
79
* The [ChromeDriver][android] supports running tests on the Chrome browser as
80
* well as [WebView apps][webview] starting in Android 4.4 (KitKat). In order to
81
* work with Android, you must first start the adb
82
*
83
* adb start-server
84
*
85
* By default, adb will start on port 5037. You may change this port, but this
86
* will require configuring a [custom server](#custom-server) that will connect
87
* to adb on the {@linkplain ServiceBuilder#setAdbPort correct port}:
88
*
89
* let service = new chrome.ServiceBuilder()
90
* .setAdbPort(1234)
91
* build();
92
* // etc.
93
*
94
* The ChromeDriver may be configured to launch Chrome on Android using
95
* {@link Options#androidChrome()}:
96
*
97
* let driver = new Builder()
98
* .forBrowser('chrome')
99
* .setChromeOptions(new chrome.Options().androidChrome())
100
* .build();
101
*
102
* Alternatively, you can configure the ChromeDriver to launch an app with a
103
* Chrome-WebView by setting the {@linkplain Options#androidActivity
104
* androidActivity} option:
105
*
106
* let driver = new Builder()
107
* .forBrowser('chrome')
108
* .setChromeOptions(new chrome.Options()
109
* .androidPackage('com.example')
110
* .androidActivity('com.example.Activity'))
111
* .build();
112
*
113
* [Refer to the ChromeDriver site] for more information on using the
114
* [ChromeDriver with Android][android].
115
*
116
* [ChromeDriver]: https://chromedriver.chromium.org/
117
* [ChromeDriver release]: http://chromedriver.storage.googleapis.com/index.html
118
* [PATH]: http://en.wikipedia.org/wiki/PATH_%28variable%29
119
* [android]: https://chromedriver.chromium.org/getting-started/getting-started---android
120
* [webview]: https://developer.chrome.com/multidevice/webview/overview
121
*
122
* @module selenium-webdriver/chrome
123
*/
124
125
'use strict'
126
127
const { Browser } = require('./lib/capabilities')
128
const chromium = require('./chromium')
129
const CHROME_CAPABILITY_KEY = 'goog:chromeOptions'
130
131
/** @type {remote.DriverService} */
132
133
/**
134
* Creates {@link selenium-webdriver/remote.DriverService} instances that manage
135
* a [ChromeDriver](https://chromedriver.chromium.org/)
136
* server in a child process.
137
*/
138
class ServiceBuilder extends chromium.ServiceBuilder {
139
/**
140
* @param {string=} opt_exe Path to the server executable to use. If omitted,
141
* the builder will attempt to locate the chromedriver on the current
142
* PATH. If the chromedriver is not available in path, selenium-manager will
143
* download the chromedriver
144
* @throws {Error} If provided executable does not exist, or the chromedriver
145
* cannot be found on the PATH.
146
*/
147
constructor(opt_exe) {
148
super(opt_exe)
149
}
150
}
151
152
/**
153
* Class for managing ChromeDriver specific options.
154
*/
155
class Options extends chromium.Options {
156
/**
157
* Sets the path to the Chrome binary to use. On Mac OS X, this path should
158
* reference the actual Chrome executable, not just the application binary
159
* (e.g. "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome").
160
*
161
* The binary path be absolute or relative to the chromedriver server
162
* executable, but it must exist on the machine that will launch Chrome.
163
*
164
* @param {string} path The path to the Chrome binary to use.
165
* @return {!Options} A self reference.
166
*/
167
setChromeBinaryPath(path) {
168
return this.setBinaryPath(path)
169
}
170
171
/**
172
* Configures the ChromeDriver to launch Chrome on Android via adb. This
173
* function is shorthand for
174
* {@link #androidPackage options.androidPackage('com.android.chrome')}.
175
* @return {!Options} A self reference.
176
*/
177
androidChrome() {
178
return this.androidPackage('com.android.chrome')
179
}
180
181
/**
182
* Sets the path to Chrome's log file. This path should exist on the machine
183
* that will launch Chrome.
184
* @param {string} path Path to the log file to use.
185
* @return {!Options} A self reference.
186
*/
187
setChromeLogFile(path) {
188
return this.setBrowserLogFile(path)
189
}
190
191
/**
192
* Sets the directory to store Chrome minidumps in. This option is only
193
* supported when ChromeDriver is running on Linux.
194
* @param {string} path The directory path.
195
* @return {!Options} A self reference.
196
*/
197
setChromeMinidumpPath(path) {
198
return this.setBrowserMinidumpPath(path)
199
}
200
}
201
202
/**
203
* Creates a new WebDriver client for Chrome.
204
*/
205
class Driver extends chromium.Driver {
206
/**
207
* Creates a new session with the ChromeDriver.
208
*
209
* @param {(Capabilities|Options)=} opt_config The configuration options.
210
* @param {(remote.DriverService|http.Executor)=} opt_serviceExecutor Either
211
* a DriverService to use for the remote end, or a preconfigured executor
212
* for an externally managed endpoint. If neither is provided, the
213
* {@linkplain ##getDefaultService default service} will be used by
214
* default.
215
* @return {!Driver} A new driver instance.
216
*/
217
static createSession(opt_config, opt_serviceExecutor) {
218
let caps = opt_config || new Options()
219
return /** @type {!Driver} */ (super.createSession(caps, opt_serviceExecutor, 'goog', CHROME_CAPABILITY_KEY))
220
}
221
222
/**
223
* returns new instance chrome driver service
224
* @returns {remote.DriverService}
225
*/
226
static getDefaultService() {
227
return new ServiceBuilder().build()
228
}
229
}
230
231
Options.prototype.CAPABILITY_KEY = CHROME_CAPABILITY_KEY
232
Options.prototype.BROWSER_NAME_VALUE = Browser.CHROME
233
234
// PUBLIC API
235
module.exports = {
236
Driver,
237
Options,
238
ServiceBuilder,
239
}
240
241