From 60b2cff330c3a4c954c8ba6b2e274a9767805c2f Mon Sep 17 00:00:00 2001 From: titusfortner Date: Tue, 28 Sep 2021 23:37:45 -0500 Subject: [PATCH] [rb] add android specific methods to Chrome, Edge and Firefox --- rb/lib/selenium/webdriver/chrome/options.rb | 25 +++++++++++- rb/lib/selenium/webdriver/firefox/options.rb | 25 +++++++++++- .../selenium/webdriver/chrome/options_spec.rb | 40 ++++++++++++++++++- .../selenium/webdriver/edge/options_spec.rb | 40 ++++++++++++++++++- .../webdriver/firefox/options_spec.rb | 40 ++++++++++++++++++- 5 files changed, 165 insertions(+), 5 deletions(-) diff --git a/rb/lib/selenium/webdriver/chrome/options.rb b/rb/lib/selenium/webdriver/chrome/options.rb index 4d206506d783e..dea76855bc21d 100755 --- a/rb/lib/selenium/webdriver/chrome/options.rb +++ b/rb/lib/selenium/webdriver/chrome/options.rb @@ -37,7 +37,11 @@ class Options < WebDriver::Options minidump_path: 'minidumpPath', emulation: 'mobileEmulation', perf_logging_prefs: 'perfLoggingPrefs', - window_types: 'windowTypes'}.freeze + window_types: 'windowTypes', + android_package: 'androidPackage', + android_activity: 'androidActivity', + android_device_serial: 'androidDeviceSerial', + android_use_running_app: 'androidUseRunningApp'}.freeze # NOTE: special handling of 'extensions' to validate when set instead of when used attr_reader :extensions @@ -193,6 +197,25 @@ def add_emulation(**opts) @options[:emulation] = opts end + # + # Enables mobile browser use on Android. + # + # @see https://chromedriver.chromium.org/getting-started/getting-started---android + # + # @param [String] package The package name of the Chrome or WebView app. + # @param [String] serial_number The device serial number on which to launch the Chrome or WebView app. + # @param [String] use_running_app When true uses an already-running Chrome or WebView app, + # instead of launching the app with a clear data directory. + # @param [String] activity Name of the Activity hosting the WebView (Not available on Chrome Apps). + # + + def enable_android(package: 'com.android.chrome', serial_number: nil, use_running_app: nil, activity: nil) + @options[:android_package] = package + @options[:android_activity] = activity unless activity.nil? + @options[:android_device_serial] = serial_number unless serial_number.nil? + @options[:android_use_running_app] = use_running_app unless use_running_app.nil? + end + private def enable_logging(browser_options) diff --git a/rb/lib/selenium/webdriver/firefox/options.rb b/rb/lib/selenium/webdriver/firefox/options.rb index 0780b5ac1b972..7e6f75631b21a 100644 --- a/rb/lib/selenium/webdriver/firefox/options.rb +++ b/rb/lib/selenium/webdriver/firefox/options.rb @@ -29,7 +29,11 @@ class Options < WebDriver::Options CAPABILITIES = {binary: 'binary', args: 'args', log: 'log', - prefs: 'prefs'}.freeze + prefs: 'prefs', + android_package: 'androidPackage', + android_activity: 'androidActivity', + android_device_serial: 'androidDeviceSerial', + android_intent_arguments: 'androidIntentArguments'}.freeze BROWSER = 'firefox' # NOTE: special handling of 'profile' to validate when set instead of when used @@ -131,6 +135,25 @@ def log_level=(level) @options[:log] = {level: level} end + # + # Enables mobile browser use on Android. + # + # @see https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions#android + # + # @param [String] package The package name of the Chrome or WebView app. + # @param [String] serial_number The serial number of the device on which to launch the application. + # If not specified and multiple devices are attached, an error will be returned. + # @param [String] activity The fully qualified class name of the activity to be launched. + # @param [Array] intent_arguments Arguments to launch the intent with. + # + + def enable_android(package: 'org.mozilla.firefox', serial_number: nil, activity: nil, intent_arguments: nil) + @options[:android_package] = package + @options[:android_activity] = activity unless activity.nil? + @options[:android_device_serial] = serial_number unless serial_number.nil? + @options[:android_intent_arguments] = intent_arguments unless intent_arguments.nil? + end + private def process_browser_options(browser_options) diff --git a/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb b/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb index 015821067c029..cbda7424b5e18 100644 --- a/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb +++ b/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb @@ -53,6 +53,10 @@ module Chrome minidump_path: 'linux/only', perf_logging_prefs: {enable_network: true}, window_types: %w[normal devtools], + android_package: 'package', + android_activity: 'activity', + android_device_serial: '123', + android_use_running_app: true, 'custom:options': {foo: 'bar'}) expect(opts.args).to eq(%w[foo bar]) @@ -77,6 +81,10 @@ module Chrome expect(opts.strict_file_interactability).to eq(true) expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1) expect(opts.set_window_rect).to eq(false) + expect(opts.android_package).to eq('package') + expect(opts.android_activity).to eq('activity') + expect(opts.android_device_serial).to eq('123') + expect(opts.android_use_running_app).to eq(true) expect(opts.options[:'custom:options']).to eq(foo: 'bar') end end @@ -212,6 +220,28 @@ module Chrome end end + describe '#enable_android' do + it 'adds default android settings' do + options.enable_android + + expect(options.android_package).to eq('com.android.chrome') + expect(options.android_activity).to be_nil + expect(options.android_device_serial).to be_nil + expect(options.android_use_running_app).to be_nil + end + + it 'accepts parameters' do + options.enable_android(package: 'foo', + serial_number: '123', + activity: 'qualified', + use_running_app: true) + expect(options.android_package).to eq('foo') + expect(options.android_activity).to eq('qualified') + expect(options.android_device_serial).to eq('123') + expect(options.android_use_running_app).to eq(true) + end + end + describe '#as_json' do it 'returns empty options by default' do expect(options.as_json).to eq("browserName" => "chrome", "goog:chromeOptions" => {}) @@ -269,6 +299,10 @@ module Chrome minidump_path: 'linux/only', perf_logging_prefs: {'enable_network': true}, window_types: %w[normal devtools], + android_package: 'package', + android_activity: 'activity', + android_device_serial: '123', + android_use_running_app: true, 'custom:options': {foo: 'bar'}) key = 'goog:chromeOptions' @@ -301,7 +335,11 @@ module Chrome 'excludeSwitches' => %w[foobar barfoo], 'minidumpPath' => 'linux/only', 'perfLoggingPrefs' => {'enableNetwork' => true}, - 'windowTypes' => %w[normal devtools]}) + 'windowTypes' => %w[normal devtools], + 'androidPackage' => 'package', + 'androidActivity' => 'activity', + 'androidDeviceSerial' => '123', + 'androidUseRunningApp' => true}) end end end diff --git a/rb/spec/unit/selenium/webdriver/edge/options_spec.rb b/rb/spec/unit/selenium/webdriver/edge/options_spec.rb index 244418e9622cb..8a6e161c4f9ec 100644 --- a/rb/spec/unit/selenium/webdriver/edge/options_spec.rb +++ b/rb/spec/unit/selenium/webdriver/edge/options_spec.rb @@ -53,6 +53,10 @@ module Edge minidump_path: 'linux/only', perf_logging_prefs: {enable_network: true}, window_types: %w[normal devtools], + android_package: 'package', + android_activity: 'activity', + android_device_serial: '123', + android_use_running_app: true, 'custom:options': {foo: 'bar'}) expect(opts.args.to_a).to eq(%w[foo bar]) @@ -77,6 +81,10 @@ module Edge expect(opts.strict_file_interactability).to eq(true) expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1) expect(opts.set_window_rect).to eq(false) + expect(opts.android_package).to eq('package') + expect(opts.android_activity).to eq('activity') + expect(opts.android_device_serial).to eq('123') + expect(opts.android_use_running_app).to eq(true) expect(opts.options[:'custom:options']).to eq(foo: 'bar') end end @@ -168,6 +176,28 @@ module Edge end end + describe '#enable_android' do + it 'adds default android settings' do + options.enable_android + + expect(options.android_package).to eq('com.android.chrome') + expect(options.android_activity).to be_nil + expect(options.android_device_serial).to be_nil + expect(options.android_use_running_app).to be_nil + end + + it 'accepts parameters' do + options.enable_android(package: 'foo', + serial_number: '123', + activity: 'qualified', + use_running_app: true) + expect(options.android_package).to eq('foo') + expect(options.android_activity).to eq('qualified') + expect(options.android_device_serial).to eq('123') + expect(options.android_use_running_app).to eq(true) + end + end + describe '#as_json' do it 'returns empty options by default' do expect(options.as_json).to eq("browserName" => "MicrosoftEdge", "ms:edgeOptions" => {}) @@ -211,6 +241,10 @@ module Edge minidump_path: 'linux/only', perf_logging_prefs: {'enable_network': true}, window_types: %w[normal devtools], + android_package: 'package', + android_activity: 'activity', + android_device_serial: '123', + android_use_running_app: true, 'custom:options': {foo: 'bar'}) key = 'ms:edgeOptions' @@ -239,7 +273,11 @@ module Edge 'excludeSwitches' => %w[foobar barfoo], 'minidumpPath' => 'linux/only', 'perfLoggingPrefs' => {'enableNetwork' => true}, - 'windowTypes' => %w[normal devtools]}) + 'windowTypes' => %w[normal devtools], + 'androidPackage' => 'package', + 'androidActivity' => 'activity', + 'androidDeviceSerial' => '123', + 'androidUseRunningApp' => true}) end end end diff --git a/rb/spec/unit/selenium/webdriver/firefox/options_spec.rb b/rb/spec/unit/selenium/webdriver/firefox/options_spec.rb index 7f333e02b745c..991cf46691d90 100644 --- a/rb/spec/unit/selenium/webdriver/firefox/options_spec.rb +++ b/rb/spec/unit/selenium/webdriver/firefox/options_spec.rb @@ -46,6 +46,10 @@ module Firefox foo: 'bar', profile: profile, log_level: :debug, + android_package: 'package', + android_activity: 'activity', + android_device_serial: '123', + android_intent_arguments: %w[foo bar], 'custom:options': {foo: 'bar'}) expect(opts.args.to_a).to eq(%w[foo bar]) @@ -63,6 +67,10 @@ module Firefox expect(opts.strict_file_interactability).to eq(true) expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1) expect(opts.set_window_rect).to eq(false) + expect(opts.android_package).to eq('package') + expect(opts.android_activity).to eq('activity') + expect(opts.android_device_serial).to eq('123') + expect(opts.android_intent_arguments).to eq(%w[foo bar]) expect(opts.options[:'custom:options']).to eq(foo: 'bar') end end @@ -143,6 +151,28 @@ module Firefox end end + describe '#enable_android' do + it 'adds default android settings' do + options.enable_android + + expect(options.android_package).to eq('org.mozilla.firefox') + expect(options.android_activity).to be_nil + expect(options.android_device_serial).to be_nil + expect(options.android_intent_arguments).to be_nil + end + + it 'accepts parameters' do + options.enable_android(package: 'foo', + serial_number: '123', + activity: 'qualified', + intent_arguments: %w[foo bar]) + expect(options.android_package).to eq('foo') + expect(options.android_activity).to eq('qualified') + expect(options.android_device_serial).to eq('123') + expect(options.android_intent_arguments).to eq(%w[foo bar]) + end + end + describe '#as_json' do it 'returns empty options by default' do expect(options.as_json).to eq("browserName" => "firefox", "moz:firefoxOptions" => {}) @@ -176,6 +206,10 @@ module Firefox foo: 'bar', profile: profile, log_level: :debug, + android_package: 'package', + android_activity: 'activity', + android_device_serial: '123', + android_intent_arguments: %w[foo bar], 'custom:options': {foo: 'bar'}) key = 'moz:firefoxOptions' @@ -196,7 +230,11 @@ module Firefox 'prefs' => {'foo' => 'bar'}, 'profile' => 'encoded_profile', 'log' => {'level' => 'debug'}, - 'foo' => 'bar'}) + 'foo' => 'bar', + 'androidPackage' => 'package', + 'androidActivity' => 'activity', + 'androidDeviceSerial' => '123', + 'androidIntentArguments' => %w[foo bar]}) end end end # Options