0% found this document useful (0 votes)
4 views

Synchronization in Selenium

The document discusses synchronization in Selenium, emphasizing the importance of managing timing issues in real-time testing of dynamic web applications. It details various waiting mechanisms including Implicit Wait, Explicit Wait, Fluent Wait, and Static Wait (Thread.sleep()), explaining their functionalities and use cases. The document highlights how these synchronization techniques help ensure that test scripts execute smoothly by waiting for elements to become available or for specific conditions to be met.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Synchronization in Selenium

The document discusses synchronization in Selenium, emphasizing the importance of managing timing issues in real-time testing of dynamic web applications. It details various waiting mechanisms including Implicit Wait, Explicit Wait, Fluent Wait, and Static Wait (Thread.sleep()), explaining their functionalities and use cases. The document highlights how these synchronization techniques help ensure that test scripts execute smoothly by waiting for elements to become available or for specific conditions to be met.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Synchronization in Selenium

In real-time testing scenarios, synchronization in Selenium ensures that the test scripts execute
smoothly by addressing timing issues between the automation script and the web application under
test.

1. Dynamic Nature of Web Applications

• Modern web applications often use AJAX or dynamic loading to fetch data asynchronously.
This can lead to situations where elements are not immediately available or are still being
updated after a page load.

• Without synchronization, Selenium may try to interact with an element that hasn’t yet been
rendered, resulting in exceptions like:

o NoSuchElementException

o StaleElementReferenceException

Implicit wait :

Implicit wait is one of the synchronization mechanisms in Selenium WebDriver. It defines a global
waiting time for the WebDriver to wait for an element to become available in the DOM before throwing
a NoSuchElementException. This waiting time is applied to all WebElement searches throughout the
WebDriver instance.

Implicit Wait in Selenium is considered a dynamic wait because it dynamically waits for a specified
amount of time until the condition (finding the element) is met, rather than waiting for a fixed duration.

How It Works

1. When an element is not immediately available in the DOM, Selenium waits for the specified
duration before throwing an exception.

2. During this wait time, the WebDriver polls the DOM periodically to check if the element has
become available.
3. If the element appears before the timeout, WebDriver proceeds with the next step in the script
without waiting for the full duration.

Example :

// Set implicit wait to 10 seconds


driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

// Locate an element
WebElement element = driver.findElement(By.id("example"));

How Implicit Wait Really Works

1. If the Element is Present:

o Selenium will immediately proceed as soon as the element is located. It does not wait
unnecessarily for the entire timeout duration.

2. If the Element is Not Present:

o Selenium will continuously poll the DOM (default polling interval: 500ms) until:

▪ The element is found, or

▪ The timeout period specified in the implicit wait is reached.

3. Efficiency:

o If an element is found before the timeout, Selenium stops waiting and moves on.

o It does not waste time if the element is already available.

How Implicit Wait Really Works

1. If the Element is Present:

o Selenium will immediately proceed as soon as the element is located. It does not wait
unnecessarily for the entire timeout duration.

2. If the Element is Not Present:

o Selenium will continuously poll the DOM (default polling interval: 500ms) until:

▪ The element is found, or

▪ The timeout period specified in the implicit wait is reached.

3. Efficiency:

o If an element is found before the timeout, Selenium stops waiting and moves on.

o It does not waste time if the element is already available.

Example :

driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
Where to use :

• The Duration.ofSeconds(30) sets the timeout to 30 seconds.

• The WebDriver will wait for the page to load completely within this specified time. If the page
does not load fully in this time, it throws a TimeoutException.

Purpose:

• It is used when you're waiting for a page to finish loading, which includes loading resources like
images, scripts, and stylesheets.

• If the page load takes longer than the specified time, the TimeoutException is triggered.

Note:

• It does not wait for the element visibility or interactability; it waits only for the complete page
load, including all assets like images and scripts.

SetScriptTimeout

• SetScriptTimeout is used to set the maximum amount of time that the WebDriver will wait for
an asynchronous JavaScript script to finish execution. This timeout is particularly important
when dealing with operations like executing custom JavaScript or AJAX requests.

Example :

driver.manage().timeouts().setScriptTimeout(Duration.ofSeconds(30));

• Where:
o The Duration.ofSeconds(30) sets the script execution timeout to 30 seconds.
o WebDriver will wait for the completion of the JavaScript execution (or AJAX requests)
within this time. If the script does not finish in time, a TimeoutException will be
thrown.
• Purpose:
o It’s typically used when executing JavaScript via executeScript() in Selenium
WebDriver.
o If the JavaScript is taking longer than expected to execute (for example, in cases
where there are long-running AJAX requests or heavy scripts), the WebDriver can
throw an exception after the timeout is reached.
• Use Case:
o When you are running JavaScript to interact with the page or check something, like
retrieving the page’s title using JavaScript or executing AJAX-based actions. The
timeout will prevent waiting for an indefinite amount of time if the script hangs.
Explicit Wait in Selenium

Explicit Wait is a type of dynamic wait in Selenium that allows you to pause the execution of your
script until a certain condition is met. Unlike Implicit Wait, which applies to all element searches
globally, Explicit Wait is applied only to specific elements or conditions, providing more control over
waiting for particular conditions

Key Characteristics of Explicit Wait:

• Condition-based: You can specify a particular condition for the element to be in a desired
state (e.g., visible, clickable).

• Time-based: You can set a maximum wait time, and if the condition is met before the time
expires, the execution continues immediately.

• Applied to Specific Elements: It is used only for specific elements, not globally across all
element searches.

WebDriverWait

• WebDriverWait is an implementation of Explicit Wait in Selenium that allows you to wait for a
certain condition to occur within a specified amount of time.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));


WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someElementId")));

FluentWait

FluentWait is a more flexible version of WebDriverWait. It allows you to define the frequency with
which the condition is checked and to ignore specific exceptions while waiting. It is ideal when you
need more fine-grained control over the wait.

Key Features of FluentWait:

• Polling Interval: You can set the polling interval (i.e., how frequently to check for the
condition).
• Exception Handling: FluentWait allows you to ignore certain exceptions (such as
NoSuchElementException) while waiting for the condition to be met.

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)


.withTimeout(Duration.ofSeconds(10)) // Total timeout
.pollingEvery(Duration.ofSeconds(1)) // Polling interval
.ignoring(NoSuchElementException.class); // Exception to ignore

Explanation:
• withTimeout: Defines the maximum time to wait for the condition to be met (e.g., 10
seconds).
• pollingEvery: Defines the interval between each check (e.g., check every 1 second).
• ignoring: Specifies which exceptions should be ignored during the wait (e.g.,
NoSuchElementException).
Static Wait in Selenium: Thread.sleep()

Thread.sleep() is a static wait mechanism in Selenium, which causes the current thread to pause for a
specified period of time. This is a simple way to introduce a delay in your test execution, but it should
be used carefully due to its limitations.

How Thread.sleep() Works:


• Thread.sleep(timeInMillis): This method is a Java function that pauses the current executing
thread for the specified amount of time.
• The time is given in milliseconds, so for example:
o Thread.sleep(2000) will pause the execution for 2 seconds.

Key Characteristics of Thread.sleep():


1. Fixed Wait: Unlike dynamic waits (implicitlyWait(), explicitWait()), Thread.sleep() always
waits for the full duration specified, regardless of whether the condition (such as element
presence) is met before the wait time ends. This makes it static, as you can't dynamically
change the wait time based on the conditions.
2. No Condition Checking: Unlike WebDriverWait or FluentWait, Thread.sleep() does not check
any conditions (such as element visibility or readiness of the DOM). It simply pauses the
execution for a fixed time.
3. Execution Halting: It completely halts the execution of the thread for the given period. During
this time, the WebDriver does nothing (doesn't check for elements or handle actions).

You might also like