Explicit wait

There are times when the app under test can be slow on certain specific elements, such as page submit, form submit, or somewhere it fetches data from an external system and takes a little more time to load. In that case, using implicit wait to handle the situation will be a flawed approach, given that it has to wait for each and every element for the same specified time.

To handle this situation, we can use explicit wait for such elements. In explicit wait, we tell the web driver instance to wait for a certain condition invoked through ExpectedConditions. So, this wait applies explicitly to the specified element. Explicit wait can be invoked using this code:

WebDriverWait wait = new WebDriverWait(appiumDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("text1")));

In the preceding code, we are creating an instance of WebDriverWait with a maximum waiting time of 10 seconds and then using an ExpectedConditions, which tells the driver to wait till the visibility of the specified element can be located. ExpectedConditions has a bunch of methods available to be used under different conditions. WebDriverWait, by default, calls ExpectedConditions every 500 ms until it returns successfully, otherwise it throws the TimeoutException, as follows:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.id: text1 (tried for 10 second(s) with 500 MILLISECONDS interval)

While automating, you will typically need the given conditions to be met for an element and, for each of the following, ExpectedConditions provides a set of predefined conditions:

  • Web element is present and clickable
  • Web element is selected
  • Web element is invisible
  • Selected web element
  • Presence of web element located by
  • Wait for a particular condition
  • Text present in a web element

Here's a list of all the methods available under ExpectedConditions:

Explicit wait is also used to check a specified property of an element, such as visibility, click-ability, invisibility, and selection state.

Let's go ahead and refactor some of the code to introduce explicit wait. Some of the places to add explicit wait, would be where we are typing in a textbox to search for an item. Consider the following examples:

  • Choosing my city:
      WebDriverWait wait = new WebDriverWait(appiumDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("city_name")));
appiumDriver.findElement(By.id("city_name")).click();
  • Searching for a specified car:
      WebDriverWait wait = new WebDriverWait(appiumDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("text1")));
List<WebElement> results =
appiumDriver.findElements(By.id("text1"));

So there are two different methods we are adding explicit wait to; we can even set a different timeout for each element. However, one observation would be that the code is repeating. We will come back to refactor this piece in subsequent chapters to create something called a base page class, which hosts all such commonly used methods that can be used in each page class.

Make the preceding changes in your code and run the test; the test should run smoothly, as earlier. Here's an implementation in one of the methods:

@And("^I choose "([^"]*)" as my city$")
public void iChooseAsMyCity(String city) throws Throwable {
WebDriverWait wait = new WebDriverWait(appiumDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("skip")));
appiumDriver.findElement(By.id("skip")).click();

try {
if (appiumDriver.findElement(By.xpath
("//android.widget.Button[@text='Later']")).isDisplayed())
appiumDriver.findElement(By.xpath
("//android.widget.Button[@text='Later']")).click();
} catch (Exception e) {
//Do nothing
}

appiumDriver.findElement(By.id("citySpinner")).click();
appiumDriver.findElement(By.id("search_ET")).click();
appiumDriver.findElement(By.id("search_ET")).sendKeys(city);

wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("city_name")));
appiumDriver.findElement(By.id("city_name")).click();
}

Let's move on to understand a wait type that is somewhat more specific and lets us further customize it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.36.38