This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private ExpectedCondition WaitForAjax(final long timeout) { | |
return new ExpectedCondition() { | |
public Boolean apply(WebDriver driver) { | |
final long startTime = System.currentTimeMillis(); | |
final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; | |
while ((startTime + timeout) >= System.currentTimeMillis()) { | |
final Boolean scriptResult = (Boolean) javascriptExecutor.executeScript("return jQuery.active == 0"); | |
if (scriptResult) | |
return true; | |
delay(100); | |
} | |
return false; | |
} | |
}; | |
} | |
private void delay(final long amount) { | |
try { | |
Thread.sleep(amount); | |
} | |
catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} |
here is a sample to use this method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SeleniumTest { | |
private final static String BASE_URL = "http://localhost:8080"; | |
private final static WebDriver driver = new FirefoxDriver(); | |
@Before | |
public void startUp() { | |
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); | |
driver.manage().window().setPosition(new Point(0, 0)); | |
driver.manage().window().setSize(new Dimension(800, 600)); | |
} | |
@Test | |
public void testMyPage() { | |
final Wait wait = new WebDriverWait(driver, 60); | |
driver.get(BASE_URL + "/myweb/"); | |
boolean isListPopulated = wait.until(WaitForAjax(60000)); | |
Assert.assertTrue(isListPopulated); | |
} | |
@After | |
public void tearDown() { | |
// Close the browser | |
driver.quit(); | |
} | |
} |