简体   繁体   English

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

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

I am using iOS version of 10.2 and xcode version is 8.3.我使用的是 10.2 的 iOS 版本,而 xcode 版本是 8.3。

Can anyone let me know how to hide the keyboard in iOS mobile automation using Appium?谁能告诉我如何使用 Appium 在 iOS 移动自动化中隐藏键盘?

programming language used: Java.使用的编程语言:Java。

I tried driver.hideKeyboard() , but it doesn't work for me.我试过driver.hideKeyboard() ,但它对我不起作用。

So, I tried with way:所以,我尝试了以下方式:

  1. by pressing the button specified key name and way按按钮指定键名和方式
  2. inspect key coordinate with appium and perform action.检查与 appium 的键坐标并执行操作。 Both ways are work for me.两种方式都适合我。
// 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();

You could use java_client library methods:您可以使用java_client 库方法:

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

driver.hideKeyboard(HideKeyboardStrategy.TAP_OUTSIDE);

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

I noticed that "Done" is not part of the keyboard group.我注意到“完成”不是键盘组的一部分。 So I tried to use the name "Done" as my reference to get the element.所以我尝试使用名称“Done”作为获取元素的参考。 I tried this on my end and it works.我最终尝试了这个并且它有效。

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

The "driver" set declared as IOSDriver.声明为 IOSDriver 的“驱动程序”集。

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

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

Solution for Python - 2020: 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()

i prefer to tap last key on keyboard for iOS instead of hide:我更喜欢点击 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;
    }

I tried using all of above method.我尝试使用上述所有方法。 In some case, it doesn't work perfectly.在某些情况下,它不能完美地工作。 In my way, it will tap on top left of keyboard.以我的方式,它将点击键盘的左上角。

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

Since the IOS device keyboard doesn't have any "Done" or "Enter" buttons anymore so we can't use any of the Appium server utility interface like HideKeyboardStrategy.由于 IOS 设备键盘不再有任何“完成”或“回车”按钮,所以我们不能使用任何 Appium 服务器实用程序界面,如 HideKeyboardStrategy。

I basically used the TouchAction class tap method to tap at top of the screen and dismiss the keyboard.我基本上使用了 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();

Quick & simple solution :快速简单的解决方案:

I always try to tap anywhere on screen , may be on我总是尝试点击屏幕上的任何地方,可能是

  • Static text静态文本
  • Image图片

after entering to hide keyboard unless I explicitly has requirement of interacting with keyboard.输入后隐藏键盘,除非我明确要求与键盘交互。 This works pretty well for me.这对我来说效果很好。 Try it :)试试看 :)

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

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