How keyPressNative works on Selenium

Lately I had been facing an issue to simulate a functionality with Selenium. The functionality was of Auto Complete feature. [Yes, just like you see when typing something in Google search box.

I tried to use type command, but it just placed the value into the text field and Auto Complete feature was not showing up.

After banging my head with code and different functions I was finally able to find something that I would like to share with all. [Guys, my understanding can be wrong as well, so please correct me If I am wrong, and leave a comment regarding the same].

There are two types of code:

a. char code

b. key code

Consider we have two char ‘a’ and ‘A’. Now, simplest way to differentiate between char code and key code is that, both ‘a’ and ‘A’ have same key code but different char code.

Does it make any sense to you? If not read it again. This time one word at a time, and slowly.

“char code for ‘a’ and ‘A’ are different, but keycode for both of them is same. “

Selenium key_press_native accepts the ascii code for the char that you want to type in. But internally it matches it with the keycode of the char and therefore when you try to type in ‘A’ or ‘a’ [with their ascii values], you will see different results as selenium will not be able to find the key code for ‘a’.

Similar issue is faced while typing in the value for keys that require us to press SHIFT key. e.g. To type in #, we have to do first press ‘SHIFT’ and then press numeric ‘3’. Similarly fr & we need to press first ‘SHIFT’ and then ‘7’.

so the solution that I used finally for this was as follows:

Algo:

1. Prepare a hash map for special symbols, that require us to press SHIFT before using them. The key here would be the symbol and value would be the ascii code for the key that is to be pressed after pressing shift.

2. For UPPERCASE char also, we need to press first of all the ‘SHIFT’ key and then the key that is to be pressed.

so let us write down a small function doing this trick for us.

I am Perl guy, so do not mind me writing the code with Perl reference here.

sub type_some_text {
   my ($class, $locator, $text) = @_;

   $class->focus($locator);

   my $ascii_map = (
 SHIFT => 16,
 ':' => 59,
 '<' => 44,
 '>' => 46,
 '?' => 47,
 '"' => 222,
 '_' => 45,
 '+' => 61,
 '{' => 91,
 '}' => 93,
 '(' => 57,
 ')' => 48,
 '&'=> 55,
 '%'=> 53,
 '*'=> 56,
 '$'=> 52,
 '!' => 49,
 '@' => 50,
);
 
 my @char_arr = split(//, $text);
 foreach my $char (@char_arr) {
my $upper_case = 0;
 my $special_char = 0;
  if($char ne lc($char) {
   $upper_case = 1;
  }
  if($ascii_map{$char}) {
   $special_char = 1;
  }
  if($upper_case || $special_char) {
   $selenium->key_down_native($ascii_map{'SHIFT'});
  }
  if($ascii_map{$char}) {
   $selenium->key_press_native($ascii_map{$char});
  }else {
   $selenium->key_press_native(ord(uc($char)));
  }
  if($upper_case || $special_char) {
   $selenium->key_up_native($ascii_map{'SHIFT'});
  }
 }
}

Now with the above stated function, we can simulate the key strokes,and therefore the Auto Complete feature.

Hope it was of some help.

Edit: Updated the code to re-initialize upper_case & special_char values in for loop per comments from Itamar. Thanks buddy.

Tagged with: , , ,
Posted in Selenium/Webdriver
2 comments on “How keyPressNative works on Selenium
  1. Itamar says:

    Thanks bro! saved me few hours of banging my head against a wall.

  2. Itamar says:

    btw:
    line 4: $class->focus($locator); instead of $selenium->focus($locator);

    line 30: close bracket
    if($char ne lc($char)) {

    $selenium->key_down_native(16) instead of shift (didn’t work for some reason)

    and last:

    foreach my $char (@char_arr) {

    $upper_case = 0;
    $special_char = 0;

    otherwise the shift key is kept pressed due to upper_case remaining 1

Leave a Reply

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

*