Arijit Biswas
Arijit Biswas

Reputation: 91

How to hide keyboard in iOS mobile automation using Appium

I am using iOS version of 10.2 and xcode version is 8.3.

Can anyone let me know how to hide the keyboard in iOS mobile automation using Appium?

programming language used: Java.

Upvotes: 4

Views: 15357

Answers (10)

S.Hasan
S.Hasan

Reputation: 404

if hideKeyboard() does not work you can try pressing the keyboard's done button.

If the keyboard does not have a done button you can send a "\n" at the end of the text you're sending to the text field.

Upvotes: 0

Narendra Chandratre
Narendra Chandratre

Reputation: 931

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

Upvotes: 0

abhishek narayan
abhishek narayan

Reputation: 139

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.

I basically used the TouchAction class tap method to tap at top of the screen and dismiss the keyboard.

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

Upvotes: 0

Marik ZMR
Marik ZMR

Reputation: 21

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

Upvotes: 1

ruth
ruth

Reputation: 11

You can use below code snippet to hide keyboard:

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

Upvotes: 1

Mizuki
Mizuki

Reputation: 2233

I tried driver.hideKeyboard(), but it doesn't work for me.

So, I tried with way:

  1. by pressing the button specified key name and way
  2. inspect key coordinate with appium and perform action. 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();

Upvotes: 2

June Caballes
June Caballes

Reputation: 21

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. I tried this on my end and it works.

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

The "driver" set declared as IOSDriver.

Upvotes: 1

Long Nguyen
Long Nguyen

Reputation: 11034

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

Upvotes: 0

MrBlack
MrBlack

Reputation: 399

You could use java_client library methods:

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

driver.hideKeyboard(HideKeyboardStrategy.TAP_OUTSIDE);

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

Upvotes: 1

Aleksei
Aleksei

Reputation: 36

i prefer to tap last key on keyboard for iOS instead of hide:

    @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;
    }

Upvotes: 0

Related Questions