|
17 | 17 |
|
18 | 18 | package io.appium.java_client; |
19 | 19 |
|
| 20 | +import com.google.common.base.Preconditions; |
20 | 21 | import com.google.common.collect.ImmutableList; |
21 | 22 | import com.google.common.collect.ImmutableMap; |
22 | 23 | import io.appium.java_client.internal.JsonToMobileElementConverter; |
| 24 | +import io.appium.java_client.remote.MobileCapabilityType; |
23 | 25 | import org.openqa.selenium.*; |
24 | 26 | import org.openqa.selenium.html5.Location; |
25 | 27 | import org.openqa.selenium.html5.LocationContext; |
@@ -81,6 +83,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){ |
81 | 83 | .put(OPEN_NOTIFICATIONS, postC("/session/:sessionId/appium/device/open_notifications")) |
82 | 84 | .put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection")) |
83 | 85 | .put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection")) |
| 86 | + .put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity")) |
84 | 87 | ; |
85 | 88 | ImmutableMap<String, CommandInfo> mobileCommands = builder.build(); |
86 | 89 |
|
@@ -168,6 +171,48 @@ public String currentActivity() { |
168 | 171 | Response response = execute(CURRENT_ACTIVITY); |
169 | 172 | return response.getValue().toString(); |
170 | 173 | } |
| 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 | + Preconditions.checkArgument((_isNotNullOrEmpty(appPackage) && _isNotNullOrEmpty(appActivity)), |
| 191 | + String.format("'%s' and '%s' are required.", |
| 192 | + MobileCapabilityType.APP_PACKAGE, |
| 193 | + MobileCapabilityType.APP_ACTIVITY)); |
| 194 | + |
| 195 | + appWaitPackage = _isNotNullOrEmpty(appWaitPackage) ? appWaitPackage : ""; |
| 196 | + appWaitActivity = _isNotNullOrEmpty(appWaitActivity) ? appWaitActivity : ""; |
| 197 | + |
| 198 | + ImmutableMap<String, String> parameters = ImmutableMap.of(MobileCapabilityType.APP_PACKAGE, appPackage, |
| 199 | + MobileCapabilityType.APP_ACTIVITY, appActivity, |
| 200 | + MobileCapabilityType.APP_WAIT_PACKAGE, appWaitPackage, |
| 201 | + MobileCapabilityType.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 | + } |
171 | 216 |
|
172 | 217 | /** |
173 | 218 | * |
@@ -229,7 +274,7 @@ public void hideKeyboard() { |
229 | 274 | */ |
230 | 275 | public void hideKeyboard(String strategy, String keyName) { |
231 | 276 | ImmutableMap<String, String> parameters = ImmutableMap.of("strategy", strategy); |
232 | | - if (keyName != null) { |
| 277 | + if (_isNotNullOrEmpty(keyName)) { |
233 | 278 | parameters = parameters.of("key", keyName); |
234 | 279 | } |
235 | 280 |
|
@@ -574,7 +619,7 @@ public void setNetworkConnection(NetworkConnectionSetting connection) { |
574 | 619 |
|
575 | 620 | @Override |
576 | 621 | public WebDriver context(String name) { |
577 | | - if (name == null) { |
| 622 | + if (_isNotNullOrEmpty(name)) { |
578 | 623 | throw new IllegalArgumentException("Must supply a context name"); |
579 | 624 | } |
580 | 625 |
|
|
0 commit comments