Path: blob/trunk/rb/lib/selenium/webdriver/chromium/options.rb
1856 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module Chromium22class Options < WebDriver::Options23attr_accessor :profile, :logging_prefs2425# see: http://chromedriver.chromium.org/capabilities26CAPABILITIES = {args: 'args',27binary: 'binary',28local_state: 'localState',29prefs: 'prefs',30detach: 'detach',31debugger_address: 'debuggerAddress',32exclude_switches: 'excludeSwitches',33minidump_path: 'minidumpPath',34emulation: 'mobileEmulation',35perf_logging_prefs: 'perfLoggingPrefs',36window_types: 'windowTypes',37android_package: 'androidPackage',38android_activity: 'androidActivity',39android_device_serial: 'androidDeviceSerial',40android_use_running_app: 'androidUseRunningApp'}.freeze4142# NOTE: special handling of 'extensions' to validate when set instead of when used43attr_reader :extensions4445# Create a new Options instance.46#47# @example48# options = Selenium::WebDriver::Chrome::Options.new(args: ['start-maximized', 'user-data-dir=/tmp/temp_profile'])49# driver = Selenium::WebDriver.for(:chrome, options: options)50#51# @param [Profile] profile An instance of a Chrome::Profile Class52# @param [Hash] opts the pre-defined options to create the Chrome::Options with53# @option opts [Array] encoded_extensions List of extensions that do not need to be Base64 encoded54# @option opts [Array<String>] args List of command-line arguments to use when starting Chrome55# @option opts [String] binary Path to the Chrome executable to use56# @option opts [Hash] prefs A hash with each entry consisting of the name of the preference and its value57# @option opts [Array<String>] extensions A list of paths to (.crx) Chrome extensions to install on startup58# @option opts [Hash] options A hash for raw options59# @option opts [Hash] emulation A hash for raw emulation options60# @option opts [Hash] local_state A hash for the Local State file in the user data folder61# @option opts [Boolean] detach whether browser is closed when the driver is sent the quit command62# @option opts [String] debugger_address address of a Chrome debugger server to connect to63# @option opts [Array<String>] exclude_switches command line switches to exclude64# @option opts [String] minidump_path Directory to store Chrome minidumps (linux only)65# @option opts [Hash] perf_logging_prefs A hash for performance logging preferences66# @option opts [Array<String>] window_types A list of window types to appear in the list of window handles67#6869def initialize(profile: nil, **)70super(**)7172@profile = profile7374@options = {args: [],75prefs: {},76emulation: {},77extensions: [],78local_state: {},79exclude_switches: [],80perf_logging_prefs: {},81window_types: []}.merge(@options)8283@logging_prefs = options.delete(:logging_prefs) || {}84@encoded_extensions = @options.delete(:encoded_extensions) || []85@extensions = []86@options.delete(:extensions).each { |ext| validate_extension(ext) }87end8889#90# Add an extension by local path.91#92# @example93# options = Selenium::WebDriver::Chrome::Options.new94# options.add_extension('/path/to/extension.crx')95#96# @param [String] path The local path to the .crx file97#9899def add_extension(path)100validate_extension(path)101end102103#104# Add an extension by local path.105#106# @example107# extensions = ['/path/to/extension.crx', '/path/to/other.crx']108# options = Selenium::WebDriver::Chrome::Options.new109# options.extensions = extensions110#111# @param [Array<String>] extensions A list of paths to (.crx) Chrome extensions to install on startup112#113114def extensions=(extensions)115extensions.each { |ext| validate_extension(ext) }116end117118#119# Add an extension by Base64-encoded string.120#121# @example122# options = Selenium::WebDriver::Chrome::Options.new123# options.add_encoded_extension(encoded_string)124#125# @param [String] encoded The Base64-encoded string of the .crx file126#127128def add_encoded_extension(encoded)129@encoded_extensions << encoded130end131132#133# Add a command-line argument to use when starting Chrome.134#135# @example Start Chrome maximized136# options = Selenium::WebDriver::Chrome::Options.new137# options.add_argument('start-maximized')138#139# @param [String] arg The command-line argument to add140#141142def add_argument(arg)143@options[:args] << arg144end145146#147# Add a preference that is only applied to the user profile in use.148#149# @example Set the default homepage150# options = Selenium::WebDriver::Chrome::Options.new151# options.add_preference('homepage', 'http://www.seleniumhq.com/')152#153# @param [String] name Key of the preference154# @param [Boolean, String, Integer] value Value of the preference155#156157def add_preference(name, value)158@options[:prefs][name] = value159end160161#162# Add emulation device information163#164# see: http://chromedriver.chromium.org/mobile-emulation165#166# @example Start Chrome in mobile emulation mode by device name167# options = Selenium::WebDriver::Chrome::Options.new168# options.add_emulation(device_name: 'iPhone 6')169#170# @example Start Chrome in mobile emulation mode by device metrics171# options = Selenium::WebDriver::Chrome::Options.new172# options.add_emulation(device_metrics: {width: 400, height: 800, pixelRatio: 1, touch: true})173#174# @param [Hash] opts the pre-defined options for adding mobile emulation values175# @option opts [String] :device_name A valid device name from the Chrome DevTools Emulation panel176# @option opts [Hash] :device_metrics Hash containing width, height, pixelRatio, touch177# @option opts [String] :user_agent Full user agent178#179180def add_emulation(**opts)181@options[:emulation] = opts182end183184#185# Enables mobile browser use on Android.186#187# @see https://chromedriver.chromium.org/getting-started/getting-started---android188#189# @param [String] package The package name of the Chrome or WebView app.190# @param [String] serial_number The device serial number on which to launch the Chrome or WebView app.191# @param [String] use_running_app When true uses an already-running Chrome or WebView app,192# instead of launching the app with a clear data directory.193# @param [String] activity Name of the Activity hosting the WebView (Not available on Chrome Apps).194#195196def enable_android(package: 'com.android.chrome', serial_number: nil, use_running_app: nil, activity: nil)197@options[:android_package] = package198@options[:android_activity] = activity unless activity.nil?199@options[:android_device_serial] = serial_number unless serial_number.nil?200@options[:android_use_running_app] = use_running_app unless use_running_app.nil?201end202203protected204205def process_browser_options(browser_options)206enable_logging(browser_options) unless @logging_prefs.empty?207208options = browser_options[self.class::KEY]209options['binary'] ||= binary_path if binary_path210211if @profile212options['args'] ||= []213options['args'] << "--user-data-dir=#{@profile.directory}"214end215216return if (@encoded_extensions + @extensions).empty?217218options['extensions'] = @encoded_extensions + @extensions.map { |ext| encode_extension(ext) }219end220221def binary_path222Chrome.path223end224225def encode_extension(path)226File.open(path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }227end228229def validate_extension(path)230raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.file?(path)231raise Error::WebDriverError, "file was not an extension #{path.inspect}" unless File.extname(path) == '.crx'232233@extensions << path234end235236def camelize?(key)237!%w[localState prefs].include?(key)238end239end # Options240end # Chromium241end # WebDriver242end # Selenium243244245