DropDownSelectAction extends FindElementAction { @Override protected void execute(WebElement webElement, SeleniumBrowser browser, TestContext context) { super.execute(webElement, browser, context); Select dropdown = new Select(webElement); if (StringUtils.hasText(option)) { dropdown.selectByValue(context.replaceDynamicContentInString(option)); } if (!CollectionUtils.isEmpty(options)) { if (BrowserType.IE.equals(browser.getEndpointConfiguration().getBrowserType())) { for (String option : options) { dropdown.selectByValue(context.replaceDynamicContentInString(option)); } } else { List<WebElement> optionElements = dropdown.getOptions(); Actions builder = new Actions(browser.getWebDriver()); builder.keyDown(Keys.CONTROL); for (String optionValue : options) { for (WebElement option : optionElements) { if (!option.isSelected() && isSameValue(option, context.replaceDynamicContentInString(optionValue))) { builder.moveToElement(option).click(option); } } } builder.keyUp(Keys.CONTROL); Action multiple = builder.build(); multiple.perform(); } } } DropDownSelectAction(Builder builder); String getOption(); List<String> getOptions();  }
@Test public void testExecuteSelect() throws Exception { WebElement option = Mockito.mock(WebElement.class); when(webDriver.findElement(any(By.class))).thenReturn(element); when(element.findElements(any(By.class))).thenReturn(Collections.singletonList(option)); when(option.isSelected()).thenReturn(false); DropDownSelectAction action = new DropDownSelectAction.Builder() .browser(seleniumBrowser) .element("name", "dropdown") .option("select_me") .build(); action.execute(context); verify(option).click(); }
@Test public void testExecuteMultiSelect() throws Exception { WebElement option = Mockito.mock(WebElement.class); seleniumBrowser.getEndpointConfiguration().setBrowserType(BrowserType.IE); when(webDriver.findElement(any(By.class))).thenReturn(element); when(element.findElements(any(By.class))).thenReturn(Collections.singletonList(option)); when(option.isSelected()).thenReturn(false); DropDownSelectAction action = new DropDownSelectAction.Builder() .browser(seleniumBrowser) .element("name", "dropdown") .options("option1", "option2") .build(); action.execute(context); verify(option, times(2)).click(); }