Skip to content

Commit ccb456a

Browse files
authored
Change altitude optional as arg for set_location (appium#415)
* Change altitude optional as arg for set_location * Add comments * review comments
1 parent cd59d45 commit ccb456a

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

appium/webdriver/extensions/location.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,25 @@ def toggle_location_services(self):
2929
self.execute(Command.TOGGLE_LOCATION_SERVICES, {})
3030
return self
3131

32-
def set_location(self, latitude, longitude, altitude):
32+
def set_location(self, latitude, longitude, altitude=None):
3333
"""Set the location of the device
3434
3535
Args:
36-
latitude (float): String or numeric value between -90.0 and 90.00
37-
longitude (float): String or numeric value between -180.0 and 180.0
38-
altitude (float): String or numeric value
36+
latitude (Union[float, str]): String or numeric value between -90.0 and 90.00
37+
longitude (Union[float, str]): String or numeric value between -180.0 and 180.0
38+
altitude (Union[float, str], optional): String or numeric value (Android real device only)
3939
4040
Returns:
4141
`appium.webdriver.webdriver.WebDriver`
4242
"""
4343
data = {
4444
"location": {
45-
"latitude": float(latitude),
46-
"longitude": float(longitude),
47-
"altitude": float(altitude)
45+
"latitude": latitude,
46+
"longitude": longitude,
4847
}
4948
}
49+
if altitude is not None:
50+
data['location']['altitude'] = altitude
5051
self.execute(Command.SET_LOCATION, data)
5152
return self
5253

test/unit/webdriver/device/location_test.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_toggle_location_services(self):
3636
assert isinstance(driver.toggle_location_services(), WebDriver)
3737

3838
@httpretty.activate
39-
def test_set_location(self):
39+
def test_set_location_float(self):
4040
driver = android_w3c_driver()
4141
httpretty.register_uri(
4242
httpretty.POST,
@@ -49,6 +49,34 @@ def test_set_location(self):
4949
assert abs(d['location']['longitude'] - 22.2) <= FLT_EPSILON
5050
assert abs(d['location']['altitude'] - 33.3) <= FLT_EPSILON
5151

52+
@httpretty.activate
53+
def test_set_location_str(self):
54+
driver = android_w3c_driver()
55+
httpretty.register_uri(
56+
httpretty.POST,
57+
appium_command('/session/1234567890/location')
58+
)
59+
assert isinstance(driver.set_location('11.1', '22.2', '33.3'), WebDriver)
60+
61+
d = get_httpretty_request_body(httpretty.last_request())
62+
assert d['location']['latitude'] == '11.1'
63+
assert d['location']['longitude'] == '22.2'
64+
assert d['location']['altitude'] == '33.3'
65+
66+
@httpretty.activate
67+
def test_set_location_without_altitude(self):
68+
driver = android_w3c_driver()
69+
httpretty.register_uri(
70+
httpretty.POST,
71+
appium_command('/session/1234567890/location')
72+
)
73+
assert isinstance(driver.set_location(11.1, 22.2), WebDriver)
74+
75+
d = get_httpretty_request_body(httpretty.last_request())
76+
assert abs(d['location']['latitude'] - 11.1) <= FLT_EPSILON
77+
assert abs(d['location']['longitude'] - 22.2) <= FLT_EPSILON
78+
assert d['location'].get('altitude') == None
79+
5280
@httpretty.activate
5381
def test_location(self):
5482
driver = android_w3c_driver()

0 commit comments

Comments
 (0)