Skip to content

added startActivity function #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
47 changes: 45 additions & 2 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String, CommandInfo> mobileCommands = builder.build();

Expand Down Expand Up @@ -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<String, String> 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;
}

/**
*
Expand Down Expand Up @@ -229,7 +272,7 @@ public void hideKeyboard() {
*/
public void hideKeyboard(String strategy, String keyName) {
ImmutableMap<String, String> parameters = ImmutableMap.of("strategy", strategy);
if (keyName != null) {
if (_isNotNullOrEmpty(keyName)) {
parameters = parameters.of("key", keyName);
}

Expand Down Expand Up @@ -574,7 +617,7 @@ public void setNetworkConnection(NetworkConnectionSetting connection) {

@Override
public WebDriver context(String name) {
if (name == null) {
if (_isNotNullOrEmpty(name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, accidental bug which escaped our code review. yay for tests.
changed to !_isNotNullOrEmpty

throw new IllegalArgumentException("Must supply a context name");
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing on this still seems wrong

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}