Skip to content

Commit b46ea64

Browse files
committed
Merge remote-tracking branch 'eric/2969'
Conflicts: src/main/java/io/appium/java_client/AppiumDriver.java src/main/java/io/appium/java_client/MobileCommand.java src/test/java/io/appium/java_client/MobileDriverAndroidTest.java
2 parents 93096fa + 409c344 commit b46ea64

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
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: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.Map;
3737
import java.util.Set;
3838

39+
import static com.google.common.base.Preconditions.checkArgument;
40+
import static io.appium.java_client.remote.MobileCapabilityType.*;
3941
import static io.appium.java_client.MobileCommand.*;
4042

4143
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByIosUIAutomation,
@@ -84,6 +86,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
8486
.put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection"))
8587
.put(GET_SETTINGS, getC("/session/:sessionId/appium/settings"))
8688
.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"))
89+
.put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity"))
8790
;
8891
ImmutableMap<String, CommandInfo> mobileCommands = builder.build();
8992

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

175218
/**
176219
*
@@ -232,7 +275,7 @@ public void hideKeyboard() {
232275
*/
233276
public void hideKeyboard(String strategy, String keyName) {
234277
ImmutableMap<String, String> parameters = ImmutableMap.of("strategy", strategy);
235-
if (keyName != null) {
278+
if (_isNotNullOrEmpty(keyName)) {
236279
parameters = parameters.of("key", keyName);
237280
}
238281

@@ -621,7 +664,7 @@ public void ignoreUnimportantViews(Boolean compress) {
621664

622665
@Override
623666
public WebDriver context(String name) {
624-
if (name == null) {
667+
if (_isNotNullOrEmpty(name)) {
625668
throw new IllegalArgumentException("Must supply a context name");
626669
}
627670

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ public interface MobileCommand {
5151
String SET_NETWORK_CONNECTION = "setNetworkConnection";
5252
String GET_SETTINGS = "getSettings";
5353
String SET_SETTINGS = "setSettings";
54+
String START_ACTIVITY = "startActivity";
5455

5556
}

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

Lines changed: 13 additions & 12 deletions
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,18 @@ public void ignoreUnimportantViews() {
139139
ignoreViews = driver.getSettings().get(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS.toString()).getAsBoolean();
140140
assertFalse(ignoreViews);
141141
}
142+
143+
@Test
144+
public void startActivityInThisAppTest(){
145+
driver.startActivity("io.appium.android.apis", ".accessibility.AccessibilityNodeProviderActivity", null, null);
146+
String activity = driver.currentActivity();
147+
assertTrue(activity.contains("Node"));
148+
}
149+
150+
@Test
151+
public void startActivityInAnotherAppTest(){
152+
driver.startActivity("com.android.contacts", ".ContactsListActivity", null, null);
153+
String activity = driver.currentActivity();
154+
assertTrue(activity.contains("Contact"));
155+
}
142156
}

0 commit comments

Comments
 (0)