Created
August 24, 2010 21:57
-
-
Save robfletcher/548404 to your computer and use it in GitHub Desktop.
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
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