Appium iOS Guide: hiding the keyboard on real devices

After scratching my head for many hours, I have finally figured out how to hide the keyboard. So I thought I would share info.

Background

There are two documented methods to hide the keyboard:

driver.hideKeyboard()

driver.hideKeyboard(String)

Both these methods (as of date) have open bugs on Appium’s Github page. In general, they do not appear to work reliably for iOS.

###So how to hide the keyboard? (a workaround)

  1. First, manually create the situation where your keyboard will be raised. (For example, enter text in a UIATextField.)
  2. Now observe the raised keyboard on your iPhone. What is the bottom-right key saying?
  3. In my example screenshot, the key is called return.
  4. Next, verify that manually pressing this key hides the keyboard.

If yes, you can go ahead and use:

driver.sendKeys(Keys.RETURN)

###Notes

  1. Verified on Appium 1.4.13, OSX 10.11, iOS 9.1, Appium Java Client 3.3.0.
1 Like

How do you use the sendKeys method on a driver object? I thought we can only do sendKeys on a WebElement object ?

below is the method to hide the keyboard on iOS

IOSDriver< WebElement> AD= (IOSDriver) driver;
AD.getKeyboard().sendKeys(Keys.RETURN);

its working fine for me

you can use following code:
EventFiringWebDriver sExecutor = new EventFiringWebDriver(Driver);
sExecutor.ececuteScript(“UIATarget.localTraget.frontMostApp.keyboard.buttons()[“Hide Keyboard”.tap();”);

Same worked for me. this is for java.

Hi All,

In some conditions there are buttons like Done . How to click on that using this same option.

IOSDriver< WebElement> AD= (IOSDriver) driver;
AD.getKeyboard().sendKeys(Keys.RETURN); //THIS WORK FINE

AD.getKeyboard().sendKeys(Keys.DONE); //THIS DOESN’T WORK

Hi,

you can you ascii values for that i.e

driver.getKeyboard().pressKey("\n");

Thanks @nitinmania I can confirm that your solution works with iOS 10.2 simulator as well.

But I’m not sure even when “Done” button is visible on keyboard, why Keys.DONE is not valid

Regards,
Vikram

When i am using above command with IOSDriver + Native App and it is showing this error message
org.openqa.selenium.WebDriverException: Execute script is not supported.

@FindBy(xpath = "//*[@name='Hide keyboard']")
WebElement btn_hideKeyboard;

Perform operation on this Webelement.

Will it work for iPhone native apps?

@FindBy(how= HOW.NAME, using = "Hide keyboard") WebElement btn_hideKeyboard; 

I think it will be better

Is it applicable for C# ? Is it available for Java too ?? @FindBy(how= HOW.NAME, using = “Hide keyboard”)

For Java it works. I use it

What is how=HOW.NAME?Can we use it for all the tags , basically when are we
suppose to use this ?


how do you do this in Ruby?

1 Like

Hi,
For me clicking on ‘return’ [softkey on iOS real device] doesn’t hides the keyboard.
Could you please suggest some other scenarios?

@Chhavi_Singh
In my experience, sending Keys.RETURN works great until you hit a multi-line text box where a return keeps the user in the input (I am testing a hybrid app). I found that in this case I had to put in a dummy touch action to an unused part of the top of the screen which brings the keyboard down…
new TouchAction((MobileDriver) appDriver).tap(260,10).perform();

2 Likes

For me its numeric keyboard.
driver.hideKeyboard() is not working for me
please suggest

You could inspect for the element OK or DONE from the iOS Keyboard and you will have something like these:

//JAVA
List els1 = (MobileElement) driver.findElementsByName(“OK”);
els1.get(0).click();

//Python
element = driver.find_elements_by_name(“OK”)
element[0].click()

//Javascript Webdriver.io
let element = driver.elements(“name=OK”);
element[0].click();

Hope that could work for you, for me works perfectly.