Difference between Implicit Wait And Explicit Wait


2. Conditional Synchronization

          a)     Implicit Wait      

          b)     Explicit Wait   

A condition needs to be mentioned necessarily, along with timeout value . So that driver checks for the condition first and if it is full filled it comes out, even if the timeout is not reached.
Advantage - This will save on the unnecessary wait time.

Driver waits for condition or the timeout , whichever is achieved first . It comes out of the loop.So we must give a little more timeout in this case, so that if the condition (like element becoming visible) is taking time to become true, driver waits for it.     

a)     Implicit Wait :  

Must read : Absolute path vs relative path

In implicit wait , WebDriver polls the DOM for a certain amount of time while trying to find the element(s) specified.





It is a mechanism which will be written once and applied for entire session automatically. It should be applied immediately once we initiate the Webdriver.

Implicit wait works only for "FindElement" and "FindElements" commands.
When implicit wait, find element will restrain the driver from throwing an exception if the element is not found in first instance, instead it will poll for the element until the timeout and then proceeds further.


Limitation: - implicit wait will be applied to all elements of test case by default.


Page Load Timeout 

It is again an extension of Implicit wait but it used in page loading.It sets the amount of time Selenium to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
Syntax:-
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
waits for 20 seconds for page to load




b) Explicit Wait

Advantage - It is applicable to the given control and waits to the maximum amount of time or move forward in case the given condition is met. 
Note:- we need to import the webdriverwait class and expectedCondition Packages
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
Expected condition can be in the form of like:-
Expected condition:    visibilityOf
WebDriverWait.until(condition-that-finds-the-element(ExpectedCondition))


Syntax: driver.manage.TimeOuts.implicitwait(5,Timeunit.SECONDS);
Example:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("www.richasd.blogspot.com");



Implicit wait applies to all the controls of a browser ,but lets take a example where in one control takes bit longer to load compared to others.Does this means we should increase the time for implicit wait to accommodate it ? Answer is strict no, we will apply explicit wait on this control with an expected condition.



To check if the button is enabled ? 
Expected condition:    elementToBeClickable
To check if the text Box is visible
Syntax:-