我們也在使用Selenium & java自動化Angularjs應用程序。我們已經寫我們自己的同步,並等待加載函數如下:
public void Sync()
{
//waitForLoading();
try
{
JavascriptExecutor js = (JavascriptExecutor)driver;
WebDriverWait wait = new WebDriverWait(webDriver, 300);//timeoutInSeconds
wait.Until(js.executeScript("return document.readyState").toString().equals("complete"));
Thread.sleep(4000);
}
catch
{
Thread.sleep(16000);
}
}
//Another version of the same function based on time based polling to make it more dynamic
public void checkPageIsReady() {
JavascriptExecutor js = (JavascriptExecutor)driver;
//Initially bellow given if condition will check ready state of page.
if (js.executeScript("return document.readyState").toString().equals("complete")){
System.out.println("Page Is loaded.");
return;
}
//This loop will rotate for 25 times to check If page Is ready after every 1 second.
//You can replace your value with 25 If you wants to Increase or decrease wait time.
for (int i=0; i<25; i++){
try {
Thread.sleep(1000);
}catch (InterruptedException e) {}
//To check page ready state.
if (js.executeScript("return document.readyState").toString().equals("complete")){
break;
}
}
}
看一看 - http://stackoverflow.com/questions/25062969/testing-angularjs-with-selenium的http://計算器.com/questions/38621451/selenium-and-angularjs-wait-until-do-do-some-actions – Grasshopper