QaMantra: How to use Explicit and Implicit Wait in Selenium

How to use Explicit and Implicit Wait in Selenium

Implicit Wait :


An implicit wait tells WebDriver or Selenium Scripts to poll the DOM for a certain amount of time when Selenium Scripts trying to find an Object/Element or Object/Elements if they are not visible or Interactable.
     
                              Implicit wait condition may slow down your tests Script Execution when an application responds normally, It will wait for each Object/Element appearing in the DOM and increase the overall tests Script Execution duration. It is Highly Suggested to avoid or reduce the usages of an Implicit wait and try to handle synchronization issues with an Explicit wait , which is much Effective ,Efficient and Convenient to use as compared to an Implicit wait .

Syntax of Implicit Wait in Selenium

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

How to use Implicit wait for 10 seconds


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


Program


package QaMantra.test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

public class AppTest {

 

 protected WebDriver driver;

 @Test

 public void QaMantratutorials() throws InterruptedException

 {

 System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );

 driver = new ChromeDriver();

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

 String ExpectedTitle = "Demo QaMantra Page";

 String aTitle = "" ;

 // launch Chrome and redirect it to the Base URL

 driver.get("http://www.qamantra.com/2018/01/how-to-use-explicit-and-implicit-wait.html" );

 //Maximizes the browser window

 driver.manage().window().maximize() ;

 //get the actual value of the title

 QaMantraTitle = driver.getTitle();

 //compare the actual title with the expected title

 if (QaMantraTitle.equals(ExpectedTitle))

 {

 System.out.println( "Test Passed") ;

 }

 else {

 System.out.println( "Test Failed" );

 }

 //close browser

 driver.close();

}

}
                                   
                                      Implicit wait Have two parameters, the first parameter is time as an integer value and the second parameter is time Unit aceptable in SECONDS, MINUTES, MILISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.

Explicit Wait :


Explicit wait tell the WebDriver to wait for certain time on basis of certain Expected conditions before throwing an "ElementNotVisibleException" exception ,Explicit wait is specific wait applied only for specified elements. Explicit wait have better flexibility then Implicit wait. 
             
                                           Selenium WebDriver provides the 'WebDriverWait' and 'ExpectedCondition' classes to Implement Explicit wait condition , 'ExpectedCondition' class provides a set of predefined conditions to wait before proceeding further
in Script.
            
                                        
     
Predefined conditionSelenium WebDriver (Java)
An element is visible and enabledelementToBeClickable(By locator)
Presence of an elementpresenceOfElementLocated(By locator)
An element is selectedelementToBeSelected(WebElement element)
Specific text present in an elementtextToBePresentInElement(By locator,
java.lang.String text)
TitletitleContains(java.lang.String title)
Element valuetextToBePresentInElementValue(By locator, java.lang.String text)


     

Explicit Wait Condition

Syntax

                         
WebDriverWait wait = new WebDriverWait(driver,TimeOut);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By locator));

                                     
  TimeOut is an Integer Value which have unit is Seconds .
              


package QaMantra.test;
 
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
 
import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.chrome.ChromeDriver;
 
import org.testng.annotations.Test;
 
public class QaMantra {
 
  
 
 public WebDriver driver;
 
 @Test
 
 public void QaMantratutorials() throws InterruptedException
 
 {
 
 System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
 
 driver = new ChromeDriver();
 
 //Implicit wait
 driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
 
 String ExpectedTitle = "Demo QaMantra Page";
 
 String aTitle = "" ;
 
 // launch Chrome and redirect it to the Base URL
 
 driver.get("http://www.qamantra.com/2018/01/how-to-use-explicit-and-implicit-wait.html" );
 
 //Maximizes the browser window
 
 driver.manage().window().maximize() ;
 
 //get the actual value of the title
 QaMantraTitle = driver.getTitle();
 
 //compare the actual title with the expected title
 
 if (QaMantraTitle.equals(ExpectedTitle))
 
 {
 
 System.out.println( "Test Passed") ;
 
 }
 
 else {
 
 System.out.println( "Test Failed" );
 
 }
  //Explicit wait 
WebDriverWait wait  =  new WebDriverWait(driver,10);
WebElement QaMantra  =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='widget-content popular-posts'])[1]")));
QaMantra.click();

}
};

No comments: