Tuesday, February 8, 2011

selenium test auto suggest

There are few editboxes in the application that shows suggestions while you type. In some of the application you need to choose from that suggestions only to filled the edit box else it won't consider as in my application while in other application it is optional, like google search engine.

If you will simple type in the editbox using type method it won't give you any suggestion coz it works on keyevent so to see the suggestion you will require to use the keyPressNative method.

following code will open the google.com, will type the "selenium handle" in the search editbox and will choose the first option available and will search that.

public class AutoSuggest {
 Selenium selenium;
 @Before
 public void setUp() throws Exception {
  selenium = new DefaultSelenium("localhost", 2323, "*chrome", "http://www.google.co.in/");
  selenium.start();
 }

 @Test
 public void testUntitled() throws Exception {
  selenium.open("/");
  enterKeyStrokes("selenium handle");
  assertTrue(selenium.isTextPresent("Gaurang Shah"));
 }

 public void enterKeyStrokes(String str) throws InterruptedException {
  char[] strarry = str.toUpperCase().toCharArray();
  selenium.click("q");
  Thread.sleep(1000);
  for(int chars=0; chars<strarry.length; chars++){
   selenium.keyPressNative(""+(int)strarry[chars]);
   Thread.sleep(1000);
  }
  selenium.keyPressNative(String.valueOf(KeyEvent.VK_DOWN));
  selenium.keyPressNative(String.valueOf(KeyEvent.VK_ENTER)); // press enter
  Thread.sleep(2000);
 }
@After
public void tearDown() throws Exception {
 selenium.stop();
}

0 comments:

Post a Comment