suggestions = getPopupSuggestionElements();
for (WebElement suggestion : suggestions) {
String text = suggestion.getText();
if (!text.isEmpty()) {
suggestionsTexts.add(text);
}
}
return suggestionsTexts;
}
/**
* Gets the elements of all suggestions on the current page.
*
* Opens the popup if not already open.
*
* @return a list of elements for the suggestions on the current page
*/
public List getPopupSuggestionElements() {
List tables = getSuggestionPopup()
.findElements(By.tagName("table"));
if (tables == null || tables.isEmpty()) {
return Collections.emptyList();
}
WebElement table = tables.get(0);
return table.findElements(By.tagName("td"));
}
/**
* Opens next popup page.
*
* @return True if next page opened. false if doesn't have next page
*/
public boolean openNextPage() {
try {
clickElement(getSuggestionPopup().findElement(byNextPage));
return true;
} catch (WebDriverException e) {
// PhantomJS driver can throw WDE instead of the more specific
// NoSuchElementException
return false;
}
}
/**
* Open previous popup page.
*
* @return True if previous page opened. False if doesn't have previous page
*/
public boolean openPrevPage() {
try {
clickElement(getSuggestionPopup().findElement(byPrevPage));
return true;
} catch (WebDriverException e) {
// PhantomJS driver can throw WDE instead of the more specific
// NoSuchElementException
return false;
}
}
/**
* Returns the suggestion popup element.
*/
public WebElement getSuggestionPopup() {
ensurePopupOpen();
return findElement(bySuggestionPopup);
}
/**
* Return value of the combo box element.
*
* @return value of the combo box element
*/
public String getValue() {
return getInputField().getAttribute("value");
}
/**
* Returns the text input field element, used for entering text into the
* combo box.
*
* @return the input field element
*/
public WebElement getInputField() {
return findElement(By.vaadin("#textbox"));
}
private void ensurePopupOpen() {
if (!isElementPresent(bySuggestionPopup)) {
openPopup();
}
}
@Override
public String getText() {
return getInputField().getAttribute("value");
}
@Override
public void clear() {
getInputField().clear();
}
@Override
public void sendKeys(CharSequence... keysToSend) {
sendKeys(50, keysToSend);
}
/**
* Use this method to simulate typing into an element, which may set its
* value.
*
* @param delay
* delay after sending each individual key (mainly needed for
* PhantomJS)
* @param keysToSend
* keys to type into the element
*/
public void sendKeys(int delay, CharSequence... keysToSend) {
WebElement input = getInputField();
for (CharSequence key : keysToSend) {
input.sendKeys(key);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
}
}
}
private void clickElement(WebElement element) {
if (isFirefox()) {
// Workaround for Selenium/TB and Firefox 45 issue
((TestBenchElement) element).clickHiddenElement();
} else {
element.click();
}
}
}