Tuesday, May 29, 2018

How to write custome wait method in selenium webdriver using Java

package com.guru99.utils;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import com.guru99.testBase.TestBase;

public class WaitUtil extends TestBase
{

public static void waitForPageToLoad(long timeOutInSeconds,WebDriver driver) {

ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {

public Boolean apply(WebDriver driver) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
};
try {
// System.out.println("Waiting for page to load...");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(expectation);

} catch (Throwable error) {
System.out.println(
"Timeout waiting for Page Load Request to complete after " + timeOutInSeconds + " seconds");
Assert.assertFalse(true, "Timeout waiting for Page Load Request to complete.");

}
}

}

No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...