Appium 【已解决】Appium 如何调用 Android Activity

杨雅洁 · 2014年08月20日 · 最后由 虎卧荒丘 回复于 2014年08月20日 · 1399 次阅读

我想对于所有输入框做一个软键盘处理,搜索网上的资料发现大家提供了两种方法:

第一种:

public static void closeKeyBoard(){
//       if (getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
//            
//             InputMethodManager imm = (InputMethodManager) getApplicationContext()
//                             .getSystemService(Context.INPUT_METHOD_SERVICE);
//
//             imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//     } 

第二种:

是监控页面 layout 变化,用来判断是否调出软键盘。

但是自动化的 appium 框架用的是 java,如上方法都需要调用 Android SDK 的 Activity, 该如何导入?希望大家能提出好的建议。

我想实现的判断就是,如果当前软键盘调出,Press Back.
谢谢!

AppiumDriver 方法解决软键盘问题:

public static void closeKeyBoard(AppiumDriver driver){
  try {
      driver.hideKeyboard();
    } catch (WebDriverException ex) {
      AutoLog.log("<Keyboard>Soft keyboard not present, cannot hide keyboard!!!");;
    }
}
共收到 4 条回复 时间 点赞

appiumdriver 提供了方法

public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByIosUIAutomation,
        FindsByAndroidUIAutomator, FindsByAccessibilityId{

  ........

  /**
   * Hides the keyboard if it is showing.
   * On iOS, there are multiple strategies for hiding the keyboard. Defaults to the "tapOutside" strategy (taps outside the keyboard).
   * Switch to using hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done") if this doesn't work.
   */
  public void hideKeyboard() {
    execute(HIDE_KEYBOARD);
  }

  /**
   * Hides the keyboard if it is showing. Available strategies are PRESS_KEY and TAP_OUTSIDE.
   * One taps outside the keyboard, the other presses a key of your choosing (probably the 'Done' key).
   * Hiding the keyboard often depends on the way an app is implemented, no single strategy always works.
   *
   * These parameters are only for iOS, and ignored by Android.
   *
   * @param strategy HideKeyboardStrategy
   * @param keyName a String, representing the text displayed on the button of the keyboard you want to press. For example: "Done"
   */
  public void hideKeyboard(String strategy, String keyName) {
    ImmutableMap<String, String> parameters = ImmutableMap.of("strategy", strategy);
    if (keyName != null) {
      parameters = parameters.of("key", keyName);
    }

   ........
}

@umbrella1978 非常感谢,调用了很好用,要好好研究 appiumdriver api!

另外,可以导入 android sdk 里面的 android.jar,解决 activity 问题。

#3 楼 @yangyajie_002 分享个用例呗。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册