forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractsWithApps.java
280 lines (264 loc) · 11.7 KB
/
InteractsWithApps.java
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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.
*/
package io.appium.java_client;
import io.appium.java_client.appmanagement.ApplicationState;
import io.appium.java_client.appmanagement.BaseActivateApplicationOptions;
import io.appium.java_client.appmanagement.BaseInstallApplicationOptions;
import io.appium.java_client.appmanagement.BaseOptions;
import io.appium.java_client.appmanagement.BaseRemoveApplicationOptions;
import io.appium.java_client.appmanagement.BaseTerminateApplicationOptions;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.UnsupportedCommandException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import static io.appium.java_client.MobileCommand.ACTIVATE_APP;
import static io.appium.java_client.MobileCommand.INSTALL_APP;
import static io.appium.java_client.MobileCommand.IS_APP_INSTALLED;
import static io.appium.java_client.MobileCommand.QUERY_APP_STATE;
import static io.appium.java_client.MobileCommand.REMOVE_APP;
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
import static io.appium.java_client.MobileCommand.TERMINATE_APP;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;
@SuppressWarnings({"rawtypes", "unchecked"})
public interface InteractsWithApps extends ExecutesMethod, CanRememberExtensionPresence {
/**
* Install an app on the mobile device.
*
* @param appPath path to app to install.
*/
default void installApp(String appPath) {
installApp(appPath, null);
}
/**
* Install an app on the mobile device.
*
* @param appPath path to app to install or a remote URL.
* @param options Set of the corresponding installation options for
* the particular platform.
*/
default void installApp(String appPath, @Nullable BaseInstallApplicationOptions options) {
final String extName = "mobile: installApp";
try {
var args = new HashMap<String, Object>();
args.put("app", appPath);
args.put("appPath", appPath);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
var args = new HashMap<String, Object>();
args.put("appPath", appPath);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
CommandExecutionHelper.execute(markExtensionAbsence(extName), Map.entry(INSTALL_APP, args));
}
}
/**
* Checks if an app is installed on the device.
*
* @param bundleId bundleId of the app.
* @return True if app is installed, false otherwise.
*/
default boolean isAppInstalled(String bundleId) {
final String extName = "mobile: isAppInstalled";
try {
return requireNonNull(
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, Map.of(
"bundleId", bundleId,
"appId", bundleId
))
);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
return requireNonNull(
CommandExecutionHelper.execute(
markExtensionAbsence(extName),
Map.entry(IS_APP_INSTALLED, Map.of("bundleId", bundleId))
)
);
}
}
/**
* Runs the current app in the background for the time
* requested. This is a synchronous method, it blocks while the
* application is in background.
*
* @param duration The time to run App in background. Minimum time resolution unit is one millisecond.
* Passing a negative value will switch to Home screen and return immediately.
*/
default void runAppInBackground(Duration duration) {
final String extName = "mobile: backgroundApp";
try {
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, Map.of(
"seconds", duration.toMillis() / 1000.0
));
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
CommandExecutionHelper.execute(
markExtensionAbsence(extName),
Map.entry(RUN_APP_IN_BACKGROUND, Map.of("seconds", duration.toMillis() / 1000.0))
);
}
}
/**
* Remove the specified app from the device (uninstall).
*
* @param bundleId the bundle identifier (or app id) of the app to remove.
* @return true if the uninstall was successful.
*/
default boolean removeApp(String bundleId) {
return removeApp(bundleId, null);
}
/**
* Remove the specified app from the device (uninstall).
*
* @param bundleId the bundle identifier (or app id) of the app to remove.
* @param options the set of uninstall options supported by the
* particular platform.
* @return true if the uninstall was successful.
*/
default boolean removeApp(String bundleId, @Nullable BaseRemoveApplicationOptions options) {
final String extName = "mobile: removeApp";
try {
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
args.put("appId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
return requireNonNull(
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args)
);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
//noinspection RedundantCast
return requireNonNull(
(Boolean) CommandExecutionHelper.execute(
markExtensionAbsence(extName),
Map.entry(REMOVE_APP, args)
)
);
}
}
/**
* Activates the given app if it installed, but not running or if it is running in the
* background.
*
* @param bundleId the bundle identifier (or app id) of the app to activate.
*/
default void activateApp(String bundleId) {
activateApp(bundleId, null);
}
/**
* Activates the given app if it installed, but not running or if it is running in the
* background.
*
* @param bundleId the bundle identifier (or app id) of the app to activate.
* @param options the set of activation options supported by the
* particular platform.
*/
default void activateApp(String bundleId, @Nullable BaseActivateApplicationOptions options) {
final String extName = "mobile: activateApp";
try {
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
args.put("appId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
CommandExecutionHelper.execute(markExtensionAbsence(extName), Map.entry(ACTIVATE_APP, args));
}
}
/**
* Queries the state of an application.
*
* @param bundleId the bundle identifier (or app id) of the app to query the state of.
* @return one of possible {@link ApplicationState} values,
*/
default ApplicationState queryAppState(String bundleId) {
final String extName = "mobile: queryAppState";
try {
return ApplicationState.ofCode(
requireNonNull(
CommandExecutionHelper.executeScript(
assertExtensionExists(extName),
extName, Map.of(
"bundleId", bundleId,
"appId", bundleId
)
)
)
);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
return ApplicationState.ofCode(
requireNonNull(
CommandExecutionHelper.execute(
markExtensionAbsence(extName),
Map.entry(QUERY_APP_STATE, Map.of("bundleId", bundleId))
)
)
);
}
}
/**
* Terminate the particular application if it is running.
*
* @param bundleId the bundle identifier (or app id) of the app to be terminated.
* @return true if the app was running before and has been successfully stopped.
*/
default boolean terminateApp(String bundleId) {
return terminateApp(bundleId, null);
}
/**
* Terminate the particular application if it is running.
*
* @param bundleId the bundle identifier (or app id) of the app to be terminated.
* @param options the set of termination options supported by the
* particular platform.
* @return true if the app was running before and has been successfully stopped.
*/
default boolean terminateApp(String bundleId, @Nullable BaseTerminateApplicationOptions options) {
final String extName = "mobile: terminateApp";
try {
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
args.put("appId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
return requireNonNull(
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args)
);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
//noinspection RedundantCast
return requireNonNull(
(Boolean) CommandExecutionHelper.execute(
markExtensionAbsence(extName), Map.entry(TERMINATE_APP, args)
)
);
}
}
}