Monday 26 February 2018

Implicit Wait Vs. Explicit Wait Vs. Fluent Wait

When we do automation in selenium then waits for element is most important part in automation.

Why We Need Wait In Selenium?

When any page is load then different elements take different time to load or visible on page. So, if we do not handle wait in automation then "ElementNotVisibleException" exception will be thrown.

So, we have to handle waits in automation framework to overcome synchronization problem.

We can handle wait in three ways.
  1. Implicit Wait
  2. Explicit Wait
  3. Fluent Wait

Implicit Wait :

This wait will tell web driver to wait certain amoutn of time before thrown "ElementNotVisibleException" exception.

Syntax:

1
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class Test 
{
 
 public WebDriver driver;

 @Test
 public void TestWait() throws InterruptedException 
 {
 System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
 driver = new ChromeDriver(); 
 driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;

 driver.findElement(By.id("txtUserId")).sendKeys(vUsername);   
 driver.findElement(By.id("txtPassword")).sendKeys(vPassword);
 driver.findElement(By.id("btnSignIn")).click();

 }

}

In above example, you can see timeout is for 10 seconds. So web driver will wait for 10 seconds if any element is not visible on page and after 10 seconds it will throw "ElementNotVisibleException" exception.


Explicit Wait :

This wait will tell web driver to wait till certain condition (Expected Condition) or maximum amount of time before throwing an "ElementNotVisibleException" exception.

Explicit wait is smart type of wait but it can only be applied specific elements. Once we declared explicit wait then we have to declare "Expected Condition" till which we want to wait.

Syntax:


1
WebDriverWait wait = new WebDriverWait(WebDriver,TimeOut);

Example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Test 
{
 
 public WebDriver driver;

 @Test
 public void TestExplicitWait() throws InterruptedException 
 {
 System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
 driver = new ChromeDriver(); 
 
 WebDriverWait wait = new WebDriverWait(driver, 15);

 driver.findElement(By.id("txtUserId")).sendKeys(vUsername);   
 driver.findElement(By.id("txtPassword")).sendKeys(vPassword);

 WebElement SignIn = wait.until(ExpectedConditions.elementToBeClickable(By.id("btnSignIn")));

 SignIn.click(); 

 }
}

In above example, you can see first we set webdriverwait for 15 seconds. It means it will wait maximum 15 seconds then we set expected condition "elementToBeClickable" for SignIn element.

There are many expected conditions. We can use any of them as per our requirement.


Fluent Wait :

The fluent wait will tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.

Frequency: it is repeat cycle with the time to verify the condition at the regular interval of time.

you can also specify Exception which you want to ignore to throw when webdriver is checking for particular element.

Syntax:


1
2
3
4
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);

Example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Test 
{
 
 public WebDriver driver;

 @Test
 public void TestFluentWait() throws InterruptedException 
 {
 System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
 driver = new ChromeDriver(); 
 
 Wait wait = new FluentWait(driver)
   .withTimeout(20, TimeUnit.SECONDS)    
   .pollingEvery(3, TimeUnit.SECONDS)    
   .ignoring(NoSuchElementException.class);

 driver.findElement(By.id("txtUserId")).sendKeys(vUsername);   
 driver.findElement(By.id("txtPassword")).sendKeys(vPassword);

 WebElement SignIn = wait.until(new Function() {
 
        public WebElement apply(WebDriver driver) {
 
        return driver.findElement(By.id("btnSignIn"));
 
        }
 
       });

 SignIn.click(); 

 }
}


In above example, you can see maximum timeout is set for 20 seconds.
Frequency is set for every 3 seconds by ignoring "NoSuchElementExecption".

No comments:

Post a Comment

Popular