Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Aug 02, 2017 12:14

By Daniel Tourgman

Use pressKeyCode() method for Appium or mobile:key:event Perfecto command for Selenium to send special key codes to the device.

Step-by-step guide

Android devices that run versions above 5.0 support KeyEvents, where each KeyEvent corresponds to an action from the keyboard or the special system keys of the device. The code samples below demonstrate how to send Key events such as the Enter/Search Key and the Backspace Key to an Android device.

Transmitting the KeyEvent code to the device is performed slightly differently for each of the testing environment that you are working in:

  • Selenium RemoteWebDriver - use the mobile:key:event Perfecto command with driver.executeScript().
  • Appium AndroidDriver - use the pressKeyCode() method.
  • UFT - use the SendKeyEvent method of the Device.

 

Remote Web Driver

driver.findElementByXPath("//input[@id=\"lst-ib\"]").click();
driver.getKeyboard().sendKeys("Perfecto Mobile communit*");
Map<String, Object> BackspaceKeyEvent  = new HashMap<>();
BackspaceKeyEvent.put("key", "67");
driver.executeScript("mobile:key:event", BackspaceKeyEvent);
driver.getKeyboard().sendKeys("y");
Map<String, Object> EnterKeyEvent  = new HashMap<>();
EnterKeyEvent.put("key", "66");
driver.executeScript("mobile:key:event", EnterKeyEvent);

Appium

driver.findElementByXPath("//input[@id=\"lst-ib\"]").click();
driver.getKeyboard().sendKeys("Perfecto Mobile communit*");
//KeyEvent 67= Backspace key. Deletes characters before the insertion point        
driver.pressKeyCode(67);
driver.getKeyboard().sendKeys("y");
//KeyEvent 66= Enter key        
driver.pressKeyCode(66);

UFT Mobile

Device("DUT").Type "Perfecto Mobile communit*"
//KeyEvent 67= Backspace key. Deletes characters before the insertion point        
Device("DUT").SendKeyEvent 67

Device("DUT").Type "y"
//KeyEvent 66= Enter key        
Device("DUT").SendKeyEvent 66

Android KeyCodes List

The following is a partial list of some commonly used KeyCodes:

  • 'a' - 'z' --> 29 - 54
  • '0' - '9'--> 7 - 16
  • SPACE --> 62
  • ENTER ---> 66
  • BACKSPACE --> 67
  • BACK --> 4
  • CALL --> 5
  • ENDCALL --> 6

You can find a complete list of Key Codes and an explanation here:
http://developer.android.com/reference/android/view/KeyEvent.html