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
A little variation using Prashanth's answer:
/**
* Selects the element at position idx from the autocomplete combo, considering the partialKeyword
* @param driver
* @param element
* @param partialKeyword
* @param idx
* @throws InterruptedException
*/
public static void selectAutoCompleteValue(WebDriver driver, WebElement element, String partialKeyword, Integer idx) throws InterruptedException{
element.sendKeys(partialKeyword);
Thread.sleep(1000);
List <WebElement> listItems = driver.findElements(By.cssSelector(".ui-autocomplete-item.ui-autocomplete-list-item"));
listItems.get(idx).click();
}
Hope this helps!
Please use typeKeys instead of type. Also use mouseDown instead of click. It works fine.
Patrick's answer is definitely important, I also found that focus and mouseDown is needed in the last versions of Jquery UI. I recorded a video of a test so that you can see it running in Sauce Labs: https://saucelabs.com/jobs/ad8c561be39bb7a42c9bb3a063214c95
Sometime the TypeKeys Doesn't work. At this time you can use keyDown
click the inputbox and type value and keyDown in the box.
I used following sequence in IDE,
and worked well
The type command may not be enough to trigger the autocomplete. Dave Webb's suggestions are otherwise spot on. My only addition would be that you might need the typeKeys command, which causes slightly different JavaScript events to be fired, which may be more likely to trigger the autocomplete widget.