Waits in selenium

Waits in Selenium is one of the important pieces of code that executes a test case. It runs on certain commands called scripts that make a page load through it. Selenium Waits makes the pages less vigorous and reliable. It provides various types of wait options adequate and suitable under favorable conditions. This ensures you don’t mess up and get ended into failed scripts while performing automation testing with it.

Elaborately, Selenium Waits helps the user to troubleshoot various issues while page redirection across different web pages. It is achieved by refreshing the entire web page and reloading it with new elements. At times, there’s a call from Ajax as well. Thus, some time lag might exist while reloading pages and reflecting elements present on the web pages after refreshing.

Another instance to understand Selenium Waits is navigating web pages back and forth with the navigate() command. This navigate() method comes from WebDriver, whose main task is to simulate and manifest real-time scenarios like navigating between web pages concerning browsing history.

Why Do You Need Waits In Selenium?

Today, most of the modern application’s front-end is built on either Ajax or Javascript, followed by popular frameworks like Angular, React, or any other, which takes some time for loading elements on the web page. Hence, in such a case, Selenium throws an ‘ElementNotVisibleException’ message when you tend to locate an element present in your script which is still not loaded on the web page.

To clarify, you can look at the below code snippet where automation testing with Selenium is executed.

The given code will help you showcase the same problem as you execute automation testing with Selenium. An example of easemytrip.com is used, where the posting user selects the ‘From’ and ‘To’ destination with a date of journey. The web application takes a certain time to load the required flight details. In this case, without applying Wait, the user tends to book the first flight from the list. Since the page hasn’t loaded yet, the script failed to find the ‘book now button. It results in throwing a ‘NoSuchElementException’. It is given below:

  1. import org.open.selenium.By;  
  2. import org.openqa.selenium.JavascriptExecutor;  
  3. import org.openqa.selenium.Keys;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.chrome.ChromeDriver;  
  6. import   
  7. com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExec  
  8. utor;  
  9. public class NoWaitImplemented {  
  10.     public static void main(String[] args) throws InterruptedException {  
  11.         System.setProperty(“webdriver.chrome.driver”,   
  12. “.\\Driver\\chromedriver.exe”);          
  13.         WebDriver driver=new ChromeDriver();          
  14.         driver.manage().window().maximize();  
  15.         driver.get(“https://www.easemytrip.com/”);          
  16.         driver.findElement(By.id(“FromSector_show”)).sendKeys(“Delhi”,   
  17. Keys.ENTER);          
  18. driver.findElement(By.id(“Editbox13_show”)).sendKeys(“Mumbai”,   
  19. Keys.ENTER);  
  20.         driver.findElement(By.id(“ddate”)).click();  
  21.         driver.findElement(By.id(“snd_4_08/08/2019”)).click();  
  22.         driver.findElement(By.className(“src_btn”)).click();  
  23.         driver.findElement(By.xpath(“//button[text()=’Book Now’]”)).click();      
  24.     }  
  25. }  

The above code snippet depicts the same problem while executing automation testing with Selenium. In this code snippet, you can see an example of “easemytrip.com,” where the user will select ‘From’ and ‘To’ destination selection with a journey date. The web application takes a certain loading time to load the available flights based on the selected input fields provided by the user. In this case, the user might select and book only the first flight from the list. Since the page is still loading, the script has failed to find the ‘Book Now’ button. This directly throws a ‘NoSuchElementExpection’ return status with the following output shown below.

Selenium Waits

Leave a comment

Your email address will not be published. Required fields are marked *