Synchronization in Selenium
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.
• 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 :
// Locate an element
WebElement element = driver.findElement(By.id("example"));
o Selenium will immediately proceed as soon as the element is located. It does not wait
unnecessarily for the entire timeout duration.
o Selenium will continuously poll the DOM (default polling interval: 500ms) until:
3. Efficiency:
o If an element is found before the timeout, Selenium stops waiting and moves on.
o Selenium will immediately proceed as soon as the element is located. It does not wait
unnecessarily for the entire timeout duration.
o Selenium will continuously poll the DOM (default polling interval: 500ms) until:
3. Efficiency:
o If an element is found before the timeout, Selenium stops waiting and moves on.
Example :
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
Where to use :
• 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
• 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.
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.
• 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.
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.