Selenium: Waits

In last post, we have seen how Xpath helps us to find dynamic elements which changes every time. Similarly, most of the front ends are developed with Ajax and JavaScript where different elements load at different time. In such cases, our driver need to wait till the element is loaded and visible otherwise it will throw 'elementNotVisibleException' exception and eventually our automation script will fail. To avoid such situations we have one powerful option in selenium that is nothing but 'Wait'.


[ source: www.deshannonspeaks.com ]


In general, we have two types of wait in selenium.
  1. Implicit wait
  2. Explicit wait
1.Implicit Wait:  This idea is borrowed from Watir. Here, we are instructing our driver to wait for certain amount of time for element to get loaded before throwing an exception.

Example: Consider our Facebook login scenario. Implicit wait syntax is highlighted in bold. It will wait for 10 seconds and if any element is not loaded in that time frame, it will throw an exception. We can specify ant time unit like seconds, minuets, hours etc.


package blog;


import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

@SuppressWarnings("unused")
public class FacebookLogin {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe");

driver=new ChromeDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("https://www.facebook.com");

Thread.sleep(1000);

driver.manage().window().maximize();
WebElement username=driver.findElement(By.id("email"));

WebElement password=driver.findElement(By.id("pass"));
username.sendKeys("*******");

password.sendKeys("********");
WebElement login=driver.findElement(By.id("loginbutton"));
login.click();
}
}


2.Explicit Wait: Explicit wait tells driver to wait till the certain conditions are met and if false, it will throw an exception. Explicit wait is more effective wait than implicit wait as we can explicitly specify the conditions for waiting.

Until now, we were using thread.sleep() for wait, it is also one type of explicit wait but we will discuss its disadvantage after the explicit wait example.

Example: Consider the same Facebook login scenario. Explicit wait syntax is highlighted in bold. Here, we first need to create one WebDriverWait object.

package blog;


import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

@SuppressWarnings("unused")
public class FacebookLogin {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
driver=new ChromeDriver();
WebDriverWait wait=new WebDriverWait(driver, 10);
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe");
driver.get("https://www.facebook.com");
driver.manage().window().maximize();
WebElement username=driver.findElement(By.id("email"));
WebElement password=driver.findElement(By.id("pass"));
username.sendKeys("*****");
password.sendKeys("******");
WebElement        login=wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("loginbutton")));
login.click();
}
}


We have specified wait limit of 10 seconds, if element is found within 10 seconds then driver will continue next execution. It will not wait for full 10 seconds and that will save our execution time. Whereas in thread.sleep(), even if element is found, it will wait for the specified time. Hence, thread.sleep() is not recommended to use.


Just like 'visibilityOfElementLocated', there are many such expected conditions for which our driver will wait. They are: 

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()

Cheers.. !





Comments

  1. Hi Sir,
    I need to send some key on my application which is running on locked screen. I can not use robot, actions class.

    Can you please help me on this.

    ReplyDelete
    Replies
    1. Can you please specify which keys ?? You want to send data or click some element ??

      Delete
    2. We need to send 10 TAB and ENTER. After this we are able to find webdriver.

      Delete
  2. I need to send many around 10 than we need to press 'Enter' key. Post that we are able to find webdriver.

    ReplyDelete
  3. I need to send many around 10 than we need to press 'Enter' key. Post that we are able to find webdriver.

    ReplyDelete
    Replies
    1. Hi, you can use .sendkeys(“\n”) in for loop to press enter 10 times. Thanks

      Delete
    2. Hi Sir,
      For send key we need to have driver available. But in my case driver is not available.

      Could you pls guide me if any other way is there to utilise SENDKEY method.

      Delete

Post a Comment

More posts on selenium...

Selenium/Java-OOPs-Encapsulation

Selenium/Java-OOPs-Abstraction

Selenium: Automate Web Application Login

Selenium/Java- CoC

Selenium/Java-OOPs-Inheritance

Selenium: All about XPath Locator in WebDriver

Selenium: Capturing the screenshot

Selenium: BDD- The Disruptive Framework

Selenium: Frameworks