Skip to content

Commit 6e9bee1

Browse files
Merge pull request #824 from mykola-mokhnach/app_management
Update applications management endpoints
2 parents 11aa8f5 + 61d1962 commit 6e9bee1

15 files changed

+625
-19
lines changed

src/main/java/io/appium/java_client/InteractsWithApps.java

Lines changed: 117 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,34 @@
1616

1717
package io.appium.java_client;
1818

19+
import static io.appium.java_client.MobileCommand.ACTIVATE_APP;
1920
import static io.appium.java_client.MobileCommand.CLOSE_APP;
2021
import static io.appium.java_client.MobileCommand.INSTALL_APP;
2122
import static io.appium.java_client.MobileCommand.IS_APP_INSTALLED;
2223
import static io.appium.java_client.MobileCommand.LAUNCH_APP;
24+
import static io.appium.java_client.MobileCommand.QUERY_APP_STATE;
2325
import static io.appium.java_client.MobileCommand.REMOVE_APP;
2426
import static io.appium.java_client.MobileCommand.RESET;
2527
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
28+
import static io.appium.java_client.MobileCommand.TERMINATE_APP;
2629
import static io.appium.java_client.MobileCommand.prepareArguments;
2730

2831
import 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;
3039
import java.time.Duration;
3140
import java.util.AbstractMap;
3241

3342
public 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
}

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ public class MobileCommand {
4444
public static final String RUN_APP_IN_BACKGROUND;
4545
protected static final String PERFORM_TOUCH_ACTION;
4646
protected static final String PERFORM_MULTI_TOUCH;
47-
protected static final String IS_APP_INSTALLED;
48-
protected static final String INSTALL_APP;
49-
protected static final String REMOVE_APP;
5047
protected static final String LAUNCH_APP;
5148
protected static final String CLOSE_APP;
5249
protected static final String GET_DEVICE_TIME;
5350
protected static final String GET_SESSION;
5451

52+
//region Applications Management
53+
protected static final String IS_APP_INSTALLED;
54+
protected static final String INSTALL_APP;
55+
protected static final String ACTIVATE_APP;
56+
protected static final String QUERY_APP_STATE;
57+
protected static final String TERMINATE_APP;
58+
protected static final String REMOVE_APP;
59+
//endregion
60+
5561
protected static final String GET_PERFORMANCE_DATA;
5662
protected static final String GET_SUPPORTED_PERFORMANCE_DATA_TYPES;
5763

@@ -97,14 +103,20 @@ public class MobileCommand {
97103
RUN_APP_IN_BACKGROUND = "runAppInBackground";
98104
PERFORM_TOUCH_ACTION = "performTouchAction";
99105
PERFORM_MULTI_TOUCH = "performMultiTouch";
100-
IS_APP_INSTALLED = "isAppInstalled";
101-
INSTALL_APP = "installApp";
102-
REMOVE_APP = "removeApp";
103106
LAUNCH_APP = "launchApp";
104107
CLOSE_APP = "closeApp";
105108
GET_DEVICE_TIME = "getDeviceTime";
106109
GET_SESSION = "getSession";
107110

111+
//region Applications Management
112+
IS_APP_INSTALLED = "isAppInstalled";
113+
QUERY_APP_STATE = "queryAppState";
114+
TERMINATE_APP = "terminateApp";
115+
ACTIVATE_APP = "activateApp";
116+
REMOVE_APP = "removeApp";
117+
INSTALL_APP = "installApp";
118+
//endregion
119+
108120
GET_PERFORMANCE_DATA = "getPerformanceData";
109121
GET_SUPPORTED_PERFORMANCE_DATA_TYPES = "getSuppportedPerformanceDataTypes";
110122

@@ -148,9 +160,6 @@ public class MobileCommand {
148160
commandRepository.put(RUN_APP_IN_BACKGROUND, postC("/session/:sessionId/appium/app/background"));
149161
commandRepository.put(PERFORM_TOUCH_ACTION, postC("/session/:sessionId/touch/perform"));
150162
commandRepository.put(PERFORM_MULTI_TOUCH, postC("/session/:sessionId/touch/multi/perform"));
151-
commandRepository.put(IS_APP_INSTALLED, postC("/session/:sessionId/appium/device/app_installed"));
152-
commandRepository.put(INSTALL_APP, postC("/session/:sessionId/appium/device/install_app"));
153-
commandRepository.put(REMOVE_APP, postC("/session/:sessionId/appium/device/remove_app"));
154163
commandRepository.put(LAUNCH_APP, postC("/session/:sessionId/appium/app/launch"));
155164
commandRepository.put(CLOSE_APP, postC("/session/:sessionId/appium/app/close"));
156165
commandRepository.put(LOCK, postC("/session/:sessionId/appium/device/lock"));
@@ -166,6 +175,16 @@ public class MobileCommand {
166175
postC("/session/:sessionId/appium/start_recording_screen"));
167176
commandRepository.put(STOP_RECORDING_SCREEN,
168177
postC("/session/:sessionId/appium/stop_recording_screen"));
178+
179+
//region Applications Management
180+
commandRepository.put(IS_APP_INSTALLED, postC("/session/:sessionId/appium/device/app_installed"));
181+
commandRepository.put(INSTALL_APP, postC("/session/:sessionId/appium/device/install_app"));
182+
commandRepository.put(ACTIVATE_APP, postC("/session/:sessionId/appium/device/activate_app"));
183+
commandRepository.put(REMOVE_APP, postC("/session/:sessionId/appium/device/remove_app"));
184+
commandRepository.put(TERMINATE_APP, postC("/session/:sessionId/appium/device/terminate_app"));
185+
commandRepository.put(QUERY_APP_STATE, postC("/session/:sessionId/appium/device/app_state"));
186+
//endregion
187+
169188
//iOS
170189
commandRepository.put(SHAKE, postC("/session/:sessionId/appium/device/shake"));
171190
commandRepository.put(TOUCH_ID, postC("/session/:sessionId/appium/simulator/touch_id"));

0 commit comments

Comments
 (0)