【问题标题】:How to hide keyboard in iOS mobile automation using Appium如何使用 Appium 在 iOS 移动自动化中隐藏键盘
【发布时间】:2017-10-15 12:46:49
【问题描述】:

我使用的是 iOS 版本 10.2,xcode 版本是 8.3。

谁能告诉我如何使用 Appium 在 iOS 移动自动化中隐藏键盘?

使用的编程语言:Java。

【问题讨论】:

    标签: ios appium


    【解决方案1】:

    我试过driver.hideKeyboard(),但它对我不起作用。

    所以,我尝试了方式:

    1. 通过按键指定按键名称和方式
    2. 使用 appium 检查键坐标并执行操作。两种方式都适合我。
    // way 1
    driver.findElementByXPath(String.format("//XCUIElementTypeButton[@name='%s']", "Done")).click();
    
    // way 2
    TouchAction touchAction = new TouchAction(driver);
    touchAction.tap(new PointOption().withCoordinates(345, 343)).perform();
    

    【讨论】:

      【解决方案2】:

      您可以使用java_client library 方法:

      driver.findElementByAccessibilityId("Hide keyboard").click();
      
      driver.hideKeyboard(HideKeyboardStrategy.TAP_OUTSIDE);
      
      driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done");
      

      【讨论】:

        【解决方案3】:

        我注意到“完成”不是键盘组的一部分。所以我尝试使用名称“Done”作为获取元素的参考。我最终尝试了这个,它有效。

        driver.findElementByName("Done").click(); 
        

        声明为 IOSDriver 的“驱动程序”集。

        【讨论】:

          【解决方案4】:

          您可以使用以下代码 sn-p 隐藏键盘:

          driver.getKeyboard().pressKey(Keys.RETURN);
          

          【讨论】:

            【解决方案5】:

            我更喜欢点击 iOS 键盘上的最后一个键而不是隐藏:

                @HowToUseLocators(iOSXCUITAutomation = LocatorGroupStrategy.CHAIN)
                @iOSXCUITFindBy(className = "XCUIElementTypeKeyboard")
                @iOSXCUITFindBy(className = "XCUIElementTypeButton")
                private List<IOSElement> last_iOSKeyboardKey;
            
                @HowToUseLocators(iOSXCUITAutomation = LocatorGroupStrategy.CHAIN)
                @iOSXCUITFindBy(className = "XCUIElementTypeKeyboard")
                @iOSXCUITFindBy(iOSNsPredicate = "type == 'XCUIElementTypeButton' AND " +
                        "(name CONTAINS[cd] 'Done' OR name CONTAINS[cd] 'return' " +
                        "OR name CONTAINS[cd] 'Next' OR name CONTAINS[cd] 'Go')")
                private IOSElement last_iOSKeyboardKey_real;
            
                public boolean tapLastKeyboardKey_iOS() {
                    System.out.println("   tapLastKeyboardKey_iOS()");
                    boolean bool = false;
                    setLookTiming(3);
                    try {
            // one way            
            //bool =  tapElement_XCTest(last_iOSKeyboardKey.get(last_iOSKeyboardKey.size()-1));
            // slightly faster way
                        bool =  tapElement_XCTest(last_iOSKeyboardKey_real);
                    } catch (Exception e) {
                        System.out.println("   tapLastKeyboardKey_iOS(): looks like keyboard closed!");
                        System.out.println(driver.getPageSource());
                    }
                    setDefaultTiming();
                    return bool;
                }
            

            【讨论】:

              【解决方案6】:

              我尝试使用上述所有方法。在某些情况下,它不能完美地工作。以我的方式,它会点击键盘的左上角。

              public void hideKeyboard() {
                  if (isAndroid()) {
                      driver.hideKeyboard();
                  } else {
                      IOSDriver iosDriver = (IOSDriver) driver;
                      // TODO: Just work for Text Field
                      // iosDriver.hideKeyboard();
                      // TODO: Tap outside of Keyboard
                      IOSElement element = (IOSElement) iosDriver.findElementByClassName("XCUIElementTypeKeyboard");
                      Point keyboardPoint = element.getLocation();
                      TouchAction touchAction = new TouchAction(driver);
                      touchAction.tap(keyboardPoint.getX() + 2, keyboardPoint.getY() - 2).perform();
                      try {
                          Thread.sleep(500);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              }
              

              【讨论】:

                【解决方案7】:

                Python 解决方案 - 2020:

                    @staticmethod
                    def hide_keyboard(platform):
                        """
                        Hides the software keyboard on the device.
                        """
                        if platform == "Android":
                            driver.hide_keyboard()
                        elif platform == "iOS":
                            driver.find_element_by_name("Done").click()
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-01-09
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-02-23
                  • 1970-01-01
                  • 2018-01-22
                  • 1970-01-01
                  相关资源
                  最近更新 更多