Enter "Enter key" in Appium Testing
How to send / press Enter key from soft keyboard in Appium in Android Automation testing?
I tried several options, but none of them work - instead of pressing a key, they clear the text entered in the 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 with this?
source to share
You can use Robot class: -
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It is not recommended that your test cases run in the background. The robot frame is just about to enter independently, without claiming anything.
Or you can try below code: -
driver.pressKeyCode(AndroidKeyCode.KEYCODE_NUMPAD_ENTER );
Source: - (It has other ways to hit Enter too): -
https://discuss.appium.io/t/how-to-click-enter-after-entering-some-text/3136/4
Hope this helps you :)
source to share
@Akash and @LuckyLikey's answer led me to this JavaScript solution:
return driver.waitForElementById(<elementId>).type(<text to type>).click().execute( "mobile: performEditorAction", { "action": "search" } );
This allowed me to find a textbox, enter text, and send a search command (similar to clicking on the search icon on my keyboard). Of course, you need to replace <elementId>
and <text to type>
at the correct values. See http://appium.io/docs/en/commands/mobile-command/ for details on "mobile: executeEditorAction".
source to share
Try the following code. This works 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 the enter key, you must first click on the text input field.
source to share
So you can send the below mentioned email:
sendKeys ('\ n') to press Enter
Link to the sendKeys API in other programming languages
source to share