Skip to content

Commit 06023c9

Browse files
Split driver methods into mixin classes (appium#291)
1 parent f98e0a8 commit 06023c9

File tree

5 files changed

+141
-67
lines changed

5 files changed

+141
-67
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from selenium import webdriver
16+
from ..mobilecommand import MobileCommand as Command
17+
18+
19+
class Context(webdriver.Remote):
20+
@property
21+
def contexts(self):
22+
"""
23+
Returns the contexts within the current session.
24+
25+
:Usage:
26+
driver.contexts
27+
"""
28+
return self.execute(Command.CONTEXTS)['value']
29+
30+
@property
31+
def current_context(self):
32+
"""
33+
Returns the current context of the current session.
34+
35+
:Usage:
36+
driver.current_context
37+
"""
38+
return self.execute(Command.GET_CURRENT_CONTEXT)['value']
39+
40+
@property
41+
def context(self):
42+
"""
43+
Returns the current context of the current session.
44+
45+
:Usage:
46+
driver.context
47+
"""
48+
return self.current_context
49+
50+
# pylint: disable=protected-access
51+
52+
def _addCommands(self):
53+
self.command_executor._commands[Command.CONTEXTS] = \
54+
('GET', '/session/$sessionId/contexts')
55+
self.command_executor._commands[Command.GET_CURRENT_CONTEXT] = \
56+
('GET', '/session/$sessionId/context')
57+
self.command_executor._commands[Command.SWITCH_TO_CONTEXT] = \
58+
('POST', '/session/$sessionId/context')
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from selenium import webdriver
16+
from ..mobilecommand import MobileCommand as Command
17+
18+
19+
class Location(webdriver.Remote):
20+
def toggle_location_services(self):
21+
"""Toggle the location services on the device. Android only.
22+
"""
23+
self.execute(Command.TOGGLE_LOCATION_SERVICES, {})
24+
return self
25+
26+
def set_location(self, latitude, longitude, altitude):
27+
"""Set the location of the device
28+
29+
:Args:
30+
- latitude - String or numeric value between -90.0 and 90.00
31+
- longitude - String or numeric value between -180.0 and 180.0
32+
- altitude - String or numeric value
33+
"""
34+
data = {
35+
"location": {
36+
"latitude": str(latitude),
37+
"longitude": str(longitude),
38+
"altitude": str(altitude)
39+
}
40+
}
41+
self.execute(Command.SET_LOCATION, data)
42+
return self
43+
44+
@property
45+
def location(self):
46+
"""Retrieves the current location
47+
48+
:return:
49+
A dictionary whose keys are
50+
- latitude
51+
- longitude
52+
- altitude
53+
"""
54+
return self.execute(Command.GET_LOCATION)['value']
55+
56+
# pylint: disable=protected-access
57+
58+
def _addCommands(self):
59+
self.command_executor._commands[Command.TOGGLE_LOCATION_SERVICES] = \
60+
('POST', '/session/$sessionId/appium/device/toggle_location_services')
61+
self.command_executor._commands[Command.GET_LOCATION] = \
62+
('GET', '/session/$sessionId/location')
63+
self.command_executor._commands[Command.SET_LOCATION] = \
64+
('POST', '/session/$sessionId/location')

appium/webdriver/mobilecommand.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class MobileCommand(object):
6666
GET_SETTINGS = 'getSettings'
6767
UPDATE_SETTINGS = 'updateSettings'
6868
SET_LOCATION = 'setLocation'
69+
GET_LOCATION = 'getLocation'
6970
GET_DEVICE_TIME = 'getDeviceTime'
7071
CLEAR = 'clear'
7172
START_RECORDING_SCREEN = 'startRecordingScreen'

appium/webdriver/webdriver.py

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import base64
1818
import copy
1919

20-
from selenium import webdriver
2120
from selenium.common.exceptions import TimeoutException, InvalidArgumentException
2221
from selenium.webdriver.common.by import By
2322
from selenium.webdriver.remote.command import Command as RemoteCommand
@@ -29,6 +28,8 @@
2928
from appium.webdriver.common.multi_action import MultiAction
3029
from appium.webdriver.common.touch_action import TouchAction
3130
from .errorhandler import MobileErrorHandler
31+
from .extensions.context import Context
32+
from .extensions.location import Location
3233
from .mobilecommand import MobileCommand as Command
3334
from .switch_to import MobileSwitchTo
3435
from .webelement import WebElement as MobileWebElement
@@ -87,7 +88,7 @@ def _make_w3c_caps(caps):
8788
return {'firstMatch': [first_match]}
8889

8990

90-
class WebDriver(webdriver.Remote):
91+
class WebDriver(Location, Context):
9192

9293
def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub',
9394
desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False):
@@ -161,36 +162,6 @@ def _merge_capabilities(self, capabilities):
161162
w3c_caps = _make_w3c_caps(capabilities)
162163
return {'capabilities': w3c_caps, 'desiredCapabilities': capabilities}
163164

164-
@property
165-
def contexts(self):
166-
"""
167-
Returns the contexts within the current session.
168-
169-
:Usage:
170-
driver.contexts
171-
"""
172-
return self.execute(Command.CONTEXTS)['value']
173-
174-
@property
175-
def current_context(self):
176-
"""
177-
Returns the current context of the current session.
178-
179-
:Usage:
180-
driver.current_context
181-
"""
182-
return self.execute(Command.GET_CURRENT_CONTEXT)['value']
183-
184-
@property
185-
def context(self):
186-
"""
187-
Returns the current context of the current session.
188-
189-
:Usage:
190-
driver.context
191-
"""
192-
return self.current_context
193-
194165
def find_element(self, by=By.ID, value=None):
195166
"""
196167
Override for Appium
@@ -812,7 +783,7 @@ def push_file(self, destination_path, base64data=None, source_path=None):
812783
try:
813784
with open(source_path, 'rb') as file:
814785
data = file.read()
815-
except FileNotFoundError:
786+
except IOError:
816787
message = 'source_path {} could not be found. Are you sure the file exists?'.format(source_path)
817788
raise InvalidArgumentException(message)
818789
base64data = base64.b64encode(data).decode('utf-8')
@@ -1164,30 +1135,6 @@ def toggle_wifi(self):
11641135
self.execute(Command.TOGGLE_WIFI, {})
11651136
return self
11661137

1167-
def toggle_location_services(self):
1168-
"""Toggle the location services on the device. Android only.
1169-
"""
1170-
self.execute(Command.TOGGLE_LOCATION_SERVICES, {})
1171-
return self
1172-
1173-
def set_location(self, latitude, longitude, altitude):
1174-
"""Set the location of the device
1175-
1176-
:Args:
1177-
- latitude - String or numeric value between -90.0 and 90.00
1178-
- longitude - String or numeric value between -180.0 and 180.0
1179-
- altitude - String or numeric value
1180-
"""
1181-
data = {
1182-
"location": {
1183-
"latitude": str(latitude),
1184-
"longitude": str(longitude),
1185-
"altitude": str(altitude)
1186-
}
1187-
}
1188-
self.execute(Command.SET_LOCATION, data)
1189-
return self
1190-
11911138
def start_recording_screen(self, **options):
11921139
"""
11931140
Start asynchronous screen recording process.
@@ -1465,12 +1412,7 @@ def finger_print(self, finger_id):
14651412
# pylint: disable=protected-access
14661413

14671414
def _addCommands(self):
1468-
self.command_executor._commands[Command.CONTEXTS] = \
1469-
('GET', '/session/$sessionId/contexts')
1470-
self.command_executor._commands[Command.GET_CURRENT_CONTEXT] = \
1471-
('GET', '/session/$sessionId/context')
1472-
self.command_executor._commands[Command.SWITCH_TO_CONTEXT] = \
1473-
('POST', '/session/$sessionId/context')
1415+
super(WebDriver, self)._addCommands()
14741416
self.command_executor._commands[Command.TOUCH_ACTION] = \
14751417
('POST', '/session/$sessionId/touch/perform')
14761418
self.command_executor._commands[Command.MULTI_ACTION] = \
@@ -1558,12 +1500,8 @@ def _addCommands(self):
15581500
('GET', '/session/$sessionId/appium/settings')
15591501
self.command_executor._commands[Command.UPDATE_SETTINGS] = \
15601502
('POST', '/session/$sessionId/appium/settings')
1561-
self.command_executor._commands[Command.TOGGLE_LOCATION_SERVICES] = \
1562-
('POST', '/session/$sessionId/appium/device/toggle_location_services')
15631503
self.command_executor._commands[Command.TOGGLE_WIFI] = \
15641504
('POST', '/session/$sessionId/appium/device/toggle_wifi')
1565-
self.command_executor._commands[Command.SET_LOCATION] = \
1566-
('POST', '/session/$sessionId/location')
15671505
self.command_executor._commands[Command.LOCATION_IN_VIEW] = \
15681506
('GET', '/session/$sessionId/element/$id/location_in_view')
15691507
self.command_executor._commands[Command.GET_DEVICE_TIME] = \

0 commit comments

Comments
 (0)