23
23
import io .appium .java_client .appmanagement .BaseOptions ;
24
24
import io .appium .java_client .appmanagement .BaseRemoveApplicationOptions ;
25
25
import io .appium .java_client .appmanagement .BaseTerminateApplicationOptions ;
26
+ import org .openqa .selenium .InvalidArgumentException ;
26
27
import org .openqa .selenium .UnsupportedCommandException ;
27
28
28
29
import javax .annotation .Nullable ;
29
30
import java .time .Duration ;
30
- import java .util .HashMap ;
31
+ import java .util .AbstractMap ;
32
+ import java .util .Collections ;
31
33
import java .util .Map ;
32
34
import java .util .Optional ;
33
35
34
36
import static com .google .common .base .Preconditions .checkNotNull ;
37
+ import static io .appium .java_client .MobileCommand .ACTIVATE_APP ;
38
+ import static io .appium .java_client .MobileCommand .INSTALL_APP ;
39
+ import static io .appium .java_client .MobileCommand .IS_APP_INSTALLED ;
40
+ import static io .appium .java_client .MobileCommand .QUERY_APP_STATE ;
41
+ import static io .appium .java_client .MobileCommand .REMOVE_APP ;
35
42
import static io .appium .java_client .MobileCommand .RUN_APP_IN_BACKGROUND ;
43
+ import static io .appium .java_client .MobileCommand .TERMINATE_APP ;
36
44
37
- @ SuppressWarnings ("rawtypes" )
45
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" } )
38
46
public interface InteractsWithApps extends ExecutesMethod {
39
47
40
48
/**
@@ -54,11 +62,23 @@ default void installApp(String appPath) {
54
62
* the particular platform.
55
63
*/
56
64
default void installApp (String appPath , @ Nullable BaseInstallApplicationOptions options ) {
57
- Map <String , Object > args = new HashMap <>();
58
- args .put ("app" , appPath );
59
- args .put ("appPath" , appPath );
60
- Optional .ofNullable (options ).map (BaseOptions ::build ).ifPresent (args ::putAll );
61
- CommandExecutionHelper .executeScript (this , "mobile: installApp" , args );
65
+ try {
66
+ Map <String , Object > args = ImmutableMap .<String , Object >builder ()
67
+ .put ("app" , appPath )
68
+ .put ("appPath" , appPath )
69
+ .putAll (Optional .ofNullable (options ).map (BaseOptions ::build ).orElseGet (Collections ::emptyMap ))
70
+ .build ();
71
+ CommandExecutionHelper .executeScript (this , "mobile: installApp" , args );
72
+ } catch (UnsupportedCommandException | InvalidArgumentException e ) {
73
+ // TODO: Remove the fallback
74
+ Map args = ImmutableMap .builder ()
75
+ .put ("appPath" , appPath )
76
+ .putAll (Optional .ofNullable (options ).map (
77
+ (opts ) -> ImmutableMap .of ("options" , opts .build ())
78
+ ).orElseGet (ImmutableMap ::of ))
79
+ .build ();
80
+ CommandExecutionHelper .execute (this , new AbstractMap .SimpleEntry <>(INSTALL_APP , args ));
81
+ }
62
82
}
63
83
64
84
/**
@@ -68,12 +88,21 @@ default void installApp(String appPath, @Nullable BaseInstallApplicationOptions
68
88
* @return True if app is installed, false otherwise.
69
89
*/
70
90
default boolean isAppInstalled (String bundleId ) {
71
- return checkNotNull (
72
- CommandExecutionHelper .executeScript (this , "mobile: isAppInstalled" , ImmutableMap .of (
73
- "bundleId" , bundleId ,
74
- "appId" , bundleId
75
- ))
76
- );
91
+ try {
92
+ return checkNotNull (
93
+ CommandExecutionHelper .executeScript (this , "mobile: isAppInstalled" , ImmutableMap .of (
94
+ "bundleId" , bundleId ,
95
+ "appId" , bundleId
96
+ ))
97
+ );
98
+ } catch (UnsupportedCommandException | InvalidArgumentException e ) {
99
+ // TODO: Remove the fallback
100
+ return checkNotNull (
101
+ CommandExecutionHelper .execute (this ,
102
+ new AbstractMap .SimpleEntry <>(IS_APP_INSTALLED , ImmutableMap .of ("bundleId" , bundleId ))
103
+ )
104
+ );
105
+ }
77
106
}
78
107
79
108
/**
@@ -114,13 +143,30 @@ default boolean removeApp(String bundleId) {
114
143
* @return true if the uninstall was successful.
115
144
*/
116
145
default boolean removeApp (String bundleId , @ Nullable BaseRemoveApplicationOptions options ) {
117
- Map <String , Object > args = new HashMap <>();
118
- args .put ("bundleId" , bundleId );
119
- args .put ("appId" , bundleId );
120
- Optional .ofNullable (options ).map (BaseOptions ::build ).ifPresent (args ::putAll );
121
- return checkNotNull (
122
- CommandExecutionHelper .executeScript (this , "mobile: removeApp" , args )
123
- );
146
+ try {
147
+ Map <String , Object > args = ImmutableMap .<String , Object >builder ()
148
+ .put ("bundleId" , bundleId )
149
+ .put ("appId" , bundleId )
150
+ .putAll (Optional .ofNullable (options ).map (BaseOptions ::build ).orElseGet (Collections ::emptyMap ))
151
+ .build ();
152
+ return checkNotNull (
153
+ CommandExecutionHelper .executeScript (this , "mobile: removeApp" , args )
154
+ );
155
+ } catch (UnsupportedCommandException | InvalidArgumentException e ) {
156
+ // TODO: Remove the fallback
157
+ Map args = ImmutableMap .builder ()
158
+ .put ("bundleId" , bundleId )
159
+ .putAll (Optional .ofNullable (options ).map (
160
+ (opts ) -> ImmutableMap .of ("options" , opts .build ())
161
+ ).orElseGet (ImmutableMap ::of ))
162
+ .build ();
163
+ //noinspection RedundantCast
164
+ return checkNotNull (
165
+ (Boolean ) CommandExecutionHelper .execute (
166
+ this , new AbstractMap .SimpleEntry <>(REMOVE_APP , args )
167
+ )
168
+ );
169
+ }
124
170
}
125
171
126
172
/**
@@ -142,11 +188,23 @@ default void activateApp(String bundleId) {
142
188
* particular platform.
143
189
*/
144
190
default void activateApp (String bundleId , @ Nullable BaseActivateApplicationOptions options ) {
145
- Map <String , Object > args = new HashMap <>();
146
- args .put ("bundleId" , bundleId );
147
- args .put ("appId" , bundleId );
148
- Optional .ofNullable (options ).map (BaseOptions ::build ).ifPresent (args ::putAll );
149
- CommandExecutionHelper .executeScript (this , "mobile: activateApp" , args );
191
+ try {
192
+ Map <String , Object > args = ImmutableMap .<String , Object >builder ()
193
+ .put ("bundleId" , bundleId )
194
+ .put ("appId" , bundleId )
195
+ .putAll (Optional .ofNullable (options ).map (BaseOptions ::build ).orElseGet (Collections ::emptyMap ))
196
+ .build ();
197
+ CommandExecutionHelper .executeScript (this , "mobile: activateApp" , args );
198
+ } catch (UnsupportedCommandException | InvalidArgumentException e ) {
199
+ // TODO: Remove the fallback
200
+ Map args = ImmutableMap .builder ()
201
+ .put ("bundleId" , bundleId )
202
+ .putAll (Optional .ofNullable (options ).map (
203
+ (opts ) -> ImmutableMap .of ("options" , opts .build ())
204
+ ).orElseGet (ImmutableMap ::of ))
205
+ .build ();
206
+ CommandExecutionHelper .execute (this , new AbstractMap .SimpleEntry <>(ACTIVATE_APP , args ));
207
+ }
150
208
}
151
209
152
210
/**
@@ -156,14 +214,24 @@ default void activateApp(String bundleId, @Nullable BaseActivateApplicationOptio
156
214
* @return one of possible {@link ApplicationState} values,
157
215
*/
158
216
default ApplicationState queryAppState (String bundleId ) {
159
- return ApplicationState .ofCode (
160
- checkNotNull (
161
- CommandExecutionHelper .executeScript (this , "mobile: queryAppState" , ImmutableMap .of (
162
- "bundleId" , bundleId ,
163
- "appId" , bundleId
164
- ))
165
- )
166
- );
217
+ try {
218
+ return ApplicationState .ofCode (
219
+ checkNotNull (
220
+ CommandExecutionHelper .executeScript (this , "mobile: queryAppState" , ImmutableMap .of (
221
+ "bundleId" , bundleId ,
222
+ "appId" , bundleId
223
+ ))
224
+ )
225
+ );
226
+ } catch (UnsupportedCommandException | InvalidArgumentException e ) {
227
+ // TODO: Remove the fallback
228
+ return ApplicationState .ofCode (
229
+ checkNotNull (
230
+ CommandExecutionHelper .execute (this ,
231
+ new AbstractMap .SimpleEntry <>(QUERY_APP_STATE , ImmutableMap .of ("bundleId" , bundleId )))
232
+ )
233
+ );
234
+ }
167
235
}
168
236
169
237
/**
@@ -185,12 +253,29 @@ default boolean terminateApp(String bundleId) {
185
253
* @return true if the app was running before and has been successfully stopped.
186
254
*/
187
255
default boolean terminateApp (String bundleId , @ Nullable BaseTerminateApplicationOptions options ) {
188
- Map <String , Object > args = new HashMap <>();
189
- args .put ("bundleId" , bundleId );
190
- args .put ("appId" , bundleId );
191
- Optional .ofNullable (options ).map (BaseOptions ::build ).ifPresent (args ::putAll );
192
- return checkNotNull (
193
- CommandExecutionHelper .executeScript (this , "mobile: terminateApp" , args )
194
- );
256
+ try {
257
+ Map <String , Object > args = ImmutableMap .<String , Object >builder ()
258
+ .put ("bundleId" , bundleId )
259
+ .put ("appId" , bundleId )
260
+ .putAll (Optional .ofNullable (options ).map (BaseOptions ::build ).orElseGet (Collections ::emptyMap ))
261
+ .build ();
262
+ return checkNotNull (
263
+ CommandExecutionHelper .executeScript (this , "mobile: terminateApp" , args )
264
+ );
265
+ } catch (UnsupportedCommandException | InvalidArgumentException e ) {
266
+ // TODO: Remove the fallback
267
+ Map args = ImmutableMap .builder ()
268
+ .put ("bundleId" , bundleId )
269
+ .putAll (Optional .ofNullable (options ).map (
270
+ (opts ) -> ImmutableMap .of ("options" , opts .build ())
271
+ ).orElseGet (ImmutableMap ::of ))
272
+ .build ();
273
+ //noinspection RedundantCast
274
+ return checkNotNull (
275
+ (Boolean ) CommandExecutionHelper .execute (
276
+ this , new AbstractMap .SimpleEntry <>(TERMINATE_APP , args )
277
+ )
278
+ );
279
+ }
195
280
}
196
281
}
0 commit comments