Skip to content

Commit a22d0fd

Browse files
[py] Add the ability to enable mobile on options classes
1 parent d7c2e4c commit a22d0fd

7 files changed

Lines changed: 65 additions & 0 deletions

File tree

py/selenium/webdriver/chrome/options.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ class Options(ChromiumOptions):
2424
@property
2525
def default_capabilities(self) -> dict:
2626
return DesiredCapabilities.CHROME.copy()
27+
28+
def enable_mobile(self, android_package="com.android.chrome", android_activity=None, device_serial=None):
29+
super().enable_mobile(android_package, android_activity, device_serial)

py/selenium/webdriver/chromium/options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def to_capabilities(self) -> dict:
169169
"""
170170
caps = self._caps
171171
chrome_options = self.experimental_options.copy()
172+
if self.mobile_options:
173+
chrome_options.update(self.mobile_options)
172174
chrome_options["extensions"] = self.extensions
173175
if self.binary_location:
174176
chrome_options["binary"] = self.binary_location

py/selenium/webdriver/common/options.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self):
2727
super(BaseOptions, self).__init__()
2828
self._caps = self.default_capabilities
2929
self.set_capability("pageLoadStrategy", "normal")
30+
self.mobile_options = None
3031

3132
@property
3233
def capabilities(self):
@@ -36,6 +37,23 @@ def set_capability(self, name, value):
3637
""" Sets a capability """
3738
self._caps[name] = value
3839

40+
def enable_mobile(self, android_package: str = None, android_activity: str = None, device_serial: str = None):
41+
"""
42+
Enables mobile browser use for browsers that support it
43+
44+
:Args:
45+
android_activity: The name of the android package to start
46+
"""
47+
if not android_package:
48+
raise AttributeError("android_package must be passed in")
49+
self.mobile_options = {
50+
"androidPackage": android_package
51+
}
52+
if android_activity:
53+
self.mobile_options["androidActivity"] = android_activity
54+
if device_serial:
55+
self.mobile_options["androidDeviceSerial"] = device_serial
56+
3957
@abstractmethod
4058
def to_capabilities(self):
4159
"""Convert options into capabilities dictionary."""

py/selenium/webdriver/firefox/options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ def page_load_strategy(self, strategy: str):
161161
else:
162162
raise ValueError("Strategy can only be one of the following: normal, eager, none")
163163

164+
def enable_mobile(self, android_package: str = "org.mozilla.firefox", android_activity=None, device_serial=None):
165+
super().enable_mobile(android_package, android_activity, device_serial)
166+
164167
def to_capabilities(self) -> dict:
165168
"""Marshals the Firefox options to a `moz:firefoxOptions`
166169
object.
@@ -182,6 +185,8 @@ def to_capabilities(self) -> dict:
182185
opts["profile"] = self._profile.encoded
183186
if self._arguments:
184187
opts["args"] = self._arguments
188+
if self.mobile_options:
189+
opts.update(self.mobile_options)
185190

186191
opts.update(self.log.to_capabilities())
187192

py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,9 @@ def test_starts_with_default_capabilities(options):
155155
def test_is_a_baseoptions(options):
156156
from selenium.webdriver.common.options import BaseOptions
157157
assert isinstance(options, BaseOptions)
158+
159+
160+
def test_enables_chrome_mobile(options):
161+
options.enable_mobile()
162+
result_caps = options.to_capabilities()
163+
assert result_caps["goog:chromeOptions"]["androidPackage"] == "com.android.chrome"

py/test/unit/selenium/webdriver/common/common_options_tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,28 @@ def test_add_arguments(options):
3333
def test_get_arguments(options):
3434
options._arguments = ['foo']
3535
assert 'foo' in options.arguments
36+
37+
38+
def test_enables_mobile(options):
39+
options.enable_mobile(android_package="cheese")
40+
assert options.mobile_options["androidPackage"] == "cheese"
41+
assert not hasattr(options.mobile_options, "androidActivity")
42+
assert not hasattr(options.mobile_options, "androidDeviceSerial")
43+
44+
45+
def test_enable_mobile_errors_without_package(options):
46+
with pytest.raises(AttributeError):
47+
options.enable_mobile()
48+
49+
50+
def test_enable_mobile_with_activity(options):
51+
options.enable_mobile(android_package="sausages",
52+
android_activity="eating")
53+
assert options.mobile_options["androidActivity"] == "eating"
54+
55+
56+
def test_enable_mobile_with_device_serial(options):
57+
options.enable_mobile(android_package="cheese",
58+
android_activity="crackers",
59+
device_serial="1234")
60+
options.mobile_options["androidDeviceSerial"] == "1234"

py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,9 @@ def test_creates_capabilities_with_page_load_strategy(options):
192192
options.page_load_strategy = 'eager'
193193
caps = options.to_capabilities()
194194
assert caps['pageLoadStrategy'] == 'eager'
195+
196+
197+
def test_enables_firefox_mobile(options):
198+
options.enable_mobile()
199+
result_caps = options.to_capabilities()
200+
assert result_caps["moz:firefoxOptions"]["androidPackage"] == "org.mozilla.firefox"

0 commit comments

Comments
 (0)