2

Is it possible to open a Vaadin ComboBox by code?

I'd like to present 2 comboboxes that depend on each other. When the user selects a value in the first, I'd like the 2nd combobox to automatically open the possible selections, so that the user can directly select one, instead of having to open the 2nd combobox himself.

Maybe there is an event that I could send to trigger the opening?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

3

I've done this in Vaadin 7 with Selenium WebDriver:

public void selectValueInCombobox(WebElement cmb, String value) {
    cmb.findElement(By.tagName("div")).click(); //shows the menu
    List<WebElement> findElements = webDriver.findElements(By.cssSelector("td[role='listitem']"));
    findElements.stream()
        .filter(item-> value.equals(item.findElement(By.tagName("span")).getText()))
        .findFirst().get().click();
}

The WebElement is the Combobox (its the div when rendered in html), and value is the listitem in the combobox you want selected.

  1. It clicks the down arrow button on the rightmost (its an inner div)
  2. Which renders the listitem html elements, just find those with role is listitem, then via Java 8's stream, filter with the same value
  3. Once found, click and it will be selected
Carlos Jaime C. De Leon
  • 2,476
  • 2
  • 37
  • 53
2

The only thing I can think of is, after giving focus to the combobox, try sending it a keystroke (ie the down arrow to try and make it open). There is a good example here

The other way, maybe to try and use Selenium to trigger the combobox, but thats probably overkill. For examples of that, look into vaadin's testbench.

Community
  • 1
  • 1
nuzz
  • 647
  • 7
  • 18