@Override public Void execute(SelenideElement proxy, WebElementSource selectField, Object[] args) { String text = (String) args[0]; WebElement element = selectField.getWebElement(); Select select = new Select(element); List<WebElement> options = element.findElements(By.xpath( ".//option[contains(normalize-space(.), " + Quotes.escape(text) + ")]")); if (options.isEmpty()) { throw new NoSuchElementException("Cannot locate option containing text: " + text); } for (WebElement option : options) { setSelected(option); if (!select.isMultiple()) { break; } } return null; }
private Object getInputValue(WebElement input) { Object value = null; String type = input.getAttribute("type"); if ("select".equals(input.getTagName())) { Select select = new Select(input); if (select.isMultiple()) { value = select.getAllSelectedOptions().stream().map(this::getValue) .collect(Collectors.toList()); } else { value = getValue(select.getFirstSelectedOption()); } } else if (Arrays.asList("checkbox", "radio").contains(type)) { if (input.isSelected()) { value = getValue(input); } else { if (Objects.equals(type, "checkbox")) { value = false; } } } else { value = getValue(input); } return value; }
/** * Wraps Selenium's method. * * @return boolean if this is a multiselect. * @see org.openqa.selenium.support.ui.Select#isMultiple() */ public boolean isMultiple() { return innerSelect.isMultiple(); }
protected boolean isMultiselectList(Select select) { return select.isMultiple(); }
/** * Indicates whether this select element support selecting multiple options at the same time. * This is done by checking the value of the "multiple" attribute. * * @return {@code true} if select element support selecting multiple options and {@code false} otherwise. */ public boolean isMultiple() { return getSelect().isMultiple(); }
/** * Is the component a multi-select list? * * @return {@code true} if the component is a multi-select */ public boolean isMultiSelect() { Select se = new Select(this); return se.isMultiple(); }
public Boolean isDropdownForMultiSelection(WebElement element) { try { return driverDropdown(element).isMultiple(); } catch (Exception e) { return FALSE; } }
/** * Indicates whether this select element support selecting multiple options at the same time. * This is done by checking the value of the "multiple" attribute. * * @return {@code true} if select element support selecting multiple options and {@code false} otherwise. */ public boolean isMultiple() { return getSelect().isMultiple(); }
/** * Indicates whether this select element support selecting multiple options at the same time. * This is done by checking the value of the "multiple" attribute. * * @return {@code true} if select element support selecting multiple options and {@code false} otherwise. */ public boolean isMultiple() { return getSelect().isMultiple(); }
public Boolean execute() { return getSelect().isMultiple(); } }
Select selectElement = new Select(driver.findElement(By.Id("productId"))); if (selectElement.isMultiple()) { /* step 1 */ List<WebElement> options = selectElement.getOptions(); /* step 2 */ for (WebElement we : options) { /* step 3 */ we.selectByVisibleText(we.getText()); } } else { // does not support multiple }
/** * Select the option at the given index. This is done by examing the "index" attribute of an * element, and not merely by counting. * * @param index The option at this index will be selected */ public void selectByIndex(int index) { String match = String.valueOf(index); for (WebElement option : getOptions()) { if (match.equals(option.getAttribute("index"))) { option.setSelected(); if (isMultiple()) { return; } } } }
/** * Select the option at the given index. This is done by examing the "index" attribute of an * element, and not merely by counting. * * @param index The option at this index will be selected */ public void selectByIndex(int index) { String match = String.valueOf(index); for (WebElement option : getOptions()) { if (match.equals(option.getAttribute("index"))) { option.setSelected(); if (isMultiple()) { return; } } } }
@Override protected void doPerform() { Select select = getSelectElement(); if (!select.isMultiple()) { throw new UnsupportedOperationException("You may only deselect all options of a multi-select"); } for (WebElement option : select.getOptions()) { if (!option.isSelected()) { option.click(); } } } }
@Override public void setValue(String value) { if (select().isMultiple()) check(value.split(";")); else select(value); } @Override
/** * Clear all selected entries. This is only valid when the SELECT supports multiple selections. * * @throws UnsupportedOperationException If the SELECT does not support multiple selections */ public void deselectAll() { if (!isMultiple()) { throw new UnsupportedOperationException( "You may only deselect all options of a multi-select"); } for (WebElement option : getOptions()) { if (option.isSelected()) { option.toggle(); } } }
@Override public String getValue() { return select().isMultiple() ? print(checked(),";") : selected(); }
/** * * @param options the options that are being selected * @throws WidgetException */ private void selectMultiple(List<String> options) throws WidgetException { org.openqa.selenium.support.ui.Select selectBox = new org.openqa.selenium.support.ui.Select( findElement()); if (!selectBox.isMultiple()) { throw new WidgetException("This is not multiple select box", getByLocator()); } for (String option : options) { selectBox.selectByVisibleText(option); } }
/** * * @param options the options that are being selected * @throws WidgetException */ private void selectMultiple(List<String> options) throws WidgetException { org.openqa.selenium.support.ui.Select selectBox = new org.openqa.selenium.support.ui.Select( findElement()); if (!selectBox.isMultiple()) { throw new WidgetException("This is not multiple select box", getByLocator()); } for (String option : options) { selectBox.selectByVisibleText(option); } }
/** * Constructor. * * @param context context. * @param selectLocator select locator. */ public SelectElement(Context context, String selectLocator) { WebDriver driver = context.getWrappedDriver(); finder = context.getElementFinder(); select = finder.findElement(driver, selectLocator); context.getJSLibrary().replaceAlertMethod(driver, select); selectUI = new Select(select); isMultiple = selectUI.isMultiple(); }