Path: blob/trunk/javascript/selenium-webdriver/chrome.js
1864 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 a {@linkplain Driver WebDriver} client for the Chrome19* web browser. Before using this module, you must download the latest20* [ChromeDriver release] and ensure it can be found on your system [PATH].21*22* There are three primary classes exported by this module:23*24* 1. {@linkplain ServiceBuilder}: configures the25* {@link selenium-webdriver/remote.DriverService remote.DriverService}26* that manages the [ChromeDriver] child process.27*28* 2. {@linkplain Options}: defines configuration options for each new Chrome29* session, such as which {@linkplain Options#setProxy proxy} to use,30* what {@linkplain Options#addExtensions extensions} to install, or31* what {@linkplain Options#addArguments command-line switches} to use when32* starting the browser.33*34* 3. {@linkplain Driver}: the WebDriver client; each new instance will control35* a unique browser session with a clean user profile (unless otherwise36* configured through the {@link Options} class).37*38* let chrome = require('selenium-webdriver/chrome');39* let {Builder} = require('selenium-webdriver');40*41* let driver = new Builder()42* .forBrowser('chrome')43* .setChromeOptions(new chrome.Options())44* .build();45*46* __Customizing the ChromeDriver Server__ <a id="custom-server"></a>47*48* By default, every Chrome session will use a single driver service, which is49* started the first time a {@link Driver} instance is created and terminated50* when this process exits. The default service will inherit its environment51* from the current process and direct all output to /dev/null. You may obtain52* a handle to this default service using53* {@link #getDefaultService getDefaultService()} and change its configuration54* with {@link #setDefaultService setDefaultService()}.55*56* You may also create a {@link Driver} with its own driver service. This is57* useful if you need to capture the server's log output for a specific session:58*59* let chrome = require('selenium-webdriver/chrome');60*61* let service = new chrome.ServiceBuilder()62* .loggingTo('/my/log/file.txt')63* .enableVerboseLogging()64* .build();65*66* let options = new chrome.Options();67* // configure browser options ...68*69* let driver = chrome.Driver.createSession(options, service);70*71* Users should only instantiate the {@link Driver} class directly when they72* need a custom driver service configuration (as shown above). For normal73* operation, users should start Chrome using the74* {@link selenium-webdriver.Builder}.75*76* __Working with Android__ <a id="android"></a>77*78* The [ChromeDriver][android] supports running tests on the Chrome browser as79* well as [WebView apps][webview] starting in Android 4.4 (KitKat). In order to80* work with Android, you must first start the adb81*82* adb start-server83*84* By default, adb will start on port 5037. You may change this port, but this85* will require configuring a [custom server](#custom-server) that will connect86* to adb on the {@linkplain ServiceBuilder#setAdbPort correct port}:87*88* let service = new chrome.ServiceBuilder()89* .setAdbPort(1234)90* build();91* // etc.92*93* The ChromeDriver may be configured to launch Chrome on Android using94* {@link Options#androidChrome()}:95*96* let driver = new Builder()97* .forBrowser('chrome')98* .setChromeOptions(new chrome.Options().androidChrome())99* .build();100*101* Alternatively, you can configure the ChromeDriver to launch an app with a102* Chrome-WebView by setting the {@linkplain Options#androidActivity103* androidActivity} option:104*105* let driver = new Builder()106* .forBrowser('chrome')107* .setChromeOptions(new chrome.Options()108* .androidPackage('com.example')109* .androidActivity('com.example.Activity'))110* .build();111*112* [Refer to the ChromeDriver site] for more information on using the113* [ChromeDriver with Android][android].114*115* [ChromeDriver]: https://chromedriver.chromium.org/116* [ChromeDriver release]: http://chromedriver.storage.googleapis.com/index.html117* [PATH]: http://en.wikipedia.org/wiki/PATH_%28variable%29118* [android]: https://chromedriver.chromium.org/getting-started/getting-started---android119* [webview]: https://developer.chrome.com/multidevice/webview/overview120*121* @module selenium-webdriver/chrome122*/123124'use strict'125126const { Browser } = require('./lib/capabilities')127const chromium = require('./chromium')128const CHROME_CAPABILITY_KEY = 'goog:chromeOptions'129130/** @type {remote.DriverService} */131132/**133* Creates {@link selenium-webdriver/remote.DriverService} instances that manage134* a [ChromeDriver](https://chromedriver.chromium.org/)135* server in a child process.136*/137class ServiceBuilder extends chromium.ServiceBuilder {138/**139* @param {string=} opt_exe Path to the server executable to use. If omitted,140* the builder will attempt to locate the chromedriver on the current141* PATH. If the chromedriver is not available in path, selenium-manager will142* download the chromedriver143* @throws {Error} If provided executable does not exist, or the chromedriver144* cannot be found on the PATH.145*/146constructor(opt_exe) {147super(opt_exe)148}149}150151/**152* Class for managing ChromeDriver specific options.153*/154class Options extends chromium.Options {155/**156* Sets the path to the Chrome binary to use. On Mac OS X, this path should157* reference the actual Chrome executable, not just the application binary158* (e.g. "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome").159*160* The binary path be absolute or relative to the chromedriver server161* executable, but it must exist on the machine that will launch Chrome.162*163* @param {string} path The path to the Chrome binary to use.164* @return {!Options} A self reference.165*/166setChromeBinaryPath(path) {167return this.setBinaryPath(path)168}169170/**171* Configures the ChromeDriver to launch Chrome on Android via adb. This172* function is shorthand for173* {@link #androidPackage options.androidPackage('com.android.chrome')}.174* @return {!Options} A self reference.175*/176androidChrome() {177return this.androidPackage('com.android.chrome')178}179180/**181* Sets the path to Chrome's log file. This path should exist on the machine182* that will launch Chrome.183* @param {string} path Path to the log file to use.184* @return {!Options} A self reference.185*/186setChromeLogFile(path) {187return this.setBrowserLogFile(path)188}189190/**191* Sets the directory to store Chrome minidumps in. This option is only192* supported when ChromeDriver is running on Linux.193* @param {string} path The directory path.194* @return {!Options} A self reference.195*/196setChromeMinidumpPath(path) {197return this.setBrowserMinidumpPath(path)198}199}200201/**202* Creates a new WebDriver client for Chrome.203*/204class Driver extends chromium.Driver {205/**206* Creates a new session with the ChromeDriver.207*208* @param {(Capabilities|Options)=} opt_config The configuration options.209* @param {(remote.DriverService|http.Executor)=} opt_serviceExecutor Either210* a DriverService to use for the remote end, or a preconfigured executor211* for an externally managed endpoint. If neither is provided, the212* {@linkplain ##getDefaultService default service} will be used by213* default.214* @return {!Driver} A new driver instance.215*/216static createSession(opt_config, opt_serviceExecutor) {217let caps = opt_config || new Options()218return /** @type {!Driver} */ (super.createSession(caps, opt_serviceExecutor, 'goog', CHROME_CAPABILITY_KEY))219}220221/**222* returns new instance chrome driver service223* @returns {remote.DriverService}224*/225static getDefaultService() {226return new ServiceBuilder().build()227}228}229230Options.prototype.CAPABILITY_KEY = CHROME_CAPABILITY_KEY231Options.prototype.BROWSER_NAME_VALUE = Browser.CHROME232233// PUBLIC API234module.exports = {235Driver,236Options,237ServiceBuilder,238}239240241