Usage of keyboard in mobile testing

Tester said:

Can anybody tell me how can i use the keyboard keys while automating the mobile apps?

Need help on urgent basis

On Android, you can interact with the keyboard through:

import the following at the top of your test:

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory

import io.appium.java_client.android.AndroidDriver
import io.appium.java_client.android.AndroidKeyCode

And then use this code to access the keys:

AndroidDriver<?> driver = MobileDriverFactory.getDriver()
driver.pressKeyCode(AndroidKeyCode.ENTER)

Where AndroidKeyCode is an enum with all of the possible key entries, e.g. KEYCODE_0, KEYCODE_A, etc.

hi,
use Mobile.tapAtPosition(1075, 1900, FailureHandling.OPTIONAL)

try it.

Hi, I am unable to enter anything from soft keyboard in device through automation. I need to enter 4 digit pin by selecting each digit from the device soft keyboard. Can you please let me know how to automate it?

Also, the above code throws error when test case is run.

. I want to automate this pin entry.

You can try using ‘adb’ command. Refer here: https://www.raizlabs.com/dev/2017/09/automating-input-events-android/. Basically this adb command can be called directly from either cmd (Windows) or terminal(macOS) and so in your script just create a util served as wrapper to call the command, e.g: http://blogs.sheelapps.com/2013/03/run-system-command-in-groovy.html

Hi @sashmita.mohanty24,

I updated the response above to include the import statements you’ll need in order to use the keyboard. The AndroidKeyCode class should have all the keys you need to interact with the pin screen: AndroidKeyCode.KEYCODE_0 through AndroidKeyCode.KEYCODE_9.

If you’re still receiving errors, could you please provide a log and screenshot of the error.

Thanks,

Chris

Hi Chris,

Am not getting any errors. The TC is passed but nothing happens. It is not clicking on the numbers. Actually this keyboard is present on the screen by default once we navigate to this screen. Once we tap on numbers, the circle gets highlighted in black. Not sure if this is behaving as a soft keyboard.

Hi @sashmita.mohanty24,

You might have better luck by disabling the soft keyboard in Android. You’d do this by setting the unicodeKeyboard and resetKeyboard capabilities to true. You can do this right before starting the app in your test:

import com.kms.katalon.core.configuration.RunConfiguration

RunConfiguration.setMobileDriverPreferencesProperty("unicodeKeyboard", true)
RunConfiguration.setMobileDriverPreferencesProperty("resetKeyboard", true)

Hope this helps,

Chris

Hi Chris,
Its me again, so I’m trying to enter numeric digit using the android keypad as well and I able to enter the digits just fine but I need to press the “DONE” button within the android keyboard which will lead to me the next screen. so far I have included these codes and here are my errors and the actual screen shot of the application, please help me resolve this issue.
BTW I have already imported- import io.appium.java_client.android.AndroidKeyCode

Hi @rkarim,

The error you’re seeing is similar to the error you saw before with startX and startY: you are repeating the:

AndroidDriver<?> driver = MobileDriverFactory.getDriver()

line, which is unnecessary. That line sets up the driver variable, and once it’s done one time in your test, you don’t need to do it again, you can just use:

driver.pressKeyCode(AndroidKeyCode.ENTER).

– Chris

Hi Chris,
I tried commenting out that line and running the “driver.pressKeyCode(AndroidKeyCode.ENTER)” code and it doesn’t press the enter button within the numeric android keyboard, Actually there isn’t any enter button within the keyboard its called DONE button, can you please help?

Hi @rkarim,

I wonder if AndroidKeyCode.KEYCODE_NUMPAD_ENTER would work in this case since it’s more specifically for a number pad.

– Chris

1 Like

Hi @Chris_Trevarthen,
Unfortunately AndroidKeyCode.KEYCODE_NUMPAD_ENTER code didn’t work, it executes successfully but doesn’t tap on the Done button within the keyboard at all.

Hi @rkarim,

That’s unfortunate that the keys aren’t working for you. Have you been able to use the Spy Mobile tool and try to capture the object for that key? That might give some hint as to what type of element it is.

The alternative, if we can’t get the key to work, is to use Mobile.tapAtPosition to just tap the screen at a certain coordinate that you know will be within the Done button:

https://docs.katalon.com/katalon-studio/docs/mobile-tap-at-position.html

Hope this helps,

Chris

Hi Chris,
If the the “Done” button of the keyboard is at the very bottom right corner of the screen how can I predict the x and y coordination for this button. I’m asking you this is because my native keyboard can not be recognized by the katalon studio spy, that’s why I’m trying to TapAtPosition method.

Hi @rkarim,

Assuming that the Done button will always be in the lower right of the screen, you could find the height and width of the screen and then use that to calculate an approximate location of the button:

def height = Mobile.getDeviceHeight()
def width = Mobile.getDeviceWidth()
def buttonX = height - 30 // this is a guess and assumes that x=0 is at the left of the screen - needs to be played with
def buttonY = width - 30 // this is a guess and assumes that y=0 is at the top of the screen - needs to be played with
Mobile.tapAtPosition(buttonX, buttonY)

Hope this helps,

Chris

Unable to use the android key code. As I searched in website, it has mentioned that Android keycode deprecated. Please provide the solution for this.

Hi @mchidambarakumar,

It looks like the replacement for AndroidKeyCode is AndroidKey:

https://appium.github.io/java-client/io/appium/java_client/android/nativekey/AndroidKey.html

You should be able to use AndroidKey.ENTER as the enter key.

Hope this helps,

Chris

Thanks it worked in me.

1 Like