Skip to content

Commit

Permalink
[rb] add android specific methods to Chrome, Edge and Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 29, 2021
1 parent c2e6b58 commit 60b2cff
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 5 deletions.
25 changes: 24 additions & 1 deletion rb/lib/selenium/webdriver/chrome/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 24 additions & 1 deletion rb/lib/selenium/webdriver/firefox/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 39 additions & 1 deletion rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -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" => {})
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
40 changes: 39 additions & 1 deletion rb/spec/unit/selenium/webdriver/edge/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -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" => {})
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
40 changes: 39 additions & 1 deletion rb/spec/unit/selenium/webdriver/firefox/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -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" => {})
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit 60b2cff

Please sign in to comment.