11

This code just aims to find the textbox and send some text to it. When it does that the keyboard appears on the android device.How to dismiss it after the sendKeys.

@Test
    public static void test_demo() throws Exception {
        WebElement element = driver.findElement(By.id("mytextfield"));
        element.sendKeys("test");
        //how do I dismiss keyboard which appears on my android device after sendKeys?  
    }
shiva1791
  • 510
  • 1
  • 4
  • 11

8 Answers8

19

driver.hideKeyboard() will only work with AppiumDriver. I am using java-client-2.2.0.jar that contains this capability.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
Eyal Sooliman
  • 1,876
  • 23
  • 29
8

Best way is to use the back button.

driver.navigate().back(); // For older version of appium
user2220762
  • 565
  • 4
  • 16
  • 1
    This will work on some android api's and will fail on others. in some android Api's it will return you back to previous activity not hiding keyboard – Khalid Abdlqader Feb 25 '15 at 14:43
  • you can check this link for my proper answer http://stackoverflow.com/questions/21704358/how-to-locate-element-in-uiautomator-if-native-keyboard-come-before-your-applica/24359760#24359760 – user2220762 Feb 25 '15 at 19:02
8

Add these desired capabilities values if you want to disable the keyboard on your android selenium tests.

capabilities.setCapability("unicodeKeyboard", true);
capabilities.setCapability("resetKeyboard", true);
SOAlgorithm
  • 407
  • 1
  • 5
  • 15
2

Please use Appium 1.0

Add libraries or add maven dependency of Appium Java client:

<dependency>
  <groupId>io.appium</groupId>
  <artifactId>java-client</artifactId>
  <version>1.1.0</version>
</dependency>

Create driver instance in the following way:

AppiumDriver driver=null;
driver= new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

And use the following function to hide the keyboard:

driver.hideKeyboard();
geoand
  • 60,071
  • 24
  • 172
  • 190
Abhishek Swain
  • 1,054
  • 8
  • 28
  • 1
    Appium 1.1.0 throws a method not yet implemented when calling hideKeyboard() – Valerio Santinelli Jun 24 '14 at 14:25
  • 1
    hideKeyboard() is implemented in java-client 1.5.0 – medy75 Jul 15 '14 at 13:48
  • What if keyboard is not present for some reason (e.g. on emulator keyboard does not show up on send_keys). In this case you'll navigate back and the test goes wrong. You need to check somehow if the OSK is present – Clergyman Oct 08 '14 at 09:44
2

I use driver.hideKeyboard(); every time I'm using sendKeys() to type something. Works perfectly for me.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Santosh Pillai
  • 8,169
  • 1
  • 31
  • 27
1
public static AndroidDriver driver= null;
......

driver.hideKeyboard();

will work perfectly based on my experience.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Prasetyo Budi
  • 96
  • 1
  • 8
1

Solution for those who are not using AppiumDriver:

((AppiumDriver)driver).hideKeyboard(); 
Al Imran
  • 882
  • 7
  • 29
0

driver.hideKeyboard(); some times does not work. Because while running testcase keyboard not appear fast, So testcase fail. If I use Thread.sleep(5000); before hideKeyboard method, it's working very fine for me every time.

try {
    Thread.sleep(5000);
} catch (Exception e) {
    e.getMessage();
}
driver.hideKeyboard();
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Dhinesh M
  • 1
  • 1