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) = @_;
#Key press native function do not need a locator. So to make selenium type in the correct inbox field, we need to focus on that field first.
$selenium->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 $upper_case = 0;
my $special_char = 0;
my @char_arr = split(//, $text);
foreach my $char (@char_arr) {
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.
Thanks,
Rajat Jindal
http://quicksilver1183-tech.blogspot.com
http://quicksilver1183.com