Skip to content

Instantly share code, notes, and snippets.

@virtix
Created March 23, 2010 14:37
Show Gist options
  • Save virtix/341240 to your computer and use it in GitHub Desktop.
Save virtix/341240 to your computer and use it in GitHub Desktop.
<p id="find_me">found</p>
<a href="javascript:void(0);" onclick="go();" id="go_link">go</a>
<script>
function go(){
window.setTimeout( function(){
location.href = 'http://dev/sandbox/ajax.html';
} , 2000);
}
</script>
/**
* Example of using WebDriverWait, to wait for an element to be available to the
* driver before timing out.
* */
package tests;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.TimeoutException;
import com.google.common.base.Function;
public class WindowLocationTest {
InternetExplorerDriver driver = null;
By find_me = null;
By go_link = null;
WebElement findMe = null;
@Before
public void start(){
driver = new InternetExplorerDriver();
driver.get("http://dev/sandbox/location.html");
go_link = By.id("go_link");
find_me = By.id("find_me");
driver.findElement(go_link).click();
}
@After
public void stop(){
driver.close();
}
@Test(expected=TimeoutException.class)
public void testLocationHrefWithTimeout() {
findMe = waitFor(find_me, 1);
Assert.fail("shouldn't get here");
}
@Test
public void testLocationHrefNoTimewout() {
findMe = waitFor(find_me, 5);
Assert.assertEquals("found", findMe.getText());
}
public WebElement waitFor(By condition, long timeout) throws TimeoutException {
Function<WebDriver, WebElement> fun = new ElementWaiter(condition);
return (WebElement) new WebDriverWait(driver, timeout).until(fun);
}
public final class ElementWaiter implements Function<WebDriver, WebElement> {
By condition;
public ElementWaiter(By condition){
this.condition = condition;
}
public WebElement apply(WebDriver driver) {
return driver.findElement(this.condition);
}
}
} //end test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment