Automate Android Actions using Appium


How to Automate Android Actions using Appium?

In this blog you will learn how to use the basic hardware keys on the phone or manage the events of the physical keys available in every Android device in during Android Automation Testing using Appium. Many of us faced such situation where we have to perform some events related to device like Press Home Button or Camera Click.

Android-OS provides a very easy method to simulate key/button press/release events via software. Every Android device is provided with physical keys with their KeyCodes. The KeyEvent class has multiple key constants that represent the different keys on a standard android device.

Automate Android Actions using Appium


  1. Launch the app
  2. Login into the app
  3. Go to Profile
  4. Change the Profile image using Camera

In the above test scenario we have to click image using device camera. For this we have to use android KeyCodes which will be performed using Android Shell Command.

Android Shell Command:-

The code samples below demonstrate how to send Key events such as the Camera Click, Enter Press, Search Key and the Backspace Key to an Android device in during Android App Automation.

Keyboard Event Method:-

public static void keyBoardEvent(int eventNumber)
Runtime.getRuntime().exec("cmd /C adb shell input keyevent " + eventNumber);
catch (Throwable t)

By passing the eventNumber , we are able to perform desired event.

For Camera Click, Event Number is 27.

Camera Click implementation:-

public void clickCamera()
System.out.print("Clicked on Camera ");

view raw
hosted with ❤ by GitHub

List of Android Events:-

Ket Constant   Description Event Number
KEYCODE_HOME int KEYCODE_HOME Home key. This key is handled by the framework and is never delivered to applications. Constant Value: 3 (0x00000003)
KEYCODE_BACK int KEYCODE_BACK Back key. Constant Value: 4 (0x00000004)
KEYCODE_CALL int KEYCODE_CALL Call key. Constant Value: 5 (0x00000005)
KEYCODE_CAMERA int KEYCODE_CAMERA Camera key. Used to launch a camera application or take pictures. Constant Value: 27 (0x0000001b)
KEYCODE_CLEAR int KEYCODE_CLEAR Clear key. Constant Value: 28 (0x0000001c)
KEYCODE_COMMA int KEYCODE_COMMA  ‘,’ key. Constant Value: 55 (0x00000037)
KEYCODE_TAB int KEYCODE_TAB Tab key. Constant Value: 61 (0x0000003d)
KEYCODE_SPACE int KEYCODE_SPACE Space key. Constant Value: 62 (0x0000003e)
KEYCODE_ENTER int KEYCODE_ENTER Enter key. Constant Value: 66 (0x00000042)
KEYCODE_DEL int KEYCODE_DEL Backspace key. Deletes characters before the insertion point, unlike KEYCODE_FORWARD_DEL. Constant Value: 67 (0x00000043)
KEYCODE_EQUALS int KEYCODE_EQUALS =’ key. Constant Value: 70 (0x00000046)
KEYCODE_MENU int KEYCODE_MENU Menu key. Constant Value: 82 (0x00000052)
KEYCODE_NOTIFICATION int KEYCODE_NOTIFICATION Notification key. Constant Value: 83 (0x00000053)
KEYCODE_MUTE int KEYCODE_MUTE Mute key. Mutes the microphone, unlike KEYCODE_VOLUME_MUTE. Constant Value: 91 (0x0000005b)
KEYCODE_PAGE_UP int KEYCODE_PAGE_UP Page Up key. Constant Value: 92 (0x0000005c)
KEYCODE_PAGE_DOWN int KEYCODE_PAGE_DOWN Page Down key. Constant Value: 93 (0x0000005d)



By implementing the above methods, you will improve the control on your android app automation techniques.



2 thoughts on “Automate Android Actions using Appium

Leave a Reply

%d bloggers like this: