Explicit wait

To understand explicit waits, we have to explore the WebDriverWait class. This class extends the FluentWait class. The FluentWait class implements the wait interface.

The WebDriverWait constructor has three overloaded versions. We will be using the version with two arguments, which is the most common version. The signature of the WebDriverWait constructor with two arguments is public WebDriverWait(WebDriver,long). The way to instantiate a WebDriverWait object is by phrasing the code as follows:

WebDriverWait wdWait = new WebDriverWait(driver,15);

Here, we specify the maximum waiting time as 15 seconds. To understand the explicit wait, we should first familiarize ourselves with a class called ExpectedConditions. This class has a number of useful methods for waiting on a WebElement to appear before throwing an ElementNotFoundException. A few of these very useful methods are as follows:

  • presenceOfElementLocated: Checks whether the element is present on the DOM of a page.
  • visibilityOfElementLocatedChecks whether the element is present on the DOM of a page and is also visible.
  • frameToBeAvailableAndSwitchToIt: Checks whether a given frame is available. If it is, it then switches to the frame.
  • elementToBeClickable: Checks whether a given element is visible and enabled.

The syntax of using these methods is as follows:

wdWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Silk Test']")));

Let's look at this line of code in a little more detail. First, let's split it. The until method is present in the FluentWait class. Since the WebDriverWait class extends the FluentWait class, we can invoke the until method on the WebDriverWait object. The until method keeps polling for the element passed as a parameter until the timeout value specified at the time of creating the WebDriverWait object. The default polling time is 500 milliseconds. Selenium will check for the presence of the element every 500 milliseconds before throwing a NoSuchElement exception.

Let's have a look at a small program that uses an explicit wait to find particular text when it is visible. We will use the same JavaScript file that was created earlier:

long startTime = 0L;
long endTime = 0L;
boolean status = false;
System.setProperty("webdriver.chrome.driver", "C:\SeleniumWD\src\main\resources\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wdWait = new WebDriverWait(driver, 12);
driver.manage().window().maximize();
driver.navigate().to( "C:\Users\Bhagyashree\Desktop\Documents\Timer.html");
wdWait.until( ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Timer Start']"))).click();
startTime = System.currentTimeMillis();
try {
wdWait.until(ExpectedConditions.presenceOfElementLocated(By
.xpath("//*[text()='Silk Test']")));
status = driver.findElement(By.xpath("//*[text()='Silk Test']"))
.isDisplayed();
} catch (NoSuchElementException exception) {
System.out.println(exception.getMessage());
}
if (status) {
System.out.println("Element is displayed");
} else {
System.out.println("Element is not displayed");
}
endTime = System.currentTimeMillis();
timeDiff = endTime - startTime;
timeDiff = timeDiff / 1000;
System.out.println("Start Time: " + startTime);
System.out.println("End Time: " + endTime);
System.out.println("Difference in Time: " + timeDiff);

The output from the preceding program is as follows:
Element is displayed
Start Time: 1535263491998
End Time: 1535263504312
Difference in Time: 12
..................Content has been hidden....................

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