Skip to content

Commit 60b2cff

Browse files
committed
[rb] add android specific methods to Chrome, Edge and Firefox
1 parent c2e6b58 commit 60b2cff

File tree

5 files changed

+165
-5
lines changed

5 files changed

+165
-5
lines changed

rb/lib/selenium/webdriver/chrome/options.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ class Options < WebDriver::Options
3737
minidump_path: 'minidumpPath',
3838
emulation: 'mobileEmulation',
3939
perf_logging_prefs: 'perfLoggingPrefs',
40-
window_types: 'windowTypes'}.freeze
40+
window_types: 'windowTypes',
41+
android_package: 'androidPackage',
42+
android_activity: 'androidActivity',
43+
android_device_serial: 'androidDeviceSerial',
44+
android_use_running_app: 'androidUseRunningApp'}.freeze
4145

4246
# NOTE: special handling of 'extensions' to validate when set instead of when used
4347
attr_reader :extensions
@@ -193,6 +197,25 @@ def add_emulation(**opts)
193197
@options[:emulation] = opts
194198
end
195199

200+
#
201+
# Enables mobile browser use on Android.
202+
#
203+
# @see https://chromedriver.chromium.org/getting-started/getting-started---android
204+
#
205+
# @param [String] package The package name of the Chrome or WebView app.
206+
# @param [String] serial_number The device serial number on which to launch the Chrome or WebView app.
207+
# @param [String] use_running_app When true uses an already-running Chrome or WebView app,
208+
# instead of launching the app with a clear data directory.
209+
# @param [String] activity Name of the Activity hosting the WebView (Not available on Chrome Apps).
210+
#
211+
212+
def enable_android(package: 'com.android.chrome', serial_number: nil, use_running_app: nil, activity: nil)
213+
@options[:android_package] = package
214+
@options[:android_activity] = activity unless activity.nil?
215+
@options[:android_device_serial] = serial_number unless serial_number.nil?
216+
@options[:android_use_running_app] = use_running_app unless use_running_app.nil?
217+
end
218+
196219
private
197220

198221
def enable_logging(browser_options)

rb/lib/selenium/webdriver/firefox/options.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class Options < WebDriver::Options
2929
CAPABILITIES = {binary: 'binary',
3030
args: 'args',
3131
log: 'log',
32-
prefs: 'prefs'}.freeze
32+
prefs: 'prefs',
33+
android_package: 'androidPackage',
34+
android_activity: 'androidActivity',
35+
android_device_serial: 'androidDeviceSerial',
36+
android_intent_arguments: 'androidIntentArguments'}.freeze
3337
BROWSER = 'firefox'
3438

3539
# NOTE: special handling of 'profile' to validate when set instead of when used
@@ -131,6 +135,25 @@ def log_level=(level)
131135
@options[:log] = {level: level}
132136
end
133137

138+
#
139+
# Enables mobile browser use on Android.
140+
#
141+
# @see https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions#android
142+
#
143+
# @param [String] package The package name of the Chrome or WebView app.
144+
# @param [String] serial_number The serial number of the device on which to launch the application.
145+
# If not specified and multiple devices are attached, an error will be returned.
146+
# @param [String] activity The fully qualified class name of the activity to be launched.
147+
# @param [Array] intent_arguments Arguments to launch the intent with.
148+
#
149+
150+
def enable_android(package: 'org.mozilla.firefox', serial_number: nil, activity: nil, intent_arguments: nil)
151+
@options[:android_package] = package
152+
@options[:android_activity] = activity unless activity.nil?
153+
@options[:android_device_serial] = serial_number unless serial_number.nil?
154+
@options[:android_intent_arguments] = intent_arguments unless intent_arguments.nil?
155+
end
156+
134157
private
135158

136159
def process_browser_options(browser_options)

rb/spec/unit/selenium/webdriver/chrome/options_spec.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ module Chrome
5353
minidump_path: 'linux/only',
5454
perf_logging_prefs: {enable_network: true},
5555
window_types: %w[normal devtools],
56+
android_package: 'package',
57+
android_activity: 'activity',
58+
android_device_serial: '123',
59+
android_use_running_app: true,
5660
'custom:options': {foo: 'bar'})
5761

5862
expect(opts.args).to eq(%w[foo bar])
@@ -77,6 +81,10 @@ module Chrome
7781
expect(opts.strict_file_interactability).to eq(true)
7882
expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1)
7983
expect(opts.set_window_rect).to eq(false)
84+
expect(opts.android_package).to eq('package')
85+
expect(opts.android_activity).to eq('activity')
86+
expect(opts.android_device_serial).to eq('123')
87+
expect(opts.android_use_running_app).to eq(true)
8088
expect(opts.options[:'custom:options']).to eq(foo: 'bar')
8189
end
8290
end
@@ -212,6 +220,28 @@ module Chrome
212220
end
213221
end
214222

223+
describe '#enable_android' do
224+
it 'adds default android settings' do
225+
options.enable_android
226+
227+
expect(options.android_package).to eq('com.android.chrome')
228+
expect(options.android_activity).to be_nil
229+
expect(options.android_device_serial).to be_nil
230+
expect(options.android_use_running_app).to be_nil
231+
end
232+
233+
it 'accepts parameters' do
234+
options.enable_android(package: 'foo',
235+
serial_number: '123',
236+
activity: 'qualified',
237+
use_running_app: true)
238+
expect(options.android_package).to eq('foo')
239+
expect(options.android_activity).to eq('qualified')
240+
expect(options.android_device_serial).to eq('123')
241+
expect(options.android_use_running_app).to eq(true)
242+
end
243+
end
244+
215245
describe '#as_json' do
216246
it 'returns empty options by default' do
217247
expect(options.as_json).to eq("browserName" => "chrome", "goog:chromeOptions" => {})
@@ -269,6 +299,10 @@ module Chrome
269299
minidump_path: 'linux/only',
270300
perf_logging_prefs: {'enable_network': true},
271301
window_types: %w[normal devtools],
302+
android_package: 'package',
303+
android_activity: 'activity',
304+
android_device_serial: '123',
305+
android_use_running_app: true,
272306
'custom:options': {foo: 'bar'})
273307

274308
key = 'goog:chromeOptions'
@@ -301,7 +335,11 @@ module Chrome
301335
'excludeSwitches' => %w[foobar barfoo],
302336
'minidumpPath' => 'linux/only',
303337
'perfLoggingPrefs' => {'enableNetwork' => true},
304-
'windowTypes' => %w[normal devtools]})
338+
'windowTypes' => %w[normal devtools],
339+
'androidPackage' => 'package',
340+
'androidActivity' => 'activity',
341+
'androidDeviceSerial' => '123',
342+
'androidUseRunningApp' => true})
305343
end
306344
end
307345
end

rb/spec/unit/selenium/webdriver/edge/options_spec.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ module Edge
5353
minidump_path: 'linux/only',
5454
perf_logging_prefs: {enable_network: true},
5555
window_types: %w[normal devtools],
56+
android_package: 'package',
57+
android_activity: 'activity',
58+
android_device_serial: '123',
59+
android_use_running_app: true,
5660
'custom:options': {foo: 'bar'})
5761

5862
expect(opts.args.to_a).to eq(%w[foo bar])
@@ -77,6 +81,10 @@ module Edge
7781
expect(opts.strict_file_interactability).to eq(true)
7882
expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1)
7983
expect(opts.set_window_rect).to eq(false)
84+
expect(opts.android_package).to eq('package')
85+
expect(opts.android_activity).to eq('activity')
86+
expect(opts.android_device_serial).to eq('123')
87+
expect(opts.android_use_running_app).to eq(true)
8088
expect(opts.options[:'custom:options']).to eq(foo: 'bar')
8189
end
8290
end
@@ -168,6 +176,28 @@ module Edge
168176
end
169177
end
170178

179+
describe '#enable_android' do
180+
it 'adds default android settings' do
181+
options.enable_android
182+
183+
expect(options.android_package).to eq('com.android.chrome')
184+
expect(options.android_activity).to be_nil
185+
expect(options.android_device_serial).to be_nil
186+
expect(options.android_use_running_app).to be_nil
187+
end
188+
189+
it 'accepts parameters' do
190+
options.enable_android(package: 'foo',
191+
serial_number: '123',
192+
activity: 'qualified',
193+
use_running_app: true)
194+
expect(options.android_package).to eq('foo')
195+
expect(options.android_activity).to eq('qualified')
196+
expect(options.android_device_serial).to eq('123')
197+
expect(options.android_use_running_app).to eq(true)
198+
end
199+
end
200+
171201
describe '#as_json' do
172202
it 'returns empty options by default' do
173203
expect(options.as_json).to eq("browserName" => "MicrosoftEdge", "ms:edgeOptions" => {})
@@ -211,6 +241,10 @@ module Edge
211241
minidump_path: 'linux/only',
212242
perf_logging_prefs: {'enable_network': true},
213243
window_types: %w[normal devtools],
244+
android_package: 'package',
245+
android_activity: 'activity',
246+
android_device_serial: '123',
247+
android_use_running_app: true,
214248
'custom:options': {foo: 'bar'})
215249

216250
key = 'ms:edgeOptions'
@@ -239,7 +273,11 @@ module Edge
239273
'excludeSwitches' => %w[foobar barfoo],
240274
'minidumpPath' => 'linux/only',
241275
'perfLoggingPrefs' => {'enableNetwork' => true},
242-
'windowTypes' => %w[normal devtools]})
276+
'windowTypes' => %w[normal devtools],
277+
'androidPackage' => 'package',
278+
'androidActivity' => 'activity',
279+
'androidDeviceSerial' => '123',
280+
'androidUseRunningApp' => true})
243281
end
244282
end
245283
end

rb/spec/unit/selenium/webdriver/firefox/options_spec.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ module Firefox
4646
foo: 'bar',
4747
profile: profile,
4848
log_level: :debug,
49+
android_package: 'package',
50+
android_activity: 'activity',
51+
android_device_serial: '123',
52+
android_intent_arguments: %w[foo bar],
4953
'custom:options': {foo: 'bar'})
5054

5155
expect(opts.args.to_a).to eq(%w[foo bar])
@@ -63,6 +67,10 @@ module Firefox
6367
expect(opts.strict_file_interactability).to eq(true)
6468
expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1)
6569
expect(opts.set_window_rect).to eq(false)
70+
expect(opts.android_package).to eq('package')
71+
expect(opts.android_activity).to eq('activity')
72+
expect(opts.android_device_serial).to eq('123')
73+
expect(opts.android_intent_arguments).to eq(%w[foo bar])
6674
expect(opts.options[:'custom:options']).to eq(foo: 'bar')
6775
end
6876
end
@@ -143,6 +151,28 @@ module Firefox
143151
end
144152
end
145153

154+
describe '#enable_android' do
155+
it 'adds default android settings' do
156+
options.enable_android
157+
158+
expect(options.android_package).to eq('org.mozilla.firefox')
159+
expect(options.android_activity).to be_nil
160+
expect(options.android_device_serial).to be_nil
161+
expect(options.android_intent_arguments).to be_nil
162+
end
163+
164+
it 'accepts parameters' do
165+
options.enable_android(package: 'foo',
166+
serial_number: '123',
167+
activity: 'qualified',
168+
intent_arguments: %w[foo bar])
169+
expect(options.android_package).to eq('foo')
170+
expect(options.android_activity).to eq('qualified')
171+
expect(options.android_device_serial).to eq('123')
172+
expect(options.android_intent_arguments).to eq(%w[foo bar])
173+
end
174+
end
175+
146176
describe '#as_json' do
147177
it 'returns empty options by default' do
148178
expect(options.as_json).to eq("browserName" => "firefox", "moz:firefoxOptions" => {})
@@ -176,6 +206,10 @@ module Firefox
176206
foo: 'bar',
177207
profile: profile,
178208
log_level: :debug,
209+
android_package: 'package',
210+
android_activity: 'activity',
211+
android_device_serial: '123',
212+
android_intent_arguments: %w[foo bar],
179213
'custom:options': {foo: 'bar'})
180214

181215
key = 'moz:firefoxOptions'
@@ -196,7 +230,11 @@ module Firefox
196230
'prefs' => {'foo' => 'bar'},
197231
'profile' => 'encoded_profile',
198232
'log' => {'level' => 'debug'},
199-
'foo' => 'bar'})
233+
'foo' => 'bar',
234+
'androidPackage' => 'package',
235+
'androidActivity' => 'activity',
236+
'androidDeviceSerial' => '123',
237+
'androidIntentArguments' => %w[foo bar]})
200238
end
201239
end
202240
end # Options

0 commit comments

Comments
 (0)