Hello everyone, Starting with the Automation testing where we write a script in order to test web applications. But have a question in mind, why we need automation? The answer is simple we have to deliver a software which is good in quality and also bug free. So, to minimize the human errors, save time and covers most of the test cases we use automation testing. Automation ensures the efficiency and the effectiveness of testing.
Selenium is a tool where we write our test scripts and execute at any time. Ultimately, it reduces cost of application and improves quality by reducing human errors and better detection of defects. Let us understand the background, here with the help of WebDriver driver our test scripts can get executed on various browsers.
As we are dealing with browser, most of the time synchronization issue occurs between the browser and the driver. These synchronization issues occur due to the dynamic nature of the web page where elements appear on the page at different time interval, sometimes alert is on the page or some advertisement, some element will appear after some action on element or some delay while loading.
Let's have some scenarios to understand the flow, If you have a web page with an elements like text boxes, buttons, links. let's say we have to click on a single button.
Scenario 1: If browser is ready it means all elements are loaded on the page and the driver also ready to perform action on the button then, The script will not have any synchronization issue and test will get executed successfully.
Scenario 2:If browser is not ready it means not all element loaded successfully but, The driver is ready to perform action on element (button) which is not present on the page then our script immediately throws an error element not found.
We write selenium scripts but we don't want any kind of exceptions/errors which will increases the testing time of the script. To avoid that, there are selenium waits and Thread.sleep().
In this article, we are going to brief about
What are Selenium waits?
Why can't we use the Thread.sleep()?
Thread.sleep(long millis) is method provided by java which suspend the thread for specific time that is provided by user. If our method says Thread.sleep(1000); then our program must suspend that thread for 1000 milliseconds. but if our script took 900 milliseconds to find that element reference whereas 100 milliseconds is the extra time where our thread is sleep. Similarly if our program added with several Thread.sleep(1000); Then it will affect execution time which is definitely more than it supposed to be.
Syntax
Thread.sleep(1000);
Selenium waits are categorized in to
Implicit Wait
Explicit Wait
Fluent Wait
Implicit Wait: In Implicit Wait, we try to tell our script through selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page.
Implicit waits will be in place for the entire time the browser is open. This amount of time is applicable for every element that is present in DOM. The default timeout is 0 seconds, If not provided it will immediately throws an error. But if we provide time unit driver will wait for that duration before throwing an error.
Implicit wait applies globally to all elements on the page. Even if we set larger implicit waits as soon as driver gets an element reference it will return and execute rest of the script. Unlike Thread.sleep(), implicit wait do not increase the execution time.
Syntax
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
Representation of how implicit wait works
Explicit Wait: An explicit wait is a more flexible wait that allows us to wait for a specific condition to be met before continuing test execution. The default polling frequency for explicit wait in Selenium is 500 milliseconds.
An exception is thrown if the condition is not met within the specified time. We can use this Wait instance and call until() with the ExpectedConditions. Explicit wait applies locally to a specific element with conditions. Here are some conditions listed such as
visibilityOfElementLocated
presenceOfAllElementsLocatedBy
textToBePresentInElement
elementToBeClickable
elementToBeSelected
elementSelectionStateToBe
Syntax
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(2)); wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_BUTTON));
driver.findElement(LOCATOR_BUTTON).click();
Note: Provide the element locator in place of LOCATOR_BUTTON.
Fluent Wait: A fluent wait is another type of explicit wait that allows more control over the waiting mechanism. It enables us to define the expected condition and the polling mechanism to check if particular condition to be met.
Unlike Explicit wait, in fluent wait we can mention the polling frequency along with ignore specified exception. Polling frequency is checking on element for particular condition to be true, if not it will go n check again with the mentioned time interval. You can change the polling frequency for fluent wait based on your needs. Fluent wait applies locally to a specific element with conditions and polling frequency.
Syntax
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(2))
.pollingEvery(Duration.ofMillis(300))
.ignoring(ElementNotInteractableException.class);
Below is an example of fluent wait.
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(5))
.pollingEvery(Duration.ofMillis(100));
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
if(alert!=null)
{
String alertmsg=alert.getText();
System.out.println("Alert Message is ---> "+alertmsg);
alert.accept();
}
else
{
System.out.println("No alert...");
}
Recommendation :
Always use waits
Use explicit wait instead of implicit wait when necessary
Use fluent waits when necessary.
Use reasonable wait times
Use ExpectedConditions class
Avoid using Thread.sleep for waits
Conclusion : Selenium waits are an excellent way to use in your script. Always use the explicit wait/fluent wait whenever required depending on your script. Always provide significant wait time to avoid execution errors in script.
Reference Links:
Thank you... For Reading my blog. Learning is fun!