Automation Testing is commonly comprising of following two segments:
Application Under Test
Test Automation Tool
We must write our test scripts in such a way that these two segments move with same desired pace, because both have their own agility. Hence, to avoid Errors which will exhaust time in debugging.
Synchronization : When two or more components involved to perform any action, we expect these components to work together with the same pace. The co-ordination between these components to run parallelly is called Synchronization.
To synchronize between script execution and application, we need to wait after performing appropriate actions.
Synchronization can be classified into two categories:
Unconditional Synchronization(Static Wait)
Conditional Synchronization(Dynamic Wait)
Unconditional Synchronization(Static Wait)
Unconditional Synchronization indicates timeout value alone and makes tool to continue further only when specified time is elapsed. In Selenium with Java, we use Thread.sleep() method to achieve static wait.
Syntax-:
This statement is helpful only when we are interacting with third party systems like interfaces because it is not possible to write conditions to check the status. Now, we want tool to wait for certain amount of time and then perform the next actions sequence.
This statement makes the tool to wait for certain time even though the application is ready, thus increasing the chance of unnecessary wait and hence giving the scope to Conditional Synchronization.
Conditional Synchronization(Dynamic Wait)
By using conditional synchronization we can specify timeout duration along with some desired conditions to check and then throw an error if nothing happens.
Types of Conditional Synchronization:
PageLoadTimeout
Implicit Wait
Explicit Wait
Fluent Wait
PageLoadTimeout
This is used for Web browser page load synchronization. If page load takes more time than WebDriver speed, then TimeOutException is thrown by the Webdriver. To overcome this situation we use PageLoadTimeout.
Syntax-:
In above example if pageload is completed in 5 seconds, driver will not wait for another fifteen seconds. This is the advantage of Dynamic Wait.
Implicit Wait
Implicit wait is used in cases where the WebDriver cannot locate an object immediately because of its unavailability.
Implicit Wait tells the WebDriver to Wait until the stated time before throwing the NoSuchElement/ElementNotVisible exception. Waiting time across the test script between each consecutive steps are taken by default. Hence, next testStep will execute only when the specified time is elapsed post executing the previous testStep.
Once the specified time limit is crossed, the webDriver will try to search the element once again for one last time. Upon success, it proceeds with the execution; upon failure, it throws exception.
It is a kind of global wait which means the wait is applicable for the entire driver. Hence, hardcoding this wait for longer time periods will hamper the execution time.
Syntax-:
The Implicit wait takes one argument: WaitTime
FromMilliSeconds
FromSeconds
FromMinutes
FromHours
FromDays
Explicit Wait (WebDriver Wait)
Explicit waits are very good to use when page loads dynamically. Explicit Wait tells the WebDriver to Wait until the specified condition is met or maximum time elapses before throwing NoSuchElement (or) ElementNotVisible Exceptions. Explicit waits are applied for the specified test step in test script.
To use Explicit waits, we must first create instance for “WebDriverWait” class.
Syntax-:
Following are the ExpectedConditions:
AlertIsPresent()
ElementSelectionStateToBe()
VisibilityOfAllElementsLocatedBy()
ElementToBeSelected()
FrameToBeAvaliableAndSwitchToIt()
InvisibilityOfTheElementLocated()
InvisibilityOfElementWithText()
ElementToBeClickable()
PresenceOfAllElementsLocatedBy()
PresenceOfElementLocated()
TextToBePresentInElement()
TextToBePresentInElementLocated()
TextToBePresentInElementValue()
TitleIs()
TitleContains()
VisibilityOf()
VisibilityOfAllElements()
VisibilityOfElementLocated()
Fluent Wait
A FluentWait instance defines the maximum amount of time to wait for a condition to take place, as well as the frequency with which to check the existence of the object condition.
Syntax-:
In this syntax,the driver will wait 30seconds for an element to be available on the page, but we will check its avilable once in every 5seconds.
In Conclusion , the static wait is not generally recommended as it can slow our execution. Among dynamic waits, the Implicit wait is simple when compared to Explicit wait and can be declared in setup method of the test script in single line of code. However, the Implicit wait has drawbacks despite being easy and simple to apply. Hence rising scope to use Explicit Waits because these waits can be applied for a specified test step in test script.
I hope this will be helpful for you while scripting.. Happy Testing !!!