Tuesday, March 29, 2011

selenium Timed out after 30000ms error

"Timed out after 30000ms error" is one of the most frequent error of selenium. However the solution of this is very simple, but before we look into it let's see why we are getting this error.

Mostly this error appears after the following statement
selenium.waitForPageToLoad("30000");

Now let's see why it comes.
waitForPageToLoad(timeout) API says that, it will wait for the giving timeout and if page hasn't loaded withing that time it will return error. and so there are two reason it returns error.
  • timeout given is less that what it requires to load the page
  • page is not loading at all, it's just a AJAX call and only part of the page is being updated.

Now let's see the solultion.
The best solution for this is not to use waitForPageToLoad API, rather to wait for some specific element on the page to appear.

For example I am rather than waiting for the google page to load I am waiting until the searchbox is visible.

//Wait until searchbox appears or 3 min
waitForElementPresent("q",3); 
/**
  * @author Gaurang
  * @param xpath
  * @param timeout (minutes)
  * @throws InterruptedException
  * @return returns true if element found in given time else false
  *  Wait for specified time till specific element is not present 
  */
 public boolean waitForElementPresent(String xpath, int timeout) throws InterruptedException{
  int count = 0;
  while(selenium.isElementPresent(xpath) != true){
   Thread.sleep(10*1000); //Wait 10 seconds
   if(count++ > timeout*6 ) break;
  }
  
  if(selenium.isElementPresent(xpath))
   return true;
  else
   return false;
    
 }

Wednesday, March 16, 2011

Selenium with Flex

Recently I just got the chance to check the selenium support with Flex. And after few minutes spending on google I finally find almost everything required for to test flex application.
To provide flex support to selenium is easy, you just need to add few more JAR files but there is a minor problmes too.
  1. You require your developers to rebuild your application with provided library file (SeleniumFlexAPI.swc) by selenium flex
  2. It's not working with Firefox 3.0 or greater version (at least it didn't work with me !!, I am still searching for the solution)

Now let's see how to test flex application with selenium step by step.
  1. Rebuild your flex application
    • Download the zip file from here and extract the zip file
    • In FlexBuilder, add the SeleniumFlexAPI.swc in the /src folder, then build your application with -include-libraries SeleniumFlexAPI.swc as the additional compiler argument
  2. Include the JAR files in the the project
  3. Before you began coding you require following jar files in your build in addition to selenium-java-client-driver.jar
    flashselenium-java-client-extension.jar
    flex-ui-selenium.jar
  4. Write the code
    okay let's write the code.. no no no hang on !! before we write the code we need to identify the elements of the flex application.  I am using following add-on of Firefox to identify the elements.
    FlashFirebug (this is actually a extension of the firebug add-on)
  5. /**
     * @author Gaurang
     */
    import org.junit.Before;
    import org.junit.Test;
    import static org.junit.Assert.*;
    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.FlexUISelenium;
    import com.thoughtworks.selenium.Selenium;
    
    public class TestFlex {
     Selenium selenium;
      private FlexUISelenium flexUITester;
     @Before
     public void setUp() throws Exception {
      selenium = new DefaultSelenium("localhost", 2323, "*chrome", "http://qtp-help.blogspot.com");
      selenium.start();
      while(selenium.isElementPresent("myInput"))
      Thread.sleep(1000);
      selenium.open("/2011/03/flex-example.html");
      flexUITester = new FlexUISelenium(selenium, "selenium_demo");
     }
     
     @Test
     public void test() {
      //Enter text
      flexUITester.type("Gaurang").at("myInput");
      flexUITester.click("myButton");
      assertEquals("Gaurang", flexUITester.readFrom("myText"));
     }
    }
    
    

Flex example