Test Diary

Archives


Social Profile


Recent Comments


My Second Selenium WebDriver Test (Implicit and Explicit waits)

Olufemi Ade-OlusileOlufemi Ade-Olusile

Menu Next Topic

In this post, we will be looking at the most used “wait” conditions in Selenium. Waits can be used to help users troubleshoot issues that might be encountered while reloading, redirecting or refreshing a webpage. According to Selenium (2015), Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step.  Also, waits can help a user solve issues with Ajax calls and complex web front-end.

 Hold on, lets cut to the chase: Testing Ajax calls can be a pain.

Lots of users have come up with different ways to solve Ajax issues. I have come across users in the past  who use pre assertions as a wait condition before assertions. In my opinion this are really verbose and most times this assertions get dropped when refactoring. “As a beginner, you may not understand this paragraph, but don’t worry lets move ahead and you will grab this in time”. 

Anyway, Selenium provides us with “Waits” that can be used or refactored by the user to solve some of this issues we have mentioned in the above. I’ll be discussing the two most common waits used.

Implicit Wait

This are used to supply a waiting time between your test steps. Implicit waits, tell the Webdriver to wait a certain amount of time when trying to find an element, or reloading, redirecting and refreshing a webpage.

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

 You can specify the amount of time you want WebDriver to wait, by using this syntax. 

Explicit Wait

This wait is used to to halt your test step, or wait for a particular condition to occur before it proceeds to the next test step. Also you can stipulate a maximum wait time just in case the condition does not occur.

WebElement wait = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("xxxxxxxx")));

This explicit wait will wait for 10 seconds until WebDriver can locate the presence of element “xxxxxx” by ID.

In our first test (previous post), we used a “thread.sleep()” which is a Java wait method. We will remove the the thread.sleep() and use an explicit and implicit wait in our code. “Please note that it is not good practice to use both waits in a test, but we will use this for demonstration purpose.”

Second Test (Implicit and Explicit wait) 
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Testdiary_ExplicitAndImplicitWait {
public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";

driver.get(contactUrl);

// *Tells WebDriver to wait for 10 seconds if targeted element is not found
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement name = driver.findElement(By.name("your-name"));

name.clear();

name.sendKeys("Femi");

WebElement email = driver.findElement(By.name("your-email"));

email.clear();

email.sendKeys("xxxx@testdiary.com");

WebElement subject = driver.findElement(By.name("your-subject"));
subject.clear();
subject.sendKeys("New tester");

WebElement your_message = driver.findElement(By.name("your-message"));
your_message.clear();
your_message.sendKeys("I want to be the best tester.");

WebElement send = driver
.findElement(By.xpath("//input[@value='Send']"));

send.click();

String expected_message = "Your message was sent successfully. Thanks.";

// wait 10 seconds until text is present in the element located
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.textToBePresentInElementLocated(
By.xpath("//div[@id='wpcf7-f551-p683-o1']/form/div[2]"),
expected_message));

String actual_message = driver.findElement(
By.xpath("//div[@id='wpcf7-f551-p683-o1']/form/div[2]"))
.getText();

if (expected_message.equals(actual_message)) {

System.out.println("verification successful - " + actual_message);
} else {
System.out.println("verification unsuccessful");
}

driver.close();

System.exit(0);
}
}

In this test above explicit wait waits for 10 seconds until text is present in the element located. If the text is not present and 10 seconds elapses, Webdriver will continue with the next test step despite being unable to find the text present. Also, we introduced an implicit wait which tells WebDriver to wait for 10 seconds before it locates the name text box on the webpage. Why do we tell WebDriver to wait? Because a webpage can delay in loading, which causes WebDriver to initiate the next step of your test script without waiting for the page to be loaded. In addition, your test will fail as WebDriver will not be able to locate the element.

Furthermore, we have different types of explicit waits and i will discuss a few of them in the below.

  1.  Wait until text is present 
    (new WebDriverWait(driver, 10)).until(ExpectedConditions.textToBePresentInElement(By.id("id"), "text");
    
  2. Wait until title of a webpage is equal to the title you are expecting 
    (new WebDriverWait(driver, 10)).until(ExpectedConditions.titleIs("title"));

In conclusion, we have looked at two different waits and how they can be used effectively in our test scripts. Selenium also provides us with the fluent wait, but that will not be covered in this post.

You can download or clone this test from the Testdiary Github.

Click: Explicit and Implicit waits

Happy Testing!!!

Menu Next Topic

“My name is Olufemi Ade-Olusile, and I am a Software Developer in Test. I am the owner of "Test diary" which is aimed at inspiring and teaching software testers.

  • Tanushri Mukherjee

    Hi, The message “Your message was sent successfully. Thanks.” doesnt gets displayed on clicking send.

    • Sorry for the delay in replying, I just tested this code and it works. The message displays. You can put a break point on that area of the point and check your browser and verify this.