We can have a modified version of the custom page load wait function in which we can run a while loop for a maximum of 10 times and see if page load status is compete we break out of the loop otherwise we wait for some seconds using a wait function and run it again till status is complete.
Then we can run the loop for jQuery and see if no jQuery is running in the background. If no jQuery is running the status will be 0.So we keep running the loop till status is 0 and at time we break out of the loop otherwise we wait using wait function and run again.
public void waitForPageToLoad(){
JavascriptExecutor js = (JavascriptExecutor)driver;
int i=0;
while(i!=10){
String state = (String)js.executeScript("return document.readyState;");
if(state.equals("complete"))
break;
else
wait(2);
i++;
}
i=0;
while(i!=10){
Long duration = (Long) js.executeScript("return jQuery.active;");
if(duration.longValue() == 0 )
break;
else
wait(2);
i++;
}
}
We define wait function as below
public void wait(int time){
try {
Thread.sleep(time*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}