Skip to content

Commit 967c24b

Browse files
author
Eric Millin
committed
added startActivity function
1 parent c53ca79 commit 967c24b

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Javadocs: http://appium.github.io/java-client/
2121
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.
2222

2323

24+
- startActivity()
2425
- resetApp()
2526
- getAppString()
2627
- sendKeyEvent()

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package io.appium.java_client;
1919

20+
import com.google.common.base.Preconditions.*;
2021
import com.google.common.collect.ImmutableList;
2122
import com.google.common.collect.ImmutableMap;
2223
import io.appium.java_client.internal.JsonToMobileElementConverter;
24+
import io.appium.java_client.remote.MobileCapabilityType;
2325
import org.openqa.selenium.*;
2426
import org.openqa.selenium.html5.Location;
2527
import org.openqa.selenium.html5.LocationContext;
@@ -33,6 +35,8 @@
3335
import java.util.Map;
3436
import java.util.Set;
3537

38+
import static com.google.common.base.Preconditions.checkArgument;
39+
import static io.appium.java_client.remote.MobileCapabilityType.*;
3640
import static io.appium.java_client.MobileCommand.*;
3741

3842
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByIosUIAutomation,
@@ -81,6 +85,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
8185
.put(OPEN_NOTIFICATIONS, postC("/session/:sessionId/appium/device/open_notifications"))
8286
.put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"))
8387
.put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection"))
88+
.put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity"))
8489
;
8590
ImmutableMap<String, CommandInfo> mobileCommands = builder.build();
8691

@@ -168,6 +173,46 @@ public String currentActivity() {
168173
Response response = execute(CURRENT_ACTIVITY);
169174
return response.getValue().toString();
170175
}
176+
177+
/**
178+
* Launches an arbitrary activity during a test. If the activity belongs to
179+
* another application, that application is started and the activity is opened.
180+
*
181+
* This is an Android-only method.
182+
* @param appPackage The package containing the activity. [Required]
183+
* @param appActivity The activity to start. [Required]
184+
* @param appWaitPackage Automation will begin after this package starts. [Optional]
185+
* @param appWaitActivity Automation will begin after this activity starts. [Optional]
186+
* @example
187+
* driver.startActivity("com.foo.bar", ".MyActivity", null, null);
188+
*/
189+
public void startActivity(String appPackage, String appActivity, String appWaitPackage, String appWaitActivity)
190+
throws IllegalArgumentException {
191+
192+
checkArgument((_isNotNullOrEmpty(appPackage) && _isNotNullOrEmpty(appActivity)),
193+
String.format("'%s' and '%s' are required.", APP_PACKAGE, APP_ACTIVITY));
194+
195+
appWaitPackage = _isNotNullOrEmpty(appWaitPackage) ? appWaitPackage : "";
196+
appWaitActivity = _isNotNullOrEmpty(appWaitActivity) ? appWaitActivity : "";
197+
198+
ImmutableMap<String, String> parameters = ImmutableMap.of(APP_PACKAGE, appPackage,
199+
APP_ACTIVITY, appActivity,
200+
APP_WAIT_PACKAGE, appWaitPackage,
201+
APP_WAIT_ACTIVITY, appWaitActivity);
202+
203+
execute(START_ACTIVITY, parameters);
204+
}
205+
206+
/**
207+
* Checks if a string is null, empty, or whitespace.
208+
*
209+
* @param str String to check.
210+
*
211+
* @return True if str is not null or empty.
212+
*/
213+
private static boolean _isNotNullOrEmpty(String str) {
214+
return str != null && !str.isEmpty() && str.trim().length() > 0;
215+
}
171216

172217
/**
173218
*
@@ -229,7 +274,7 @@ public void hideKeyboard() {
229274
*/
230275
public void hideKeyboard(String strategy, String keyName) {
231276
ImmutableMap<String, String> parameters = ImmutableMap.of("strategy", strategy);
232-
if (keyName != null) {
277+
if (_isNotNullOrEmpty(keyName)) {
233278
parameters = parameters.of("key", keyName);
234279
}
235280

@@ -574,7 +619,7 @@ public void setNetworkConnection(NetworkConnectionSetting connection) {
574619

575620
@Override
576621
public WebDriver context(String name) {
577-
if (name == null) {
622+
if (_isNotNullOrEmpty(name)) {
578623
throw new IllegalArgumentException("Must supply a context name");
579624
}
580625

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ public interface MobileCommand {
4949
String OPEN_NOTIFICATIONS = "openNotifications";
5050
String GET_NETWORK_CONNECTION = "getNetworkConnection";
5151
String SET_NETWORK_CONNECTION = "setNetworkConnection";
52+
String START_ACTIVITY = "startActivity";
5253

5354
}

src/main/java/io/appium/java_client/remote/MobileCapabilityType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ public interface MobileCapabilityType extends CapabilityType {
1919
String APP_PACKAGE = "appPackage";
2020
String APP_ACTIVITY = "appActivity";
2121
String APP_WAIT_ACTIVITY = "appWaitActivity";
22+
String APP_WAIT_PACKAGE = "appWaitPackage";
2223
}

src/test/java/io/appium/java_client/MobileDriverAndroidTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,18 @@ public void networkConnectionTest() {
129129
public void isLockedTest() {
130130
assertEquals(false, driver.isLocked());
131131
}
132+
133+
@Test
134+
public void startActivityInThisAppTest(){
135+
driver.startActivity("io.appium.android.apis", ".accessibility.AccessibilityNodeProviderActivity", null, null);
136+
String activity = driver.currentActivity();
137+
assertTrue(activity.contains("Node"));
138+
}
139+
140+
@Test
141+
public void startActivityInAnotherAppTest(){
142+
driver.startActivity("com.android.contacts", ".ContactsListActivity", null, null);
143+
String activity = driver.currentActivity();
144+
assertTrue(activity.contains("Contact"));
145+
}
132146
}

0 commit comments

Comments
 (0)