Basic Selenium Synchronization
SYNCHRONIZATION
The process of matching selenium script speed with the application speed is called
as Synchronization.
When the page loads on browser, various web elements on it may load at different
time intervals.
If the selenium script speed is faster than the application speed, it throws an
exception ( i.e., ‘NoSuchElementException’ or ‘ElementNotVisibleException’ )
So, we make use of wait commands to direct a test script to pause for a certain time
before throwing exception.
In selenium, synchronization can be done using 4 wait commands.
Types of Wait Commands :
1. [Link]()
2. Implicit Wait
3. Explicit Wait
4. Fluent Wait
Note:
We go for wait commands to reduce the usage of [Link]()[Blind wait/ Dead
wait/ Hardcoded wait]
1. [Link]() :
It is java wait statement which waits completely for the specified amount of time.
It throws ‘InterruptedException’.
It accepts “time in milliseconds” as an argument.
Disadvantage of [Link]() :
It is hard coded wait statement, where mandatorily the script is going to wait for
the given seconds irrespective with the loading time of an application.
2. Implicit Wait :
It is selenium wait statement which is going to wait implicitly without specifying
any condition
It is ‘Global wait statement’ which means declare once and it will be holding for
entire duration of the test
It is applicable for each and every element on the webpage because it
synchronizes for findElement() andfindElements().
Page1|3
Basic Selenium Synchronization
Both implicit wait and Explicit wait can be used in same script, but there the first
preference will be given to implicit wait and then goes to Explicit wait.
It internally waits for particular period of time till the element is present, which is
called as “Polling period”
Syntax :
[Link]().timeouts().implicitlyWait([Link](“time_in_seconds”)
);
3. Explicit Wait :
In Explicit wait you have to specify a waiting condition and it is going to wait only
for that condition
Explicit wait statement should be specified every time whenever synchronization
is needed.
It can be achieved by ‘ WebDriverWait’ Class
It internally waits for particular period of time till the condition is satisfied, which is
called as “Polling period”
POLLING PERIOD :
It is the time interval in which selenium starts searching again for an element after
it’s last failed try.
The default polling time is 500ms or 0.5sec
Page2|3
Basic Selenium Synchronization
If the condition is not satisfied in the specified time period, it throws
‘TimeOutException’.
Syntax :WebDriverWait wait = new WebDriverWait(driver,time_in_seconds);
Conditions
[Link]([Link](element)); // Return Type is “WebElement”
(or)
[Link]([Link](element)); // Return Type is
“WebElement”
(or)
[Link]([Link](“Title_of_the_Webpage”)); // Return Type is
“boolean” …etc.
4. Fluent Wait :
Fluent wait is same as Explicit wait.
Fluent wait statement should be specified every time whenever synchronization is
needed
Fluent Wait is used to customize the polling period.
It ignores any exception that might occur before timeouts.
It is not widely used in Industry.
Syntax:
FluentWait wait = new FluentWait<>(driver)
.withTimeout([Link](20))
.pollingEvery([Link](2))
.ignoring([Link]);
WebElement element = [Link](driver -
>[Link]([Link]("username")));
*********************************************************************************************************
Interview Questions
1. What is Synchronization in Selenium?
2. What are the different types of waits in Selenium?
3. What is the difference between Implicit and Explicit wait?
4. What is the default polling interval of Explicit/Fluent Wait?
5. What is [Link]()? Should we use it?
6. What are ExpectedConditions in Selenium?
7. How do you implement Fluent Wait?
8. What happens if the element appears before the wait time?
9. What happens if the element does not appear within wait time?
10. What is the difference between Explicit Wait and Fluent Wait?
11. Can we use both Implicit and Explicit waits together?
12. How do you handle Ajax elements in Selenium?
13. How do you wait for a page to load completely?
14. What is the difference between Static Wait and Dynamic Wait?
15. What are common synchronization problems in Selenium?
Page3|3