2323import io .appium .java_client .appmanagement .BaseOptions ;
2424import io .appium .java_client .appmanagement .BaseRemoveApplicationOptions ;
2525import io .appium .java_client .appmanagement .BaseTerminateApplicationOptions ;
26+ import org .openqa .selenium .InvalidArgumentException ;
2627import org .openqa .selenium .UnsupportedCommandException ;
2728
2829import javax .annotation .Nullable ;
2930import java .time .Duration ;
30- import java .util .HashMap ;
31+ import java .util .AbstractMap ;
32+ import java .util .Collections ;
3133import java .util .Map ;
3234import java .util .Optional ;
3335
3436import 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 ;
3542import static io .appium .java_client .MobileCommand .RUN_APP_IN_BACKGROUND ;
43+ import static io .appium .java_client .MobileCommand .TERMINATE_APP ;
3644
37- @ SuppressWarnings ("rawtypes" )
45+ @ SuppressWarnings ({ "rawtypes" , "unchecked" } )
3846public interface InteractsWithApps extends ExecutesMethod {
3947
4048 /**
@@ -54,11 +62,23 @@ default void installApp(String appPath) {
5462 * the particular platform.
5563 */
5664 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+ }
6282 }
6383
6484 /**
@@ -68,12 +88,21 @@ default void installApp(String appPath, @Nullable BaseInstallApplicationOptions
6888 * @return True if app is installed, false otherwise.
6989 */
7090 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+ }
77106 }
78107
79108 /**
@@ -114,13 +143,30 @@ default boolean removeApp(String bundleId) {
114143 * @return true if the uninstall was successful.
115144 */
116145 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+ }
124170 }
125171
126172 /**
@@ -142,11 +188,23 @@ default void activateApp(String bundleId) {
142188 * particular platform.
143189 */
144190 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+ }
150208 }
151209
152210 /**
@@ -156,14 +214,24 @@ default void activateApp(String bundleId, @Nullable BaseActivateApplicationOptio
156214 * @return one of possible {@link ApplicationState} values,
157215 */
158216 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+ }
167235 }
168236
169237 /**
@@ -185,12 +253,29 @@ default boolean terminateApp(String bundleId) {
185253 * @return true if the app was running before and has been successfully stopped.
186254 */
187255 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+ }
195280 }
196281}
0 commit comments