Appium | How to hide Keyboard duringmobile test execution:
Hiding Keyboard in mobile automation is one of the common action
because sometime you want take screenshot of the entire page, that
time half of the page will be covered with keyboard. So it will be
good to hide keyboard based on your requirement. 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.
If you want to click or select element or value from drop down which
is hidden behind android keyboard. For that, You need to hide android
keyboard first so that you can see and select drop down value
In Appium automation , we have hideKeyboard()
method of AndroidDriver. Using this we can achive this.
Let's learn how to hide android keyboard in appium
mobile automation with example. Here I’m going to load google.com and
enter search keyword on search box and hide the keyboard.
Now, we need to find the elements; we are going to find them by
xpath:
WebElement searchBox =
driver.findElement(By.xpath("//input[@name='q']"));
for search button:
WebElement searchButton =
driver.findElement(By.xpath("//button[@class='Tg7LZd']"));
6. Now, we need to perform send text & hidekeyboard actions :
searchBox.sendKeys("mahantesh-hadimani blog");
driver.hideKeyboard();
searchButton.click();
7. Complete code should look like the following block of code:
package practise;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class HideKeyBoardExample {
AndroidDriver<MobileElement> driver;
@BeforeClass
public void setUp() throws MalformedURLException
{
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability("deviceName", "emulator-5554");
cap.setCapability("udid", "emulator-5554");
cap.setCapability("browserName", "Chrome");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9.0");
driver=new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
}
@Test
public void testExample(){
WebElement searchBox = driver.findElement(By.xpath("//input[@name='q']"));
searchBox.sendKeys("mahantesh-hadimani blog");
driver.hideKeyboard();
WebElement searchButton = driver.findElement(By.xpath("//button[@class='Tg7LZd']"));
searchButton.click();
}
@AfterClass
public void tearDown(){
driver.quit();
}
}
driver.hideKeyboard() - This appium command will be used to hide the keyboard in mobile.
same method will work for native app, hybrid app & also for web
app. irrespective of any type of mobile app you can use this
method
Any issues?
Sometimes , your script will fail due to mismatch in the chrome
browser version and chromedriver version. Makes sure you installed
chromedriver which supports the mobile chrome browser.
Does this work for python also?
ReplyDeleteWebElement searchBox = driver.findElement(By.xpath("//input[@name='q']"));
searchBox.sendKeys("mahantesh-hadimani blog");
driver.hideKeyboard();
WebElement searchButton = driver.findElement(By.xpath("//button[@class='Tg7LZd']"));
searchButton.click();
yes. it should work. but need to use different syntax as per the python
ReplyDelete