I have a test case that requires typing in a partial value into an ajax based textfield and verifying the list has the expected content. If it does, select the content. An
I recently wrote a HOWTO on this very topic - using Selenium to test an AJAX-driven JQuery autocomplete menu:
I was able to solve this by using the below function: The below function takes the text you want to select as a parameter. Ex: If you want to select "javascript", just type "java" in your textbox & pass the text you want to select, in this specific case it is "javascript".
public void selectOptionWithText(String textToSelect) {
try {
//Add the below sleep if necessary
// Thread.sleep(1000);
WebElement autoOptions = driver.findElement(By.className("autocomplete"));
List<WebElement> optionsToSelect = autoOptions().findElements(By.tagName("div"));
for (WebElement option : optionsToSelect) {
if (option.getText().equals(textToSelect)) {
System.out.println("Trying to select: " + textToSelect);
option.click();
break;
}
}
}
catch(Exception e){
System.out.println("Error");
}
}
There are few answers with code here. So, I will do my contribution.
The code that I'm using to select an item in the autocomplete component from PrimeFaces 2.2:
driver.findElement(By.id(codigoBanco_input)).sendKeys("text");
waitForElementLocated(By.cssSelector(listItensSelector), 5);
List<WebElement> listItems = driver.findElements(By.cssSelector(listItensSelector));
Actions builder = new Actions(driver);
builder.moveToElement(listItems.get(0)).build().perform();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(itemSelector)));
driver.findElement(By.cssSelector(itemSelector)).click();
We had some problems with typeKeys. sendKeys seems to become the final solution, but it is still experimental. From the reference:
This command is experimental. It may replace the typeKeys command in the future.
For those who are interested in the details, unlike the typeKeys command, which tries to fire the keyDown, the keyUp and the keyPress events, this command is backed by the atoms from Selenium 2 and provides a much more robust implementation that will be maintained in the future.
I found I needed to do a focus on the field before doing typeKeys to get it to work.