Skip to content

Instantly share code, notes, and snippets.

@rlogiacco
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlogiacco/a1e1e7aa566ba3e9c108 to your computer and use it in GitHub Desktop.
Save rlogiacco/a1e1e7aa566ba3e9c108 to your computer and use it in GitHub Desktop.
Selenium wait for elements
public class WaitingWebDriver {
private SharedWebDriver browser;
private Class<? extends WebDriver> driver;
@Before
public void before() {
driver = ChromeDriver.class;
System.setProperty(SharedWebDriver.SELENIUM_DRIVER_PROPERTY, driver.getName());
}
@Test
public void findCombined() throws Throwable {
browser = SharedWebDriver.open(driver);
browser.get("http://run.plnkr.co/plunks/17M8WwGto2uieNqFXKje/");
assertEquals("0", browser.findElement(By.className("counter")).getText());
assertEquals("0", browser.findElement(By.name("counter")).getText());
assertEquals("0", browser.findElement(By.id("counter").name("counter")).getText());
}
@Test
public void findSync() throws Throwable {
browser = SharedWebDriver.open(driver);
browser.get("http://run.plnkr.co/plunks/17M8WwGto2uieNqFXKje/");
assertEquals("0", browser.findElement(By.id("counter")).getText());
browser.findElement(By.id("sync")).click();
assertEquals("1", browser.findElement(By.id("counter")).getText());
}
@Test
public void findAsyncAdded() throws Throwable {
browser = SharedWebDriver.open(driver);
browser.get("http://run.plnkr.co/plunks/17M8WwGto2uieNqFXKje/");
assertEquals("0", browser.findElement(By.id("counter")).getText());
browser.findElement(By.id("async")).click();
assertEquals("0", browser.findElement(By.id("counter")).getText());
browser.findElement(By.id("wait-if"));
assertEquals("0", browser.findElement(By.id("counter")).getText());
new FluentWait<WebDriver>(browser)
.withTimeout(10, TimeUnit.SECONDS)
.until(new Predicate<WebDriver>() {
public boolean apply(WebDriver browser) {
try {
browser.findElement(By.id("wait-if"));
return false;
} catch (NoSuchElementException nsee) {
return true;
}
}
});
assertEquals("1", browser.findElement(By.id("counter")).getText());
}
@Test
public void findAsyncHidden() throws Throwable {
browser = SharedWebDriver.open(driver);
browser.get(SharedWebDriverTest.TEST_PAGE);
assertEquals("0", browser.findElement(By.id("counter")).getText());
browser.findElement(By.id("async")).click();
assertEquals("0", browser.findElement(By.id("counter")).getText());
browser.findElement(By.id("wait-show"));
assertEquals("0", browser.findElement(By.id("counter")).getText());
new FluentWait<WebDriver>(browser)
//.withTimeout(10, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class)
.until(new Predicate<WebDriver>() {
public boolean apply(WebDriver browser) {
return !browser.findElement(By.id("wait-show")).isDisplayed();
}
});
assertEquals("1", browser.findElement(By.id("counter")).getText());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment