Enter "Enter key" in Appium Testing

Viewed 27636

How to send/press Enter key from soft keyboard in Appium in Android Automation testing?
I tried several options, but none of them are working - instead of pressing the key, they are clearing the text entered in text area. Below is the code (in JAVA Language):

String app_package_name = "abc.xyz.android";

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Nexus_5X_API_23");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("appPackage", app_package_name);
capabilities.setCapability("appActivity", app_package_name + ".activity.StartupActivity_");

String url = "http://127.0.0.1:4723/wd/hub";
AndroidDriver driver = new AndroidDriver(new URL(url), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

By password = By.id(app_package_name_with_id + "et_password");
WebElement enterPassword = driver.findElement(password);
enterPassword.click();
driver.getKeyboard().sendKeys("12345");

driver.getKeyboard().sendKeys(Keys.ENTER); // THIS IS NOT WORKING.
driver.getKeyboard().sendKeys(Keys.RETURN); // THIS IS ALSO NOT WORKING.
driver.pressKeyCode(AndroidKeyCode.ENTER); // THIS IS ALSO NOT WORKING.
driver.pressKeyCode(AndroidKeyCode.KEYCODE_NUMPAD_ENTER); // SAME HERE.
enterPassword.sendKeys(Keys.ENTER); // SAME HERE.

Please if anyone can help me achieve it?

14 Answers

you can try this, it will work with all devices

driver.executeScript("mobile:performEditorAction", ImmutableMap.of("action", "done"));

The answer from @akash and @LuckyLikey led me to this solution in JavaScript:

return driver.waitForElementById(<elementId>).type(<text to type>).click().execute( "mobile: performEditorAction", { "action": "search" } );

This allowed me to find a text field, input some text, and submit a search command (same as clicking the search icon in the keyboard). Of course, you have to replace <elementId> and <text to type> with the proper values. See http://appium.io/docs/en/commands/mobile-command/ for details on "mobile: performEditorAction".

Try following code. It is working for me:

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;

((AndroidDriver<MobileElement>) driver).pressKey(new KeyEvent(AndroidKey.ENTER));

Before using enter key, you must first click on text input field.

So you can send below specified letter:

sendKeys('\n') for press Enter

Reference more sendKeys API in other programing languages

you can use action class, it worked for me

Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).perform();

So you can like this which has worked for me in some situations in the past and its real simple

for press Enter

sendKeys('\n') 

I was facing the same problem while automating amazon ((AndroidDriver)driver).pressKeyCode(AndroidKeyCode.ENTER); alone didn't work for me. But It worked when i clicked in the search box first.

searchBox.click();
searchBox.sendKeys("Example Input");
((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.ENTER));

Try this ..

element.sendKeys("some text"); driver.pressKey(new KeyEvent(AndroidKey.ENTER));

I found a better solution instead of pressing ENTER button.

I wrote the following command:

driver.hideKeyboard();
Related