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,99 @@ 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+ */
111+ default boolean removeApp (String bundleId ) {
112+ return removeApp (bundleId , null );
113+ }
114+
115+ /**
116+ * Remove the specified app from the device (uninstall).
117+ *
118+ * @param bundleId the bundle identifier (or app id) of the app to remove.
119+ * @param options the set of uninstall options supported by the
120+ * particular platform.
121+ * @return true if the uninstall was successful.
83122 */
84- default void removeApp (String bundleId ) {
85- execute (REMOVE_APP , ImmutableMap .of ("bundleId" , bundleId ));
123+ default boolean removeApp (String bundleId , @ Nullable BaseRemoveApplicationOptions options ) {
124+ String [] parameters = options == null ? new String []{"bundleId" } :
125+ new String []{"bundleId" , "options" };
126+ Object [] values = options == null ? new Object []{bundleId } :
127+ new Object []{bundleId , options .build ()};
128+ return CommandExecutionHelper .execute (this ,
129+ new AbstractMap .SimpleEntry <>(REMOVE_APP , prepareArguments (parameters , values )));
86130 }
87131
88132 /**
89- * Close the app which was provided in the capabilities at session creation.
133+ * Close the app which was provided in the capabilities at session creation
134+ * and quits the session.
90135 */
91136 default void closeApp () {
92137 execute (CLOSE_APP );
93138 }
94139
140+ /**
141+ * Activates the given app if it installed, but not running or if it is running in the
142+ * background.
143+ *
144+ * @param bundleId the bundle identifier (or app id) of the app to activate.
145+ */
146+ default void activateApp (String bundleId ) {
147+ activateApp (bundleId , null );
148+ }
149+
150+ /**
151+ * Activates the given app if it installed, but not running or if it is running in the
152+ * background.
153+ *
154+ * @param bundleId the bundle identifier (or app id) of the app to activate.
155+ * @param options the set of activation options supported by the
156+ * particular platform.
157+ */
158+ default void activateApp (String bundleId , @ Nullable BaseActivateApplicationOptions options ) {
159+ String [] parameters = options == null ? new String []{"bundleId" } :
160+ new String []{"bundleId" , "options" };
161+ Object [] values = options == null ? new Object []{bundleId } :
162+ new Object []{bundleId , options .build ()};
163+ CommandExecutionHelper .execute (this ,
164+ new AbstractMap .SimpleEntry <>(ACTIVATE_APP , prepareArguments (parameters , values )));
165+ }
166+
167+ /**
168+ * Queries the state of an application.
169+ *
170+ * @param bundleId the bundle identifier (or app id) of the app to query the state of.
171+ * @return one of possible {@link ApplicationState} values,
172+ */
173+ default ApplicationState queryAppState (String bundleId ) {
174+ return ApplicationState .ofCode (CommandExecutionHelper .execute (this ,
175+ new AbstractMap .SimpleEntry <>(QUERY_APP_STATE , ImmutableMap .of ("bundleId" , bundleId ))));
176+ }
177+
178+ /**
179+ * Terminate the particular application if it is running.
180+ *
181+ * @param bundleId the bundle identifier (or app id) of the app to be terminated.
182+ * @return true if the app was running before and has been successfully stopped.
183+ */
184+ default boolean terminateApp (String bundleId ) {
185+ return terminateApp (bundleId , null );
186+ }
187+
188+ /**
189+ * Terminate the particular application if it is running.
190+ *
191+ * @param bundleId the bundle identifier (or app id) of the app to be terminated.
192+ * @param options the set of termination options supported by the
193+ * particular platform.
194+ * @return true if the app was running before and has been successfully stopped.
195+ */
196+ default boolean terminateApp (String bundleId , @ Nullable BaseTerminateApplicationOptions options ) {
197+ String [] parameters = options == null ? new String []{"bundleId" } :
198+ new String []{"bundleId" , "options" };
199+ Object [] values = options == null ? new Object []{bundleId } :
200+ new Object []{bundleId , options .build ()};
201+ return CommandExecutionHelper .execute (this ,
202+ new AbstractMap .SimpleEntry <>(TERMINATE_APP , prepareArguments (parameters , values )));
203+ }
95204}
0 commit comments