0% found this document useful (0 votes)
5 views7 pages

Synchronization

Synchronization in automation testing ensures that scripts wait for web pages or elements to be ready before executing actions, preventing errors like NoSuchElementException. There are four types of wait commands in Selenium: Thread.sleep(), Implicit Wait, Explicit Wait, and Fluent Wait, each serving different purposes in managing timing. Proper use of these wait commands is essential to align script execution speed with application response times.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Synchronization

Synchronization in automation testing ensures that scripts wait for web pages or elements to be ready before executing actions, preventing errors like NoSuchElementException. There are four types of wait commands in Selenium: Thread.sleep(), Implicit Wait, Explicit Wait, and Fluent Wait, each serving different purposes in managing timing. Proper use of these wait commands is essential to align script execution speed with application response times.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Synchronization

Synchronization in automation testing means making your script wait


until the web page or element is ready before performing actions.

(Or)

The process of matching selenium script speed with the application speed
is called as Synchronization.

Without synchronization, Selenium may try to click or read something


before it loads, causing errors.

Why Synchronization is Needed

Web pages load at different speeds due to:

 Internet speed

 Server response

 Animations

So we must control timing to avoid failures like:

 NoSuchElementException.
 ElementNotInteractableException.

* In selenium, synchronization can be done using 4 wait commands.

Types of Wait Commands :

1. [Link]()/Static Wait

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() and findElements().

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

If the condition is not satisfied in the specified time period, it throws


‘TimeOutException’.

Syntax :

WebDriverWait wait = new WebDriverWait(driver,time_in_seconds);

It tells Selenium:

“Wait up to this much time until my condition becomes true.”

 [Link](condition);
Conditions :

[Link] Of Element

[Link]([Link](element));

 Waits until element is visible on screen


 Returns → WebElement

Means you can store it:

WebElement e = [Link]([Link](element));

[Link] To Be Clickable

[Link]([Link](element));

Waits until element is:

 visible

 enabled

Returns → WebElement

Used before clicking buttons.

[Link] Contains

[Link]([Link]("Title_of_the_Webpage"));

 Waits until page title contains given text


 Returns → boolean

Example:

boolean status = [Link]([Link]("Login"));

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.
IMPLICIT WAIT

public class WebDriverMethods {

public static void main(String[] args) throws InterruptedException


{

WebDriver driver = new ChromeDriver();

[Link]().window().maximize();

//used for navigation

[Link]().timeouts().implicitlyWait([Link](10));

[Link]("[Link]

[Link]([Link]());

[Link]([Link]("//a[@class='link
facebook']")).click();

[Link]([Link]("//a[@class='link
twitter']")).click();

Set<String> id = [Link]();

[Link](id);

for(String sid:id)

[Link]().window(sid);

if([Link]().contains("[Link]
UCmQRa3pWM9zsB474URz8ESg"))

[Link]();
}

}}

EXPLICIT WAIT

public class ExpliciWait {

public static void main(String[] args) {

WebDriver driver = new ChromeDriver();

[Link]().window().maximize();

[Link]("[Link]
scenario=1");

WebDriverWait wait = new


WebDriverWait(driver,[Link](6));

[Link]([Link]([Link]
h("//section[text()='Dropdown']")));

[Link]([Link]("//section[text()='Dropdown']")).click
();

[Link]([Link]([Link]
h("//a[text()='Multi Select']")));

[Link]([Link]("//a[text()='Multi
Select']")).click();

[Link]();

They solve different problems:

Wait Type Purpose

Implicit waits for elements


Wait Type Purpose

Wait globally

[Link] pauses execution


ep completely

Some cases cannot be handled by waits, like:

 waiting for animation to finish

 waiting for file download

You might also like