|
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;
|
|
33 | 35 | import java.util.Map;
|
34 | 36 | import java.util.Set;
|
35 | 37 |
|
| 38 | +import static com.google.common.base.Preconditions.checkArgument; |
| 39 | +import static io.appium.java_client.remote.MobileCapabilityType.*; |
36 | 40 | import static io.appium.java_client.MobileCommand.*;
|
37 | 41 |
|
38 | 42 | public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByIosUIAutomation,
|
@@ -81,6 +85,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
|
81 | 85 | .put(OPEN_NOTIFICATIONS, postC("/session/:sessionId/appium/device/open_notifications"))
|
82 | 86 | .put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"))
|
83 | 87 | .put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection"))
|
| 88 | + .put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity")) |
84 | 89 | ;
|
85 | 90 | ImmutableMap<String, CommandInfo> mobileCommands = builder.build();
|
86 | 91 |
|
@@ -168,6 +173,46 @@ public String currentActivity() {
|
168 | 173 | Response response = execute(CURRENT_ACTIVITY);
|
169 | 174 | return response.getValue().toString();
|
170 | 175 | }
|
| 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 | + } |
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