Skip to content

Commit 409c344

Browse files
author
Eric Millin
committed
added startActivity function
1 parent c53ca79 commit 409c344

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

README.md

+1
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

+45-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.Map;
3434
import java.util.Set;
3535

36+
import static com.google.common.base.Preconditions.checkArgument;
37+
import static io.appium.java_client.remote.MobileCapabilityType.*;
3638
import static io.appium.java_client.MobileCommand.*;
3739

3840
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByIosUIAutomation,
@@ -81,6 +83,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
8183
.put(OPEN_NOTIFICATIONS, postC("/session/:sessionId/appium/device/open_notifications"))
8284
.put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"))
8385
.put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection"))
86+
.put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity"))
8487
;
8588
ImmutableMap<String, CommandInfo> mobileCommands = builder.build();
8689

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

172215
/**
173216
*
@@ -229,7 +272,7 @@ public void hideKeyboard() {
229272
*/
230273
public void hideKeyboard(String strategy, String keyName) {
231274
ImmutableMap<String, String> parameters = ImmutableMap.of("strategy", strategy);
232-
if (keyName != null) {
275+
if (_isNotNullOrEmpty(keyName)) {
233276
parameters = parameters.of("key", keyName);
234277
}
235278

@@ -574,7 +617,7 @@ public void setNetworkConnection(NetworkConnectionSetting connection) {
574617

575618
@Override
576619
public WebDriver context(String name) {
577-
if (name == null) {
620+
if (_isNotNullOrEmpty(name)) {
578621
throw new IllegalArgumentException("Must supply a context name");
579622
}
580623

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

+1
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

+13-12
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
import org.openqa.selenium.remote.CapabilityType;
44

55
public interface MobileCapabilityType extends CapabilityType {
6+
7+
String AUTOMATION_NAME = "automationName";
68

7-
String AUTOMATION_NAME = "automationName";
9+
String PLATFORM_NAME = "platformName";
10+
String PLATFORM_VERSION = "platformVersion";
811

9-
String PLATFORM_NAME = "platformName";
10-
String PLATFORM_VERSION = "platformVersion";
12+
String DEVICE_NAME = "deviceName";
1113

12-
String DEVICE_NAME = "deviceName";
14+
String NEW_COMMAND_TIMEOUT = "newCommandTimeout";
15+
String DEVICE_READY_TIMEOUT = "deviceReadyTimeout";
16+
String LAUNCH_TIMEOUT = "launchTimeout";
1317

14-
String NEW_COMMAND_TIMEOUT = "newCommandTimeout";
15-
String DEVICE_READY_TIMEOUT = "deviceReadyTimeout";
16-
String LAUNCH_TIMEOUT = "launchTimeout";
17-
18-
String APP = "app";
19-
String APP_PACKAGE = "appPackage";
20-
String APP_ACTIVITY = "appActivity";
21-
String APP_WAIT_ACTIVITY = "appWaitActivity";
18+
String APP = "app";
19+
String APP_PACKAGE = "appPackage";
20+
String APP_ACTIVITY = "appActivity";
21+
String APP_WAIT_ACTIVITY = "appWaitActivity";
22+
String APP_WAIT_PACKAGE = "appWaitPackage";
2223
}

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

+14
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)