0

i'm trying automate the search scenario, i mean pass one text to input field and hit enter button of keyboard.

so far, i tried below:

  1. ((AndroidDriver)driver).pressKeyCode(AndroidKeyCode.ENTER);
  2. driver.getKeyboard().sendKeys(Keys.ENTER);
  3. driver.press_keycode(66)
  4.     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();
        }

and, i'm using java-client 6.0.0-BETA2

can anyone help in this?

Screenshot: enter image description here

Jagadeesh
  • 358
  • 5
  • 17

1 Answers1

0

Try following code. It must work:

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.

Suban Dhyako
  • 2,436
  • 4
  • 16
  • 38
  • cannot import "import io.appium.java_client.android.nativekey.AndroidKey;" and "import io.appium.java_client.android.nativekey.KeyEvent;". May i know which java-client version you are using – Jagadeesh Apr 01 '19 at 06:58
  • It is working fine for java-client version 6.0.0 and above. Why are you using beta version when the stable version is available? – Suban Dhyako Apr 01 '19 at 07:55
  • Now i changed to stable version 6.0.0 and still it is not working. I want to click enter button on keyboard please have a look into attached screenshot above. – Jagadeesh Apr 01 '19 at 08:44
  • you cannot click on the enter button of keypad, all you can do is send "Enter" keyevent which is alternate of enter key on keypad – Suban Dhyako Apr 01 '19 at 09:13
  • Yeah. i'm just explaining my need. And its not working. I see after entering the text some hit action is performing but not ENTER key. If it is performing enter key my app should load the content – Jagadeesh Apr 01 '19 at 09:23
  • can you please confirm if the input text field is selected while you press Enter key? you can try `driver.findElementById("id of text input).click();` and perform above code. – Suban Dhyako Apr 01 '19 at 09:49
  • Yes, i have clicked and passed the keyword then performing enter keyevent. – Jagadeesh Apr 01 '19 at 09:52
  • i'm doing in these steps, step1 : `driver.findElement(By.id("id")).click();` step2: `driver.findElement(By.id("id")).sendKeys("akoon");` Step3: `((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.ENTER));` – Jagadeesh Apr 01 '19 at 09:53
  • Bro, problem is from application side, they have not added onKeyListener() method. Thanks for your time. Now problem resolved. – Jagadeesh Apr 01 '19 at 11:26
  • I used the code to search product in amazon app search box, once the line ((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.ENTER)); is executed, its reseting the app, clearing the text i entered before. any help? – anandhu Feb 02 '21 at 12:37