Implicit wait

The implicit wait is the first type of conditional synchronization that is always at the WebDriver-instance level. As long as the WebDriver instance is active, this wait is effective. 

The way to use the implicit wait is by phrasing it as driver.manage().timeouts().implicitlyWait(14, TimeUnit.SECONDS);.

The manage() method, when invoked on the driver object, returns an object of the options interface, after which we invoke the timeouts() method in the options interface. The timeouts() method returns an object of the timeouts interface. Finally, we invoke the implicitelyWait(long, TimeUnit) method of the timeouts interface. The implicitelyWait method is a factory method that returns an object of the same interface, namely timeouts.

When searching for a single element, the driver should poll the page until it finds the element under consideration. If the element is not found, or this timeout expires, a NoSuchElementException is thrown. When multiple elements are being searched in the DOM, the driver polls the page until at least one element has been found, or this timeout expires.

Increasing the implicit wait timeout is a method that should be used with caution, as it will have a negative effect on the test runtime, particularly when used with slower location techniques, such as XPath, when compared with CSS.

We will create a sample page in JavaScript, which will have a button. When the button is clicked, various tool names will get displayed at a time interval of 3 seconds. The following is the code for the web page: 

<html>
<body>
<button onclick="timerText()">Timer Start</button>
<p id="demoText">Click on Timer Start</p>
<script>
function timerText() {
setTimeout(displaySelenium,3000);
setTimeout(displayUFT,6000);
setTimeout(displayAppium,9000);
setTimeout(displaySilkTest,12000);
setTimeout(displayCucumber,15000);
}
function displaySelenium() {
document.getElementById("demoText").innerHTML="Selenium
WebDriver";
}
function displayUFT() {
document.getElementById("demoText").innerHTML="Unified
Functional Test";
}
function displayAppium() {
document.getElementById("demoText").innerHTML="Appium";
}
function displaySilkTest() {
document.getElementById("demoText").innerHTML="Silk Test";
}
function displayCucumber() {
document.getElementById("demoText").innerHTML="Cucumber
Framework";
}
</script>
</body>
</html>

Let's write a small program that prints Element is displayed when it finds an element with the text Silk Test on screen. Note that, as per the preceding JavaScript program, Silk Test appears after 12 seconds. 

The following is the code to print details such as whether an Element is displayed and the exact time required to find the element. The Start Time and End Time are noted, and finally, a difference in time is measured:

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();
driver.manage().window().maximize();
driver.navigate().to( "C:\Users\Bhagyashree\Desktop\Documents\Timer.html");
driver.findElement(By.xpath("//*[text()='Timer Start']")).click();
startTime = System.currentTimeMillis();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
try {
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 code is given here: 

Element is displayed
Start Time: 1535203696762
End Time: 1535203708848
Difference in Time: 12

The page load timeout is a timeout that remains in action for the lifetime of the driver. It sets the amount of time for a page load to complete fully before throwing a PageLoadTimeout exception. Let's take the example of http://www.freecrm.com. The home page takes some time to fully load before it can display the login form. As you can see, we get the exact time for Silk Test to appear on screen, which is 12 seconds. Note that it is not feasible for us to use Thread.sleep in this kind of situation.

The following script displays the use of PageLoadTimeout, where we have deliberately set the PageLoadTimeout as 3 seconds:

 System.setProperty("webdriver.chrome.driver",
"C:\SeleniumWD\src\main\resources\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("http://www.freecrm.com");

This script will throw a PageLoadTimeout exception, as shown in the following output:

Another timeout that can be applied at the driver-instance level is setScriptTimeout. This timeout requires an understanding of the JavaScriptExecutor, which is covered in a later chapter; setScriptTimeout will be discussed then.

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

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