Skip to content

Commit

Permalink
Merge pull request #824 from mykola-mokhnach/app_management
Browse files Browse the repository at this point in the history
Update applications management endpoints
  • Loading branch information
TikhomirovSergey authored Feb 14, 2018
2 parents 11aa8f5 + 61d1962 commit 6e9bee1
Show file tree
Hide file tree
Showing 15 changed files with 625 additions and 19 deletions.
124 changes: 117 additions & 7 deletions src/main/java/io/appium/java_client/InteractsWithApps.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@

package io.appium.java_client;

import static io.appium.java_client.MobileCommand.ACTIVATE_APP;
import static io.appium.java_client.MobileCommand.CLOSE_APP;
import static io.appium.java_client.MobileCommand.INSTALL_APP;
import static io.appium.java_client.MobileCommand.IS_APP_INSTALLED;
import static io.appium.java_client.MobileCommand.LAUNCH_APP;
import static io.appium.java_client.MobileCommand.QUERY_APP_STATE;
import static io.appium.java_client.MobileCommand.REMOVE_APP;
import static io.appium.java_client.MobileCommand.RESET;
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
import static io.appium.java_client.MobileCommand.TERMINATE_APP;
import static io.appium.java_client.MobileCommand.prepareArguments;

import com.google.common.collect.ImmutableMap;
import io.appium.java_client.appmanagement.ApplicationState;
import io.appium.java_client.appmanagement.BaseActivateApplicationOptions;
import io.appium.java_client.appmanagement.BaseInstallApplicationOptions;
import io.appium.java_client.appmanagement.BaseRemoveApplicationOptions;
import io.appium.java_client.appmanagement.BaseTerminateApplicationOptions;

import javax.annotation.Nullable;
import java.time.Duration;
import java.util.AbstractMap;

public interface InteractsWithApps extends ExecutesMethod {

/**
* Launch the app which was provided in the capabilities at session creation.
* Launches the app, which was provided in the capabilities at session creation,
* and (re)starts the session.
*/
default void launchApp() {
execute(LAUNCH_APP);
Expand All @@ -44,7 +55,23 @@ default void launchApp() {
* @param appPath path to app to install.
*/
default void installApp(String appPath) {
execute(INSTALL_APP, ImmutableMap.of("appPath", appPath));
installApp(appPath, null);
}

/**
* Install an app on the mobile device.
*
* @param appPath path to app to install or a remote URL.
* @param options Set of the corresponding instllation options for
* the particular platform.
*/
default void installApp(String appPath, @Nullable BaseInstallApplicationOptions options) {
String[] parameters = options == null ? new String[]{"appPath"} :
new String[]{"appPath", "options"};
Object[] values = options == null ? new Object[]{appPath} :
new Object[]{appPath, options.build()};
CommandExecutionHelper.execute(this,
new AbstractMap.SimpleEntry<>(INSTALL_APP, prepareArguments(parameters, values)));
}

/**
Expand All @@ -59,7 +86,7 @@ default boolean isAppInstalled(String bundleId) {
}

/**
* Reset the currently running app for this session.
* Resets the currently running app together with the session.
*/
default void resetApp() {
execute(RESET);
Expand All @@ -79,17 +106,100 @@ default void runAppInBackground(Duration duration) {
/**
* Remove the specified app from the device (uninstall).
*
* @param bundleId the bunble identifier (or app id) of the app to remove.
* @param bundleId the bundle identifier (or app id) of the app to remove.
* @return true if the uninstall was successful.
*/
default boolean removeApp(String bundleId) {
return removeApp(bundleId, null);
}

/**
* Remove the specified app from the device (uninstall).
*
* @param bundleId the bundle identifier (or app id) of the app to remove.
* @param options the set of uninstall options supported by the
* particular platform.
* @return true if the uninstall was successful.
*/
default void removeApp(String bundleId) {
execute(REMOVE_APP, ImmutableMap.of("bundleId", bundleId));
default boolean removeApp(String bundleId, @Nullable BaseRemoveApplicationOptions options) {
String[] parameters = options == null ? new String[]{"bundleId"} :
new String[]{"bundleId", "options"};
Object[] values = options == null ? new Object[]{bundleId} :
new Object[]{bundleId, options.build()};
return CommandExecutionHelper.execute(this,
new AbstractMap.SimpleEntry<>(REMOVE_APP, prepareArguments(parameters, values)));
}

/**
* Close the app which was provided in the capabilities at session creation.
* Close the app which was provided in the capabilities at session creation
* and quits the session.
*/
default void closeApp() {
execute(CLOSE_APP);
}

/**
* Activates the given app if it installed, but not running or if it is running in the
* background.
*
* @param bundleId the bundle identifier (or app id) of the app to activate.
*/
default void activateApp(String bundleId) {
activateApp(bundleId, null);
}

/**
* Activates the given app if it installed, but not running or if it is running in the
* background.
*
* @param bundleId the bundle identifier (or app id) of the app to activate.
* @param options the set of activation options supported by the
* particular platform.
*/
default void activateApp(String bundleId, @Nullable BaseActivateApplicationOptions options) {
String[] parameters = options == null ? new String[]{"bundleId"} :
new String[]{"bundleId", "options"};
Object[] values = options == null ? new Object[]{bundleId} :
new Object[]{bundleId, options.build()};
CommandExecutionHelper.execute(this,
new AbstractMap.SimpleEntry<>(ACTIVATE_APP, prepareArguments(parameters, values)));
}

/**
* Queries the state of an application.
*
* @param bundleId the bundle identifier (or app id) of the app to query the state of.
* @return one of possible {@link ApplicationState} values,
*/
default ApplicationState queryAppState(String bundleId) {
return ApplicationState.ofCode(CommandExecutionHelper.execute(this,
new AbstractMap.SimpleEntry<>(QUERY_APP_STATE, ImmutableMap.of("bundleId", bundleId))));
}

/**
* Terminate the particular application if it is running.
*
* @param bundleId the bundle identifier (or app id) of the app to be terminated.
* @return true if the app was running before and has been successfully stopped.
*/
default boolean terminateApp(String bundleId) {
return terminateApp(bundleId, null);
}

/**
* Terminate the particular application if it is running.
*
* @param bundleId the bundle identifier (or app id) of the app to be terminated.
* @param options the set of termination options supported by the
* particular platform.
* @return true if the app was running before and has been successfully stopped.
*/
default boolean terminateApp(String bundleId, @Nullable BaseTerminateApplicationOptions options) {
String[] parameters = options == null ? new String[]{"bundleId"} :
new String[]{"bundleId", "options"};
Object[] values = options == null ? new Object[]{bundleId} :
new Object[]{bundleId, options.build()};
return CommandExecutionHelper.execute(this,
new AbstractMap.SimpleEntry<>(TERMINATE_APP, prepareArguments(parameters, values)));
}
}
37 changes: 28 additions & 9 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ public class MobileCommand {
public static final String RUN_APP_IN_BACKGROUND;
protected static final String PERFORM_TOUCH_ACTION;
protected static final String PERFORM_MULTI_TOUCH;
protected static final String IS_APP_INSTALLED;
protected static final String INSTALL_APP;
protected static final String REMOVE_APP;
protected static final String LAUNCH_APP;
protected static final String CLOSE_APP;
protected static final String GET_DEVICE_TIME;
protected static final String GET_SESSION;

//region Applications Management
protected static final String IS_APP_INSTALLED;
protected static final String INSTALL_APP;
protected static final String ACTIVATE_APP;
protected static final String QUERY_APP_STATE;
protected static final String TERMINATE_APP;
protected static final String REMOVE_APP;
//endregion

protected static final String GET_PERFORMANCE_DATA;
protected static final String GET_SUPPORTED_PERFORMANCE_DATA_TYPES;

Expand Down Expand Up @@ -97,14 +103,20 @@ public class MobileCommand {
RUN_APP_IN_BACKGROUND = "runAppInBackground";
PERFORM_TOUCH_ACTION = "performTouchAction";
PERFORM_MULTI_TOUCH = "performMultiTouch";
IS_APP_INSTALLED = "isAppInstalled";
INSTALL_APP = "installApp";
REMOVE_APP = "removeApp";
LAUNCH_APP = "launchApp";
CLOSE_APP = "closeApp";
GET_DEVICE_TIME = "getDeviceTime";
GET_SESSION = "getSession";

//region Applications Management
IS_APP_INSTALLED = "isAppInstalled";
QUERY_APP_STATE = "queryAppState";
TERMINATE_APP = "terminateApp";
ACTIVATE_APP = "activateApp";
REMOVE_APP = "removeApp";
INSTALL_APP = "installApp";
//endregion

GET_PERFORMANCE_DATA = "getPerformanceData";
GET_SUPPORTED_PERFORMANCE_DATA_TYPES = "getSuppportedPerformanceDataTypes";

Expand Down Expand Up @@ -148,9 +160,6 @@ public class MobileCommand {
commandRepository.put(RUN_APP_IN_BACKGROUND, postC("/session/:sessionId/appium/app/background"));
commandRepository.put(PERFORM_TOUCH_ACTION, postC("/session/:sessionId/touch/perform"));
commandRepository.put(PERFORM_MULTI_TOUCH, postC("/session/:sessionId/touch/multi/perform"));
commandRepository.put(IS_APP_INSTALLED, postC("/session/:sessionId/appium/device/app_installed"));
commandRepository.put(INSTALL_APP, postC("/session/:sessionId/appium/device/install_app"));
commandRepository.put(REMOVE_APP, postC("/session/:sessionId/appium/device/remove_app"));
commandRepository.put(LAUNCH_APP, postC("/session/:sessionId/appium/app/launch"));
commandRepository.put(CLOSE_APP, postC("/session/:sessionId/appium/app/close"));
commandRepository.put(LOCK, postC("/session/:sessionId/appium/device/lock"));
Expand All @@ -166,6 +175,16 @@ public class MobileCommand {
postC("/session/:sessionId/appium/start_recording_screen"));
commandRepository.put(STOP_RECORDING_SCREEN,
postC("/session/:sessionId/appium/stop_recording_screen"));

//region Applications Management
commandRepository.put(IS_APP_INSTALLED, postC("/session/:sessionId/appium/device/app_installed"));
commandRepository.put(INSTALL_APP, postC("/session/:sessionId/appium/device/install_app"));
commandRepository.put(ACTIVATE_APP, postC("/session/:sessionId/appium/device/activate_app"));
commandRepository.put(REMOVE_APP, postC("/session/:sessionId/appium/device/remove_app"));
commandRepository.put(TERMINATE_APP, postC("/session/:sessionId/appium/device/terminate_app"));
commandRepository.put(QUERY_APP_STATE, postC("/session/:sessionId/appium/device/app_state"));
//endregion

//iOS
commandRepository.put(SHAKE, postC("/session/:sessionId/appium/device/shake"));
commandRepository.put(TOUCH_ID, postC("/session/:sessionId/appium/simulator/touch_id"));
Expand Down
Loading

0 comments on commit 6e9bee1

Please sign in to comment.