Android and Appium: Press Enter on the soft keyboard

Problem: Use Appium and press ‘Enter’ on the soft keyboard.

This post continues our series on intermediate level problems when automating mobile application tests using Appium. We thought of sending a KeyEvent to an input (textbox) element using Appium. Superficially this appears to be trivial, but while writing the script, figured out otherwise. To our surprise, it failed. And there seems to be a good reason for it too! The rest of this post outlines what we were trying to do, what we learnt and what we (reluctantly) ended up doing.


Why this post?

Qxf2’s top goal is to help testers. We identified mobile automation as one area where we could help testers. Over the past few months, we have written several basic tutorials that help testers with mobile automation. We feel like the next stage is to address some common intermediate level problems you may face as you implement mobile automation at your workplace. Over the next few posts, we will tackle some common technical challenges testers are likely to face and our approach to solve them.


New to mobile automation?

If you are new to mobile automation, we suggest the following posts to get started:
a) Run Appium on an emulator
b) Run Appium on mobile devices
c) Identify UI elements on mobile platforms
d) Execute different mobile gestures


Overview

As with our earlier post, we continue to use BigBasket as the application under test. This post deals with an issue we faced while writing the tests scripts for search functionality. We entered a string in the search box. In this particular application, once we enter the text to be searched, a soft keyboard appears with the search icon instead of the enter button. Turns out for BigBasket, the search icon cannot be identified by an xpath or id. And our quest for a solution taught us something about Android.


Our Approaches

1. Search icon (element) could not be identified using xpath or id:
Element related to the search icon could not be identified using the UIAutomatorViewer. Hence we could not write any xpath for it.

sea

As seen in the screenshot above the ‘resource-id’ shown for the search icon is actually the id for big basket ‘checkout’ button. Sigh. BigBasket devs, you can do better!

2. Enter key event:
Next, we attempted to send an ‘ENTER’ keyevent with keycode 66(which simulates the enter key behaviour) after we input the search text but we were not successful.

text_field = self.get_element(self.search_box)
text_field.send_keys(value)
self.wait(3)
self.driver.keyevent(66)

The Appium logs did not show any error but the event was not triggered and the search results were not shown. This blew our minds. How was a KeyEvent being ignored? Surely we were doing something wrong! We spent many an hour trying to figure out why sending the ‘Enter’ KeyEvent was failing. Finally, while pouring through Android documentation, we hit this:

You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses.

Sigh. Sigh. Sigh.

3. Using Co-ordinates:
So we kicked. We screamed. Tore our hair out. Banged our heads on a wall. And then went about using co-ordinates to solve this issue. These co-ordinates unfortunately vary for different devices. We perform a ‘tap_positon’ for the corresponding co-ordinates (as shown in the top right corner of the above screenshot)

text_field = self.get_element(self.search_box)
text_field.send_keys(value)
self.wait(3)
self.tap_position([(500,840)])

And that ended our little adventure. We sincerely hope that we are missing something here. Let us know if you found a better way!

Note: We have open-sourced an intuitive, well-organized, Pythonic test automation framework. Check it out!


Subscribe to our weekly Newsletter


View a sample



26 thoughts on “Android and Appium: Press Enter on the soft keyboard

  1. Is there similar way to automation typing using appium? I am more looking to cover IME handing code via some tests not sendKeyEvents.

  2. Hi Dheeraj,
    As we mentioned earlier
    IME_ACTIONS is what Android recommends developers use. As we stated in the article, we were unable to figure out how to (robustly) automate soft keyboard events that are listening for IME_ACTIONS. We ended up using co-ordinates but that is a lousy and flakey workaround.

    If you do find an answer, please post here. We would love to learn how to make our automation more robust in this specific case.

  3. To make it much more general I have used the below :

    Dimension dimensions = driver.manage().window().getSize();
    int yMax = dimensions.getHeight();
    int xMax = dimensions.getWidth();
    ((TouchShortcuts) driver).tap(1, (int) (xMax * 0.95), (int) (yMax * 0.95), 200);

    This worked most of the devices with me (Around 10 android devices of different screen size)

  4. Hi
    I am also facing similar issue while automating iOS and android app using xamarin.uitest if anyone could suggest how can i tap on search button on keyboard.
    I have tried driver.KeyEvent() but it doesnt really works and not supportd in xamarin

  5. God bless you Vrushali, this post literally saved my life.
    I was able to send “Enter” through script now.

    Also, when can we expect something more from you with python?
    I’m truly enjoying all of your blog posts here…please continue to contribute and share knowlegde.

Leave a Reply

Your email address will not be published. Required fields are marked *