This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
1 / 13
May 2017

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.

  • created

    May '17
  • last reply

    Feb '19
  • 12

    replies

  • 8.1k

    views

  • 8

    users

  • 1

    like

@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 month later

@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.png40

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.

1 year later

I just did this:

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

Worked for me.

6 months later

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