Friday, January 3, 2014

Different Type Of Wait in Selenium Web Driver


Sometime our test fails due to page was not loaded with all element for slow internet connection or may be the poor response time.So now question is how to handle this problems?

There is 2 different way to handle this.

1.Implicit Waits
2.Explicit Waits


1.Implicit Waits

WebDriver to wait for an element if they are not immediately available. So, WebDriver does not throw NoSuchElementException immediately.This is known as implicitlyWait().This can be achieved using :

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


Keep in mind:
1.If the DOM is present within the wait time it should not wait for the remaining time it go for the next step,
for an example:

 driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

Here we wait for 25 seconds, after that it gives NoSuchElementException.If the element present in 10 second then it should not waiting for another 15 seconds.

Cons:
1. No Condition it blindly wait for given seconds.
2.Once set, the implicit wait is set for the life of the WebDriver object instance. 

 2.Explicit Waits

A.Thread.sleep() 

One way is that, wait the running program for sometime, this can be achieved using:

Thread.sleep().

Cons:
1. It is not a better way because if the the element is present within this wait time, program does not move further until the wait time finished.
2. Some computer are fast, and others are slow, in a slow computer, an element may not show up within the wait time.
3.It will sleep time for script, not good way to use in script as it's sleep without condition.

B.Expected Condition

Another way is that we define to wait for a certain condition to occur before proceeding further in the code like :
        
        Wait<WebDriver> waitforelement=new WebDriverWait(driver, 10);
       
waitforelement.until(ExpectedConditions.elementToBeClickable(By.name("q"))); 

What we do here in this code? Here we check that element is click able or not and we try to wait 10 sec for this condition.If this condition fulfill with in 10 sec then it should not wait for remaining time. 

If we want to customize the Expected condition then we write in this way

       waitforelement.until(new ExpectedCondition<Boolean>(){
       
       @Override
        public Boolean apply(WebDriver f) {
            return f.findElement(By.name("q")).isEnabled();
        }
          
       });

 Or
       waitforelement.until(new Function<WebDriver, Boolean>() {

        @Override
        public Boolean apply(WebDriver f) {
               
return f.findElement(By.name("q")).isEnabled();    
    });


Another thing that we can also use for explecit wait, which is Fluent wait .Using this  the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, when searching for an element on the page.

It can be writen as:

       Wait<WebDriver> waitforelement=new FluentWait<WebDriver>(driver)
             .withTimeout(10, TimeUnit.SECONDS) 
             .pollingEvery(2, TimeUnit.SECONDS) 
             .ignoring(NoSuchElementException.class);
       waitforelement.until(ExpectedConditions.elementToBeClickable(By.name("q")));


Or
     waitforelement.until(new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver f) {
            return f.findElement(By.name("q")).isEnabled();
        }
    });


Or 
     waitforelement.until(new Function<WebDriver, Boolean>(){

        @Override
        public Boolean apply(WebDriver f) {
        return f.findElement(By.name("q")).isEnabled();
        }
    });




 

 



No comments:

Post a Comment