From 3aa020c80df6e7007a72e397f9c1b702e57d536f Mon Sep 17 00:00:00 2001 From: Eric Millin Date: Fri, 5 Sep 2014 17:10:16 -0400 Subject: [PATCH] added startActivity function --- README.md | 1 + java-client.iml | 106 +----------------- .../io/appium/java_client/AppiumDriver.java | 59 ++++++++-- .../io/appium/java_client/MobileCommand.java | 1 + .../remote/MobileCapabilityType.java | 1 + .../java_client/MobileDriverAndroidTest.java | 16 +++ 6 files changed, 72 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index 93510a206..b34b09d3b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Javadocs: http://appium.github.io/java-client/ More can be found in the docs, but here's a quick list of features which this project has added to the usual selenium binding. +- startActivity() - resetApp() - getAppString() - sendKeyEvent() diff --git a/java-client.iml b/java-client.iml index 9578231aa..fad45fca6 100644 --- a/java-client.iml +++ b/java-client.iml @@ -1,107 +1,3 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/src/main/java/io/appium/java_client/AppiumDriver.java b/src/main/java/io/appium/java_client/AppiumDriver.java index d62879e48..742fe8726 100644 --- a/src/main/java/io/appium/java_client/AppiumDriver.java +++ b/src/main/java/io/appium/java_client/AppiumDriver.java @@ -17,9 +17,11 @@ package io.appium.java_client; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import io.appium.java_client.internal.JsonToMobileElementConverter; +import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.*; import org.openqa.selenium.html5.Location; import org.openqa.selenium.html5.LocationContext; @@ -81,6 +83,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){ .put(OPEN_NOTIFICATIONS, postC("/session/:sessionId/appium/device/open_notifications")) .put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection")) .put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection")) + .put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity")) ; ImmutableMap mobileCommands = builder.build(); @@ -154,11 +157,11 @@ public void sendKeyEvent(int key) { * @param metastate metastate for the keypress */ public void sendKeyEvent(int key, Integer metastate) { - ImmutableMap.Builder builder = ImmutableMap.builder(); - builder.put("keycode", key); - if (metastate != null) { builder.put("metastate", metastate); } - ImmutableMap parameters = builder.build(); - execute(KEY_EVENT, parameters); + ImmutableMap.Builder builder = ImmutableMap.builder(); + builder.put("keycode", key); + if (metastate != null) { builder.put("metastate", metastate); } + ImmutableMap parameters = builder.build(); + execute(KEY_EVENT, parameters); } /** @@ -168,6 +171,48 @@ public String currentActivity() { Response response = execute(CURRENT_ACTIVITY); return response.getValue().toString(); } + + /** + * Launches an arbitrary activity during a test. If the activity belongs to + * another application, that application is started and the activity is opened. + * + * This is an Android-only method. + * @param appPackage The package containing the activity. [Required] + * @param appActivity The activity to start. [Required] + * @param appWaitPackage Automation will begin after this package starts. [Optional] + * @param appWaitActivity Automation will begin after this activity starts. [Optional] + * @example + * driver.startActivity("com.foo.bar", ".MyActivity", null, null); + */ + public void startActivity(String appPackage, String appActivity, String appWaitPackage, String appWaitActivity) + throws IllegalArgumentException { + + Preconditions.checkArgument((_isNotNullOrEmpty(appPackage) && _isNotNullOrEmpty(appActivity)), + String.format("'%s' and '%s' are required.", + MobileCapabilityType.APP_PACKAGE, + MobileCapabilityType.APP_ACTIVITY)); + + appWaitPackage = _isNotNullOrEmpty(appWaitPackage) ? appWaitPackage : ""; + appWaitActivity = _isNotNullOrEmpty(appWaitActivity) ? appWaitActivity : ""; + + ImmutableMap parameters = ImmutableMap.of(MobileCapabilityType.APP_PACKAGE, appPackage, + MobileCapabilityType.APP_ACTIVITY, appActivity, + MobileCapabilityType.APP_WAIT_PACKAGE, appWaitPackage, + MobileCapabilityType.APP_WAIT_ACTIVITY, appWaitActivity); + + execute(START_ACTIVITY, parameters); + } + + /** + * Checks if a string is null, empty, or whitespace. + * + * @param str String to check. + * + * @return True if str is not null or empty. + */ + private static boolean _isNotNullOrEmpty(String str) { + return str != null && !str.isEmpty() && str.trim().length() > 0; + } /** * @@ -229,7 +274,7 @@ public void hideKeyboard() { */ public void hideKeyboard(String strategy, String keyName) { ImmutableMap parameters = ImmutableMap.of("strategy", strategy); - if (keyName != null) { + if (_isNotNullOrEmpty(keyName)) { parameters = parameters.of("key", keyName); } @@ -574,7 +619,7 @@ public void setNetworkConnection(NetworkConnectionSetting connection) { @Override public WebDriver context(String name) { - if (name == null) { + if (_isNotNullOrEmpty(name)) { throw new IllegalArgumentException("Must supply a context name"); } diff --git a/src/main/java/io/appium/java_client/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index c9d9920d2..781f5e745 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -49,5 +49,6 @@ public interface MobileCommand { String OPEN_NOTIFICATIONS = "openNotifications"; String GET_NETWORK_CONNECTION = "getNetworkConnection"; String SET_NETWORK_CONNECTION = "setNetworkConnection"; + String START_ACTIVITY = "startActivity"; } diff --git a/src/main/java/io/appium/java_client/remote/MobileCapabilityType.java b/src/main/java/io/appium/java_client/remote/MobileCapabilityType.java index d59573ba9..f824e82f5 100644 --- a/src/main/java/io/appium/java_client/remote/MobileCapabilityType.java +++ b/src/main/java/io/appium/java_client/remote/MobileCapabilityType.java @@ -19,4 +19,5 @@ public interface MobileCapabilityType extends CapabilityType { String APP_PACKAGE = "appPackage"; String APP_ACTIVITY = "appActivity"; String APP_WAIT_ACTIVITY = "appWaitActivity"; + String APP_WAIT_PACKAGE = "appWaitPackage"; } diff --git a/src/test/java/io/appium/java_client/MobileDriverAndroidTest.java b/src/test/java/io/appium/java_client/MobileDriverAndroidTest.java index 99d23885e..152e874f7 100644 --- a/src/test/java/io/appium/java_client/MobileDriverAndroidTest.java +++ b/src/test/java/io/appium/java_client/MobileDriverAndroidTest.java @@ -28,6 +28,8 @@ import java.io.File; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import static org.junit.Assert.*; @@ -129,4 +131,18 @@ public void networkConnectionTest() { public void isLockedTest() { assertEquals(false, driver.isLocked()); } + + @Test + public void startActivityInThisAppTest(){ + driver.startActivity("io.appium.android.apis", ".accessibility.AccessibilityNodeProviderActivity", null, null); + String activity = driver.currentActivity(); + assertTrue(activity.contains("Node")); + } + + @Test + public void startActivityInAnotherAppTest(){ + driver.startActivity("com.android.contacts", ".ContactsListActivity", null, null); + String activity = driver.currentActivity(); + assertTrue(activity.contains("Contact")); + } }