testbench icon indicating copy to clipboard operation
testbench copied to clipboard

ComboBoxElement.selectByText() not working when custom values are allowed

Open tarekoraby opened this issue 3 years ago • 1 comments

ComboBoxElement.selectByText is not working when using custom values are allowed.

Vaadin 14.3.7. Tested on Chrome and Firefox

To reproduce, based on this view:

comboBox.setItems("00", "11", "22", "33");
comboBox.addCustomValueSetListener(e -> {
    comboBox.setValue(e.getDetail());
});
add(comboBox);

Run the following test:

@Test
public void testComboBox() throws InterruptedException {
    ComboBoxElement combo = $(ComboBoxElement.class).first();
    combo.selectByText("11");
    Thread.sleep(5000);
    Assert.assertEquals("11", combo.getSelectedText());
}

Expected: the test would pass

Actual: The equality assertion fails due to the combo value being equal to "2" (rather than "11");

tarekoraby avatar Oct 06 '20 07:10 tarekoraby

Here's a workaround that has been reported as working by a customer. It gives an alternative method to selectByText(String). that works when custom values are allowed. It extends the ComboBoxElement.

public class EnhancedComboBoxElement extends ComboBoxElement {
         public void selectByOption(String optionToSelect) {
                openPopup();
                goToNextSelection();
                 for(String option: getOptions()) {
                          if(option.equals(optionToSelect)) {
                                 selectOption();
                                  return;
                         }
                         goToNextSelection();
                }
                 throw new IllegalArgumentException(“Value ‘” + optionToSelect + “’ not found in the combobox”);
         }
         private void goToNextSelection() {
                sendKeys(Keys.ARROW_DOWN);
        }
         private void selectOption() {
                sendKeys(Keys.RETURN);
         }
}

Peppe avatar Mar 05 '21 09:03 Peppe