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/src/main/java/io/appium/java_client/AppiumDriver.java b/src/main/java/io/appium/java_client/AppiumDriver.java index d62879e48..417c5a79c 100644 --- a/src/main/java/io/appium/java_client/AppiumDriver.java +++ b/src/main/java/io/appium/java_client/AppiumDriver.java @@ -33,6 +33,8 @@ import java.util.Map; import java.util.Set; +import static com.google.common.base.Preconditions.checkArgument; +import static io.appium.java_client.remote.MobileCapabilityType.*; import static io.appium.java_client.MobileCommand.*; public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByIosUIAutomation, @@ -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(); @@ -168,6 +171,46 @@ 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 { + + checkArgument((_isNotNullOrEmpty(appPackage) && _isNotNullOrEmpty(appActivity)), + String.format("'%s' and '%s' are required.", APP_PACKAGE, APP_ACTIVITY)); + + appWaitPackage = _isNotNullOrEmpty(appWaitPackage) ? appWaitPackage : ""; + appWaitActivity = _isNotNullOrEmpty(appWaitActivity) ? appWaitActivity : ""; + + ImmutableMap parameters = ImmutableMap.of(APP_PACKAGE, appPackage, + APP_ACTIVITY, appActivity, + APP_WAIT_PACKAGE, appWaitPackage, + 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 +272,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 +617,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..d822b2a02 100644 --- a/src/main/java/io/appium/java_client/remote/MobileCapabilityType.java +++ b/src/main/java/io/appium/java_client/remote/MobileCapabilityType.java @@ -3,20 +3,21 @@ import org.openqa.selenium.remote.CapabilityType; public interface MobileCapabilityType extends CapabilityType { + + String AUTOMATION_NAME = "automationName"; - String AUTOMATION_NAME = "automationName"; + String PLATFORM_NAME = "platformName"; + String PLATFORM_VERSION = "platformVersion"; - String PLATFORM_NAME = "platformName"; - String PLATFORM_VERSION = "platformVersion"; + String DEVICE_NAME = "deviceName"; - String DEVICE_NAME = "deviceName"; + String NEW_COMMAND_TIMEOUT = "newCommandTimeout"; + String DEVICE_READY_TIMEOUT = "deviceReadyTimeout"; + String LAUNCH_TIMEOUT = "launchTimeout"; - String NEW_COMMAND_TIMEOUT = "newCommandTimeout"; - String DEVICE_READY_TIMEOUT = "deviceReadyTimeout"; - String LAUNCH_TIMEOUT = "launchTimeout"; - - String APP = "app"; - String APP_PACKAGE = "appPackage"; - String APP_ACTIVITY = "appActivity"; - String APP_WAIT_ACTIVITY = "appWaitActivity"; + String APP = "app"; + 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..527dfe72b 100644 --- a/src/test/java/io/appium/java_client/MobileDriverAndroidTest.java +++ b/src/test/java/io/appium/java_client/MobileDriverAndroidTest.java @@ -129,4 +129,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")); + } }