|  | 
| 36 | 36 | import java.util.Map; | 
| 37 | 37 | import java.util.Set; | 
| 38 | 38 | 
 | 
|  | 39 | +import static com.google.common.base.Preconditions.checkArgument; | 
|  | 40 | +import static io.appium.java_client.remote.MobileCapabilityType.*; | 
| 39 | 41 | import static io.appium.java_client.MobileCommand.*; | 
| 40 | 42 | 
 | 
| 41 | 43 | public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByIosUIAutomation, | 
| @@ -84,6 +86,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){ | 
| 84 | 86 |             .put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection")) | 
| 85 | 87 |             .put(GET_SETTINGS, getC("/session/:sessionId/appium/settings")) | 
| 86 | 88 |             .put(SET_SETTINGS, postC("/session/:sessionId/appium/settings")) | 
|  | 89 | +            .put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity")) | 
| 87 | 90 |             ; | 
| 88 | 91 |     ImmutableMap<String, CommandInfo> mobileCommands = builder.build(); | 
| 89 | 92 | 
 | 
| @@ -171,6 +174,46 @@ public String currentActivity() { | 
| 171 | 174 |     Response response = execute(CURRENT_ACTIVITY); | 
| 172 | 175 |     return response.getValue().toString(); | 
| 173 | 176 |   } | 
|  | 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 | +  } | 
| 174 | 217 | 
 | 
| 175 | 218 |   /** | 
| 176 | 219 |    * | 
| @@ -232,7 +275,7 @@ public void hideKeyboard() { | 
| 232 | 275 |    */ | 
| 233 | 276 |   public void hideKeyboard(String strategy, String keyName) { | 
| 234 | 277 |     ImmutableMap<String, String> parameters = ImmutableMap.of("strategy", strategy); | 
| 235 |  | -    if (keyName != null) { | 
|  | 278 | +    if (_isNotNullOrEmpty(keyName)) { | 
| 236 | 279 |       parameters = parameters.of("key", keyName); | 
| 237 | 280 |     } | 
| 238 | 281 | 
 | 
| @@ -621,7 +664,7 @@ public void ignoreUnimportantViews(Boolean compress) { | 
| 621 | 664 | 
 | 
| 622 | 665 |   @Override | 
| 623 | 666 |   public WebDriver context(String name) { | 
| 624 |  | -    if (name == null) { | 
|  | 667 | +    if (_isNotNullOrEmpty(name)) { | 
| 625 | 668 |       throw new IllegalArgumentException("Must supply a context name"); | 
| 626 | 669 |     } | 
| 627 | 670 | 
 | 
|  | 
0 commit comments