繁体   English   中英

如何使用 Appium 在 iOS 移动自动化中隐藏键盘

[英]How to hide keyboard in iOS mobile automation using Appium

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

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

使用的编程语言:Java。

我试过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();

您可以使用java_client 库方法:

driver.findElementByAccessibilityId("Hide keyboard").click();

driver.hideKeyboard(HideKeyboardStrategy.TAP_OUTSIDE);

driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done");

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

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

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

您可以使用下面的代码片段来隐藏键盘:

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

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()

我更喜欢点击 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;
    }

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

 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(); } } }

由于 IOS 设备键盘不再有任何“完成”或“回车”按钮,所以我们不能使用任何 Appium 服务器实用程序界面,如 HideKeyboardStrategy。

我基本上使用了 TouchAction 类的点击方法来点击屏幕顶部并关闭键盘。

        TouchAction touchAction = new TouchAction(driver);
        int topY = driver.manage().window().getSize().height / 8;
        int pressX = driver.manage().window().getSize().width / 2;
        touchAction.tap(new PointOption().withCoordinates(pressX, topY)).perform();

快速简单的解决方案:

我总是尝试点击屏幕上的任何地方,可能是

  • 静态文本
  • 图片

输入后隐藏键盘,除非我明确要求与键盘交互。 这对我来说效果很好。 试试看 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM