1616
1717package io .appium .java_client ;
1818
19+ import static io .appium .java_client .MobileCommand .ACTIVATE_APP ;
1920import static io .appium .java_client .MobileCommand .CLOSE_APP ;
2021import static io .appium .java_client .MobileCommand .INSTALL_APP ;
2122import static io .appium .java_client .MobileCommand .IS_APP_INSTALLED ;
2223import static io .appium .java_client .MobileCommand .LAUNCH_APP ;
24+ import static io .appium .java_client .MobileCommand .QUERY_APP_STATE ;
2325import static io .appium .java_client .MobileCommand .REMOVE_APP ;
2426import static io .appium .java_client .MobileCommand .RESET ;
2527import static io .appium .java_client .MobileCommand .RUN_APP_IN_BACKGROUND ;
28+ import static io .appium .java_client .MobileCommand .TERMINATE_APP ;
2629import static io .appium .java_client .MobileCommand .prepareArguments ;
2730
2831import com .google .common .collect .ImmutableMap ;
32+ import io .appium .java_client .appmanagement .ApplicationState ;
33+ import io .appium .java_client .appmanagement .BaseActivateApplicationOptions ;
34+ import io .appium .java_client .appmanagement .BaseInstallApplicationOptions ;
35+ import io .appium .java_client .appmanagement .BaseRemoveApplicationOptions ;
36+ import io .appium .java_client .appmanagement .BaseTerminateApplicationOptions ;
2937
38+ import javax .annotation .Nullable ;
3039import java .time .Duration ;
3140import java .util .AbstractMap ;
3241
3342public interface InteractsWithApps extends ExecutesMethod {
43+
3444 /**
35- * Launch the app which was provided in the capabilities at session creation.
45+ * Launches the app, which was provided in the capabilities at session creation,
46+ * and (re)starts the session.
3647 */
3748 default void launchApp () {
3849 execute (LAUNCH_APP );
@@ -44,7 +55,23 @@ default void launchApp() {
4455 * @param appPath path to app to install.
4556 */
4657 default void installApp (String appPath ) {
47- execute (INSTALL_APP , ImmutableMap .of ("appPath" , appPath ));
58+ installApp (appPath , null );
59+ }
60+
61+ /**
62+ * Install an app on the mobile device.
63+ *
64+ * @param appPath path to app to install or a remote URL.
65+ * @param options Set of the corresponding instllation options for
66+ * the particular platform.
67+ */
68+ default void installApp (String appPath , @ Nullable BaseInstallApplicationOptions options ) {
69+ String [] parameters = options == null ? new String []{"appPath" } :
70+ new String []{"appPath" , "options" };
71+ Object [] values = options == null ? new Object []{appPath } :
72+ new Object []{appPath , options .build ()};
73+ CommandExecutionHelper .execute (this ,
74+ new AbstractMap .SimpleEntry <>(INSTALL_APP , prepareArguments (parameters , values )));
4875 }
4976
5077 /**
@@ -59,7 +86,7 @@ default boolean isAppInstalled(String bundleId) {
5986 }
6087
6188 /**
62- * Reset the currently running app for this session.
89+ * Resets the currently running app together with the session.
6390 */
6491 default void resetApp () {
6592 execute (RESET );
@@ -79,17 +106,100 @@ default void runAppInBackground(Duration duration) {
79106 /**
80107 * Remove the specified app from the device (uninstall).
81108 *
82- * @param bundleId the bunble identifier (or app id) of the app to remove.
109+ * @param bundleId the bundle identifier (or app id) of the app to remove.
110+ * @return true if the uninstall was successful.
111+ */
112+ default boolean removeApp (String bundleId ) {
113+ return removeApp (bundleId , null );
114+ }
115+
116+ /**
117+ * Remove the specified app from the device (uninstall).
118+ *
119+ * @param bundleId the bundle identifier (or app id) of the app to remove.
120+ * @param options the set of uninstall options supported by the
121+ * particular platform.
122+ * @return true if the uninstall was successful.
83123 */
84- default void removeApp (String bundleId ) {
85- execute (REMOVE_APP , ImmutableMap .of ("bundleId" , bundleId ));
124+ default boolean removeApp (String bundleId , @ Nullable BaseRemoveApplicationOptions options ) {
125+ String [] parameters = options == null ? new String []{"bundleId" } :
126+ new String []{"bundleId" , "options" };
127+ Object [] values = options == null ? new Object []{bundleId } :
128+ new Object []{bundleId , options .build ()};
129+ return CommandExecutionHelper .execute (this ,
130+ new AbstractMap .SimpleEntry <>(REMOVE_APP , prepareArguments (parameters , values )));
86131 }
87132
88133 /**
89- * Close the app which was provided in the capabilities at session creation.
134+ * Close the app which was provided in the capabilities at session creation
135+ * and quits the session.
90136 */
91137 default void closeApp () {
92138 execute (CLOSE_APP );
93139 }
94140
141+ /**
142+ * Activates the given app if it installed, but not running or if it is running in the
143+ * background.
144+ *
145+ * @param bundleId the bundle identifier (or app id) of the app to activate.
146+ */
147+ default void activateApp (String bundleId ) {
148+ activateApp (bundleId , null );
149+ }
150+
151+ /**
152+ * Activates the given app if it installed, but not running or if it is running in the
153+ * background.
154+ *
155+ * @param bundleId the bundle identifier (or app id) of the app to activate.
156+ * @param options the set of activation options supported by the
157+ * particular platform.
158+ */
159+ default void activateApp (String bundleId , @ Nullable BaseActivateApplicationOptions options ) {
160+ String [] parameters = options == null ? new String []{"bundleId" } :
161+ new String []{"bundleId" , "options" };
162+ Object [] values = options == null ? new Object []{bundleId } :
163+ new Object []{bundleId , options .build ()};
164+ CommandExecutionHelper .execute (this ,
165+ new AbstractMap .SimpleEntry <>(ACTIVATE_APP , prepareArguments (parameters , values )));
166+ }
167+
168+ /**
169+ * Queries the state of an application.
170+ *
171+ * @param bundleId the bundle identifier (or app id) of the app to query the state of.
172+ * @return one of possible {@link ApplicationState} values,
173+ */
174+ default ApplicationState queryAppState (String bundleId ) {
175+ return ApplicationState .ofCode (CommandExecutionHelper .execute (this ,
176+ new AbstractMap .SimpleEntry <>(QUERY_APP_STATE , ImmutableMap .of ("bundleId" , bundleId ))));
177+ }
178+
179+ /**
180+ * Terminate the particular application if it is running.
181+ *
182+ * @param bundleId the bundle identifier (or app id) of the app to be terminated.
183+ * @return true if the app was running before and has been successfully stopped.
184+ */
185+ default boolean terminateApp (String bundleId ) {
186+ return terminateApp (bundleId , null );
187+ }
188+
189+ /**
190+ * Terminate the particular application if it is running.
191+ *
192+ * @param bundleId the bundle identifier (or app id) of the app to be terminated.
193+ * @param options the set of termination options supported by the
194+ * particular platform.
195+ * @return true if the app was running before and has been successfully stopped.
196+ */
197+ default boolean terminateApp (String bundleId , @ Nullable BaseTerminateApplicationOptions options ) {
198+ String [] parameters = options == null ? new String []{"bundleId" } :
199+ new String []{"bundleId" , "options" };
200+ Object [] values = options == null ? new Object []{bundleId } :
201+ new Object []{bundleId , options .build ()};
202+ return CommandExecutionHelper .execute (this ,
203+ new AbstractMap .SimpleEntry <>(TERMINATE_APP , prepareArguments (parameters , values )));
204+ }
95205}
0 commit comments