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;
    
 }

2 comments:

Anonymous said...

int count = timeout*6;
while(selenium.isElementPresent(xpath)){
Thread.sleep(10*1000); //Wait 10 seconds
if(count-- < 0) break;
}
return count > 0;

Hope said...

Thanku so much ... it was really useinformation

Post a Comment