Type Text Using Java Robot Class


When it comes to test automation using Selenium Webdriver, There are scenarios where typing in a textbox using the SendKeys() function does not actually perform the expected outcome

Eg:- I came across a particular date field that didn't accept the value correctly in either SendKeys(), Javascript or using the Actions class.

Therefore, the alternative solution was to use the Java Robot class(java.awt.Robot) to send the required key sequence.
Robot class is used to generate native system input events for the purposes of test automation and other applications where control of the mouse and keyboard is required.
Unlike selenium, this would actually simulate a key press or mouse movement on the computer like a real user does. For example, Robot.mouseMove will actually move the mouse cursor.

However, this class doesn't have a method to type a string of text, rather just a single key code.

Below SmartRobot class which is an extended version of the Robot class provides few methods to achieve this: (Reference: http://gruimed.blogspot.com/2009/09/using-java-robot-to-type-text-strings.html)

public class SmartRobot extends Robot {

 public SmartRobot() throws AWTException {
  super();
 }

 public void typeKey(int keyCode) {
  keyPress(keyCode);
  delay(100);
  keyRelease(keyCode);
 }

 public void typeKey(int keyCode, int keyCodeModifier) {
  keyPress(keyCodeModifier);
  keyPress(keyCode);
  delay(100);
  keyRelease(keyCode);
  keyRelease(keyCodeModifier);
 }

 /**
  * Type the text
  * @param text value to type
  */
 public void type(String text) {
  for (int i = 0; i < text.length(); ++i) {
   typeChar(text.charAt(i));
  }
 }

 /**
  * This will handle finding the keyCode of a special characters and 
  * decide whether pressing Shift Key to simulate that, and call the correct keyType function
  * @param c character
  */
 private void typeChar(char c) {
  boolean shift = true;
  int keyCode;

  switch (c) {
  case '~':
   keyCode = (int) '`';
   break;
  case '!':
   keyCode = (int) '1';
   break;
  case '@':
   keyCode = (int) '2';
   break;
  case '#':
   keyCode = (int) '3';
   break;
  case '$':
   keyCode = (int) '4';
   break;
  case '%':
   keyCode = (int) '5';
   break;
  case '^':
   keyCode = (int) '6';
   break;
  case '&':
   keyCode = (int) '7';
   break;
  case '*':
   keyCode = (int) '8';
   break;
  case '(':
   keyCode = (int) '9';
   break;
  case ')':
   keyCode = (int) '0';
   break;
  case ':':
   keyCode = (int) ';';
   break;
  case '_':
   keyCode = (int) '-';
   break;
  case '+':
   keyCode = (int) '=';
   break;
  case '|':
   keyCode = (int) '\\';
   break;
  case '"':
   keyCode = KeyEvent.VK_QUOTE;
   break;
  case '?':
   keyCode = (int) '/';
   break;
  case '{':
   keyCode = (int) '[';
   break;
  case '}':
   keyCode = (int) ']';
   break;
  case '<':
   keyCode = (int) ',';
   break;
  case '>':
   keyCode = (int) '.';
   break;
  default:
   keyCode = (int) Character.toUpperCase(c);
   shift = Character.isUpperCase(c);
   break;
  }
  if (shift)
   typeKey(keyCode, KeyEvent.VK_SHIFT);
  else
   typeKey(keyCode);
 }

}


No comments

Powered by Blogger.