Appium - Hide Android Keyboard During Test

Hiding keyboard in android device is one of the common action. In your android mobile device, It will show you soft keyboard on screen automatically when you type text in text box of software app so it will hide some of the elements. Now supposing you wants to select value from drop down which is hidden behind android keyboard. For that, You need to hide android keyboard first so that you can see drop down and then you can select value from it. In android appium software automation test, It is very easy to hide android keyboard using hideKeyboard() method of AndroidDriver. Let's learn how to hide android keyboard in appium software automation test.

App To Use And Aim Of Test
We will use same API Demos software app In this keyboard hiding test. Main aim of this appium software automation test is to hide android soft keyboard after typing text in text box as shown in bellow image.

appium hide keyboard of android

Manually you can view above screen in API Demos software app from API Demos app's Home -> Views -> Controls -> 2. Dark Theme.

Create And Run Test
Create bellow given Keyboard hiding appium test eclipse.

SelectDropDownValue.java
package Android;

import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SelectDropDownValue {
 AndroidDriver driver;

 @BeforeTest
 public void setUp() throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "io.appium.android.apis");
  capabilities.setCapability("appActivity","io.appium.android.apis.ApiDemos");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void select() throws InterruptedException {
  // Scroll till element which contains "Views" text If It Is not visible on screen.
  driver.scrollTo("Views");
  // Click on Views.
  driver.findElement(By.name("Views")).click();
  // Scroll till element which contains "Controls" text If It Is not visible on screen.
  driver.scrollTo("Controls");
  // Click on Controls.
  driver.findElement(By.name("Controls")).click();
  // Scroll till element which contains "2. Dark Theme" text If It Is not visible on screen.
  driver.scrollTo("2. Dark Theme");
  // Click on 2. Dark Theme.
  driver.findElement(By.name("2. Dark Theme")).click();
  // Typing in text box using sendKeys command.
  driver.findElement(By.id("io.appium.android.apis:id/edit")).sendKeys("Test");
  //To hide keyboard. 
  driver.hideKeyboard();
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}

Run above test in eclipse using testng and appium. Last syntax of @Test method will hide android keyboard.

This way you can hide keyboard of android device in appium android software automation testing.

3 comments:

  1. How is it different than sending the keycode to hit the back button?

    ReplyDelete
  2. Hola, hace poco emití el comando
    driver.hideKeyboard();

    Pero después de terminar la prueba mi smartphone ya no me muestra el teclado, solo muestra una pantalla negra y se oculta automáticamente. Agradezco su ayuda.

    ReplyDelete
  3. Scroll() method is deprecated can you please tell me what is alternative method is use to ?

    ReplyDelete