Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created August 24, 2010 21:57
Show Gist options
  • Save robfletcher/548404 to your computer and use it in GitHub Desktop.
Save robfletcher/548404 to your computer and use it in GitHub Desktop.
import groovy.transform.*
import org.junit.*
import org.openqa.selenium.*
import org.openqa.selenium.support.ui.*
import org.openqa.selenium.firefox.*
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
class AutocompleteTests {
private WebDriver driver
private ClosureWait wait
@Before void startWebDriver() {
driver = new FirefoxDriver()
wait = new ClosureWait(driver, 5)
}
@After void stopWebDriver() {
driver.close()
}
@Test void testUserCanSelectAutocompleteOption() {
driver.get("http://localhost:8080/autocomplete/")
assertThat typeInAutocomplete(By.id("tags"), "G"), hasItems("Erlang", "Groovy")
selectAutocompleteOption "Groovy"
assertThat driver.findElement(By.id("tags")).value, equalTo("Groovy")
}
List<String> typeInAutocomplete(By locator, String keystrokes) {
driver.findElement(locator).sendKeys(keystrokes)
wait.until {
driver.findElement(By.cssSelector("ul.ui-autocomplete")).isDisplayed()
}
driver.findElements(By.cssSelector("ul.ui-autocomplete li")).text
}
void selectAutocompleteOption(String optionText) {
def option = driver.findElement(By.cssSelector("ul.ui-autocomplete")).findElement(By.linkText(optionText))
driver.executeScript "\$(arguments[0]).trigger('mouseover')", option
option.click()
}
}
@InheritConstructors
class ClosureWait extends WebDriverWait {
def until(Closure condition) {
until(new ClosureCondition(condition))
}
}
class ClosureCondition implements ExpectedCondition {
private final Closure closure
ClosureCondition(Closure closure) {
this.closure = closure
}
def apply(WebDriver driver) {
closure(driver)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment