forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplications.py
More file actions
216 lines (179 loc) · 7.6 KB
/
applications.py
File metadata and controls
216 lines (179 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from selenium import webdriver
from ..mobilecommand import MobileCommand as Command
class Applications(webdriver.Remote):
def background_app(self, seconds):
"""Puts the application in the background on the device for a certain duration.
Args:
seconds (int): the duration for the application to remain in the background
Returns:
`appium.webdriver.webdriver.WebDriver`
"""
data = {
'seconds': seconds,
}
self.execute(Command.BACKGROUND, data)
return self
def is_app_installed(self, bundle_id):
"""Checks whether the application specified by `bundle_id` is installed on the device.
Args:
bundle_id (str): the id of the application to query
Returns:
bool: `True` if app is installed
"""
data = {
'bundleId': bundle_id,
}
return self.execute(Command.IS_APP_INSTALLED, data)['value']
def install_app(self, app_path, **options):
"""Install the application found at `app_path` on the device.
Args:
app_path (str): the local or remote path to the application to install
Keyword Args:
replace (bool): [Android only] whether to reinstall/upgrade the package if it is
already present on the device under test. True by default
timeout (int): [Android only] how much time to wait for the installation to complete.
60000ms by default.
allowTestPackages (bool): [Android only] whether to allow installation of packages marked
as test in the manifest. False by default
useSdcard (bool): [Android only] whether to use the SD card to install the app. False by default
grantPermissions (bool): [Android only] whether to automatically grant application permissions
on Android 6+ after the installation completes. False by default
Returns:
`appium.webdriver.webdriver.WebDriver`
"""
data = {
'appPath': app_path,
}
if options:
data.update({'options': options})
self.execute(Command.INSTALL_APP, data)
return self
def remove_app(self, app_id, **options):
"""Remove the specified application from the device.
Args:
app_id (str): the application id to be removed
Keyword Args:
keepData (bool): [Android only] whether to keep application data and caches after it is uninstalled.
False by default
timeout (int): [Android only] how much time to wait for the uninstall to complete.
20000ms by default.
Returns:
`appium.webdriver.webdriver.WebDriver`
"""
data = {
'appId': app_id,
}
if options:
data.update({'options': options})
self.execute(Command.REMOVE_APP, data)
return self
def launch_app(self):
"""Start on the device the application specified in the desired capabilities.
Returns:
`appium.webdriver.webdriver.WebDriver`
"""
self.execute(Command.LAUNCH_APP)
return self
def close_app(self):
"""Stop the running application, specified in the desired capabilities, on
the device.
Returns:
`appium.webdriver.webdriver.WebDriver`
"""
self.execute(Command.CLOSE_APP)
return self
def terminate_app(self, app_id, **options):
"""Terminates the application if it is running.
Args:
app_id (str): the application id to be terminates
Keyword Args:
`timeout` (int): [Android only] how much time to wait for the uninstall to complete.
500ms by default.
Returns:
bool: True if the app has been successfully terminated
"""
data = {
'appId': app_id,
}
if options:
data.update({'options': options})
return self.execute(Command.TERMINATE_APP, data)['value']
def activate_app(self, app_id):
"""Activates the application if it is not running
or is running in the background.
Args:
app_id (str): the application id to be activated
Returns:
`appium.webdriver.webdriver.WebDriver`
"""
data = {
'appId': app_id,
}
self.execute(Command.ACTIVATE_APP, data)
return self
def query_app_state(self, app_id):
"""Queries the state of the application.
Args:
app_id (str): the application id to be queried
Returns:
One of possible application state constants. See ApplicationState
class for more details.
"""
data = {
'appId': app_id,
}
return self.execute(Command.QUERY_APP_STATE, data)['value']
def app_strings(self, language=None, string_file=None):
"""Returns the application strings from the device for the specified
language.
Args:
language (str): strings language code
string_file (str): the name of the string file to query
"""
data = {}
if language is not None:
data['language'] = language
if string_file is not None:
data['stringFile'] = string_file
return self.execute(Command.GET_APP_STRINGS, data)['value']
def reset(self):
"""Resets the current application on the device.
"""
self.execute(Command.RESET)
return self
# pylint: disable=protected-access
def _addCommands(self):
self.command_executor._commands[Command.BACKGROUND] = \
('POST', '/session/$sessionId/appium/app/background')
self.command_executor._commands[Command.IS_APP_INSTALLED] = \
('POST', '/session/$sessionId/appium/device/app_installed')
self.command_executor._commands[Command.INSTALL_APP] = \
('POST', '/session/$sessionId/appium/device/install_app')
self.command_executor._commands[Command.REMOVE_APP] = \
('POST', '/session/$sessionId/appium/device/remove_app')
self.command_executor._commands[Command.TERMINATE_APP] = \
('POST', '/session/$sessionId/appium/device/terminate_app')
self.command_executor._commands[Command.ACTIVATE_APP] = \
('POST', '/session/$sessionId/appium/device/activate_app')
self.command_executor._commands[Command.QUERY_APP_STATE] = \
('POST', '/session/$sessionId/appium/device/app_state')
self.command_executor._commands[Command.GET_APP_STRINGS] = \
('POST', '/session/$sessionId/appium/app/strings')
self.command_executor._commands[Command.RESET] = \
('POST', '/session/$sessionId/appium/app/reset')
self.command_executor._commands[Command.LAUNCH_APP] = \
('POST', '/session/$sessionId/appium/app/launch')
self.command_executor._commands[Command.CLOSE_APP] = \
('POST', '/session/$sessionId/appium/app/close')