Showing posts with label thread.sleep(). Show all posts
Showing posts with label thread.sleep(). Show all posts

Thursday 9 January 2014

Selenium WebDriver: How to Handle Ajax Application

A common problem we face during automation using selenium WebDriver is to handle the ajax calls. It is difficult to know when the call is complete and page has been updated. There are several ways to handle the ajax calls. This blog describes them one by one.
We can handle Ajax calls in two ways:


1. Hard Wait - this is most commonly used method but not recommended while doing the automation. Only in some exceptional cases use or for short time use it
·    JAVA: Use java method Thread.Sleep( to suspend execution for specified time. Syntax –

Thread.sleep(1000) // time unit is millisecond

·   Implicit Wait – An implicit wait is to tell WebDriver to stop the DOM for a certain amount of time. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. Syntax –

driver.manage().timeouts().Implicitly Wait(10, TimeUnit.SECONDS); 
// Time is in  seconds or we can also change Unit in second argument

2.   Explicit Wait: An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. Sample code is as follows -

WebDriver driver = new FirefoxDriver();
driver.get("http://google.co.in");
WebDriverWait myDElement = new WebDriverWait(driver, 10)

.until(ExpectedConditions.presenceOfElementLocated(By.xpath("xxxxx")));