Tuesday, November 15, 2011

Selenium - Autosuggest edit field

keyPressNative method will be used to handle autosuggestion.
keyPressNative method -Simulates a user pressing and releasing a key by sending a native operating system keystroke. This function uses the java.awt.Robot class to send a keystroke; this more accurately simulates typing a key on the keyboard. It does not honor settings from the shiftKeyDown, controlKeyDown, altKeyDown and metaKeyDown commands, and does not target any particular HTML element. To send a keystroke to a particular element, focus on the element first before running this command.

public void selectAutosuggest(String webElementLocator,String inputValue){
char[] stringArray = inputValue.toUpperCase().toCharArray();
selenium.click(webElementLocator);
for(int i=0;i &lt stringArray.length ; i + +)
selenium.keyPress(webElementLocator,""+(int)stringArray[i]);
//Press downarrow and click EnterKey
selenium.keyPressNative(String.valueOf(KeyEvent.VK_DOWN));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_ENTER));
Thread.sleep(1000);

}

No comments: