Tuesday, December 06, 2011

Testing autocomplete fields with Selenium

Sometimes we face some  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 . 
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.

While typing TEST11 in the autocomplete field 
If you 'll use simply type() method,
  1. selenium.type("locator-of-the-autosuggest-element", "TEST11");
it willn't work.

You need to use like the following:
  1. selenium.focus("locator-of-the-autosuggest-element");
  2. selenium.keyPressNative("32"); //space
  3. selenium.keyDownNative("16"); //SHIFT ON
  4. selenium.keyPressNative("84"); //T
  5. selenium.keyPressNative("69"); //E
  6. selenium.keyPressNative("83"); //S
  7. selenium.keyPressNative("84"); //T
  8. selenium.keyUpNative("16"); // SHIFT OFF
  9. selenium.keyPressNative("49"); //1
  10. selenium.keyPressNative("49"); //1
Or,simply you can use like following:
  1. selenium.type("locator-of-the-autosuggest-element", "TEST1");
  2. selenium.focus("locator-of-the-autosuggest-element");
  3. selenium.keyPressNative("49"); //1

Reading and Writing a Properties File

The following code shows how to read and write properties file. With getProperty(key) method we can get the property value by passing property key and like that way with setProperty(key,value) we can set given value to the specified property.

// Read properties file.
  1. Properties properties = new Properties();
  2. try {
  3.     properties.load(new FileInputStream("filename.properties"));
  4.     String s= prproperties.getProperty("Key1");
  5.     System.out.println(s); //Value1
  6. } catch (IOException e) {
  7.     e.printStackTrace();
  8. }
// Write properties file.
  1. try {
  2.     prproperties.setProperty("Key1", "ValueXxx");
  3.     properties.store(new FileOutputStream("filename.properties"), null);
  4. } catch (IOException e) {
  5.     e.printStackTrace();
  6. }
//filename.properties before write
  1. Key1=Value1
  2. Key2=Value2
  3. Key3=Value3
//filename.properties after write
  1. Key1=ValueXxx
  2. Key2=Value2
  3. Key3=Value3

Renaming a File or Directory

The following code will rename the file/directory.Here 'FileOrDirNameBefore' is the directory name to be renamed to 'FileOrDirNameAfter'.
  1. File f1= new File("FileOrDirNameBefore");
  2. File f2= new File("FileOrDirNameAfter");
  3. boolean success = f1.renameTo(f2);
  4. if (!success) {
  5.     // File was not successfully renamed
  6. }

Selenium Server problem: Failed to start: SocketListener0@0.0.0.0:4444

Sometimes after starting the server due to some problem test running browser get closed, in that case the started server( linked with the browser) can't stop and shows errors like :
Failed to start: SocketListener0@0.0.0.0:4444

Solution for this problem:
Type the below in the browser URL and the server will be stopped.
http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

Start and Stop Selenium Server Programatically

The Following code contains two methods.The first method startSeleniumServer() is for starting the server and the second method stopSeleniumServer() is to stop the started server.With this we are also able to trace out the server log to a file called "logs/seleniumServer.log" into your local machine. 
  1.  protected RemoteControlConfiguration rc ;
  2.  protected SeleniumServer seleniumServer ;
  3.  
  4.  public void startSeleniumServer() {      
  5.     try {
  6.       File logfile = new File("logs/seleniumServer.log");
  7.       rc = new RemoteControlConfiguration();  
  8.       rc.setLogOutFile(logfile);  
  9.       seleniumServer = new SeleniumServer(rc);
  10.       seleniumServer.start();        
  11.   } catch (Exception e) {
  12.       e.printStackTrace();
  13.   }
  14.   }
  15.  
  16.   public void stopSeleniumServer() throws Exception {
  17.      seleniumServer.stop();
  18.   }