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.

@Arijit_Biswas you have 2 ways:

  1. hide keyboard with tap outside keyboard
  2. type last keyboard key (“Go”, “Next”, “Done” …) to continue

You can also try…

appiumDriver.hideKeyboard();

Which works in some cases still.

I did something wacky like this…

public static void hideKeyboard() {
    if (testParms.isIOS) {
        try {
            if (testParms.isTablet) {
                throw new EmptyStackException();
            } else {
                appiumDriver.hideKeyboard();
                Functions.sleep(2);

                if (keyboardExists()) {
                    // It didn't go away.  Try the alternate method
                    throw new EmptyStackException();
                }
            }
        } catch (Exception e) {
            List<String> ids = Arrays.asList("Hide keyboard", "DONE", "Done", "Return");

            for (int i = 0; i < ids.size() && keyboardExists(); i++) {
                try {
                    AutoControl btnHideKeyboard = new AutoControl(By.id(ids.get(i)));

                    if (btnHideKeyboard.exists() && btnHideKeyboard.isDisplayed()) {
                        btnHideKeyboard.click();
                        break;
                    }
                } catch (Exception e2) {
                    log.info(appiumDriver.getPageSource());
                    log.exceptionAsInfo(e2);
                }
            }
        }
    } else {
        try {
            appiumDriver.hideKeyboard();
        } catch (Exception e) {
            // Yuck!
        }
    }
}
1 Like

Use swipe() outside keyboard

Did anyone observe this weird issue: when hideKeyboard() is called for iOS simulator, driver taps the microphone button, and instead of just hiding the keyboard, it brings up the “Enable dictation” dialog?

appium 1.6.5, java, ios simulator is iPhone 6, platform 10.3.1, running on macOs

That’s how it looks BEFORE hideKeyboard() : https://pasteboard.co/GATmMGJ.png
Right after hideKeyboard(): https://pasteboard.co/GATn98R.png

@darina-techery you can at least 4 ways:


            ((IOSDriver) driver).hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done"); // Done can change to whatever valid on our screen
            ((IOSDriver) driver).hideKeyboard(HideKeyboardStrategy.TAP_OUTSIDE);
            last_iOSKeyboardKey.click(); //my favourite. last_iOSKeyboardKey you can find by any way you know.
            // last 4 is tap yourself outside in any place on screen outside keyboard e.g. in center of header

Thanks Aleksei, I’ll try these. No luck with the first one, though.
But actually, the situation I described sounds like a bug to me: is it ok to bring up voice input on hideKeyboard()?
I updated appium to 1.6.6, beta 2, java client to 5.0.0-BETA9, and the behavior is the same.

UPD: I also noticed that there is no “Done” or “Close” button on soft keyboard, its last button is “#”: https://pasteboard.co/GATL24W.png

@darina-techery in your case i suggest to tap outside on this screen.

Doesn’t work either, unfortunately. None of the ways mentioned above worked.

I solved this issue by adding a workaround: instead of custom setText() operation, which includes sendKeys() and hideKeyboard() calls, I simply send keys and tap “Done” button above the text field.

Still, it is a workaround. I believe the general problem is the following: if there is no “return”/“done”/“hide” button on a soft keyboard, driver hits voice input key instead.
I think it’s worth mentioning it in Appium docs (that is, if there is no “return” button on a soft keyboard in particular situation, hideKeyboard() will not work). Or the method should throw an exception (e.g., “Appium driver tried to close the keyboard, but no “close” button was found, use some controls outside the keyboard to dismiss it”). Now this method does something different from what’s mentioned in its name, and does it silently, no warning, no exception.

I just did this:

    const doneButton = await this._driver.findElementByText('Done');
    await doneButton.tap();

Worked for me.

Locate the return or Done keyboard button in inspector

driver.findElement(By.xpath("//XCUIElementTypeKeyboard//XCUIElementTypeButton[@name=‘Return’]")).click();

Define it in the cap:

capabilities.setCapability(“unicodeKeyboard”, true);