Example usage for org.openqa.selenium.support.ui Select isMultiple

List of usage examples for org.openqa.selenium.support.ui Select isMultiple

Introduction

In this page you can find the example usage for org.openqa.selenium.support.ui Select isMultiple.

Prototype

@Override
public boolean isMultiple() 

Source Link

Usage

From source file:com.chtr.tmoauto.webui.CommonFunctions.java

License:Open Source License

@Override
public void fw_addSelection(String locator, String optionLocator) {
    Select sel = new Select(findElement(locator));
    if (sel.isMultiple()) {
        sel.selectByVisibleText(optionLocator);
    }//from w  w  w  .ja  v  a  2s. c o  m
}

From source file:com.liferay.faces.test.showcase.select.SelectTester.java

License:Open Source License

/**
 * Click an option and wait for Ajax to rerender the option. This method exists because {@link
 * Browser#clickAndWaitForAjaxRerender(java.lang.String)} does not work on selectOneMenu, selectManyListbox, and
 * SelectManyMenu. For more information see method comments.
 *//*w w  w . j  a  v a  2  s  .  c om*/
protected void clickOptionAndWaitForAjaxRerender(Browser browser, String optionXpath) {

    // SelectOneMenu and SelectManyMenu tests occasionally fail due to elements moving off screen, so center the
    // element in the view.
    if (this.getClass().getName().contains("Menu")) {
        browser.centerElementInView(optionXpath);
    }

    WebElement optionElement = browser.findElementByXpath(optionXpath);

    // Note: clicking a selectOneMenu option on Chrome and PhantomJS via Actions.click(WebElement) (which is used
    // by Browser.clickAndWaitForAjaxRerender(String)) does not fire the select element's change event, so the
    // element must be clicked via Element.click().
    optionElement.click();

    // phantomjs browser does not fire a change event when a <select multiple="multiple"> <option> is clicked, so
    // the event must be fired manually.
    if ("phantomjs".equals(browser.getName())) {

        try {

            WebElement selectElement = optionElement.findElement(By.xpath(".."));
            Select select = new Select(selectElement);

            if (select.isMultiple()) {
                browser.executeScript(FIRE_SELECT_CHANGE_EVENT_SCRIPT, optionElement);
            }
        } catch (StaleElementReferenceException e) {
            // do nothing. The element is stale because an ajax rerender has correctly occured.
        }
    }

    browser.waitUntil(ExpectedConditions.stalenessOf(optionElement));
    browser.waitForElementVisible(optionXpath);
}

From source file:com.liferay.faces.test.showcase.TesterBase.java

License:Open Source License

/**
 * Click an option and wait for Ajax to rerender the option. This method exists because {@link
 * BrowserDriver#clickElementAndWaitForRerender(java.lang.String)} does not work on selectOneMenu,
 * selectManyListbox, and SelectManyMenu. For more information see method comments.
 *//*from  w  w  w.j  a  v  a  2  s.co m*/
protected void clickOptionAndWaitForRerender(BrowserDriver browserDriver, String optionXpath) {

    browserDriver.centerElementInCurrentWindow(optionXpath);

    WebElement optionElement = browserDriver.findElementByXpath(optionXpath);

    // Note: clicking a selectOneMenu option on Chrome and PhantomJS via Actions.click(WebElement) (which is used
    // by Browser.clickAndWaitForAjaxRerender(String)) does not fire the select element's change event, so the
    // element must be clicked via Element.click().
    optionElement.click();

    // phantomjs browser does not fire a change event when a <select multiple="multiple"> <option> is clicked,
    // so the event must be fired manually.
    if ("phantomjs".equals(browserDriver.getBrowserName())) {

        try {

            WebElement selectElement = optionElement.findElement(By.xpath(".."));
            Select select = new Select(selectElement);

            if (select.isMultiple()) {
                browserDriver.executeScriptInCurrentWindow(FIRE_SELECT_CHANGE_EVENT_SCRIPT, optionElement);
            }
        } catch (StaleElementReferenceException e) {
            // do nothing. The element is stale because an ajax rerender has correctly occured.
        }
    }

    browserDriver.waitFor(ExpectedConditions.stalenessOf(optionElement));
    browserDriver.waitForElementEnabled(optionXpath);
}

From source file:com.screenslicer.core.scrape.QueryForm.java

License:Open Source License

public static void perform(RemoteWebDriver driver, FormQuery context) throws ActionFailed {
    try {// w ww .j a  va  2s . c  o  m
        Util.get(driver, context.site, true);
        Util.doClicks(driver, context.preAuthClicks, null);
        QueryCommon.doAuth(driver, context.credentials);
        Util.doClicks(driver, context.preSearchClicks, null);
        Map<String, HtmlNode> formControls = new HashMap<String, HtmlNode>();
        for (int i = 0; i < context.formSchema.length; i++) {
            formControls.put(context.formSchema[i].guid, context.formSchema[i]);
        }
        Map<String, List<String>> formData = context.formModel;
        boolean valueChanged = false;
        int count = 0;
        final int MAX_TRIES = 3;
        Element body = Util.openElement(driver, null, null, null);
        do {
            ++count;
            valueChanged = false;
            for (Map.Entry<String, List<String>> entry : formData.entrySet()) {
                try {
                    HtmlNode formControl = formControls.get(entry.getKey());
                    if (!CommonUtil.isEmpty(entry.getValue())) {
                        if ("select".equalsIgnoreCase(formControl.tagName)) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: select");
                            }
                            Select select = new Select(Util.toElement(driver, formControl, body));
                            if (select.isMultiple()) {
                                select.deselectAll();
                            }
                            List<WebElement> selectedElements = select.getAllSelectedOptions();
                            List<String> selectedStrings = new ArrayList<String>();
                            for (WebElement selectedElement : selectedElements) {
                                String selectedString = selectedElement.getAttribute("value");
                                if (!CommonUtil.isEmpty(selectedString)) {
                                    selectedStrings.add(selectedString);
                                }
                            }
                            boolean matches = true;
                            for (String selectedString : selectedStrings) {
                                if (!entry.getValue().contains(selectedString)) {
                                    matches = false;
                                    break;
                                }
                            }
                            if (!matches || selectedStrings.size() != entry.getValue().size()) {
                                for (String val : entry.getValue()) {
                                    valueChanged = true;
                                    select.selectByValue(val);
                                    Util.driverSleepVeryShort();
                                }
                            }
                        } else if ("input".equalsIgnoreCase(formControl.tagName)
                                && ("text".equalsIgnoreCase(formControl.type)
                                        || "search".equalsIgnoreCase(formControl.type))) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: input[text|search]");
                            }
                            WebElement element = Util.toElement(driver, formControl, body);
                            valueChanged = QueryCommon.typeText(driver, element, entry.getValue().get(0), true,
                                    false);
                        } else if ("input".equalsIgnoreCase(formControl.tagName)
                                && ("checkbox".equalsIgnoreCase(formControl.type)
                                        || "radio".equalsIgnoreCase(formControl.type))) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: input[checkbox|radio]");
                            }
                            WebElement element = Util.toElement(driver, formControl, body);
                            if (entry.getValue() != null && !entry.getValue().isEmpty()) {
                                if ("radio".equalsIgnoreCase(formControl.type)) {
                                    String elementVal = element.getAttribute("value");
                                    String schemaVal = formControl.value;
                                    String modelVal = entry.getValue().get(0);
                                    if (elementVal != null && schemaVal != null
                                            && elementVal.equalsIgnoreCase(schemaVal)
                                            && modelVal.equalsIgnoreCase(schemaVal)) {
                                        if (!element.isSelected()) {
                                            if (WebApp.DEBUG) {
                                                System.out.println("Clicking radio button");
                                            }
                                            valueChanged = Util.click(driver, element);
                                        }
                                    }
                                } else if (!element.isSelected()) {
                                    if (WebApp.DEBUG) {
                                        System.out.println("Clicking [checkbox|radio]");
                                    }
                                    valueChanged = Util.click(driver, element);
                                }
                            } else {
                                if (element.isSelected()) {
                                    if (WebApp.DEBUG) {
                                        System.out.println("Deselecting [checkbox|radio]");
                                    }
                                    valueChanged = true;
                                    element.clear();
                                    Util.driverSleepVeryShort();
                                }
                            }
                        }
                    }
                } catch (Throwable t) {
                    Log.exception(t);
                }
            }
        } while (valueChanged && count < MAX_TRIES);
        doSubmit(driver, context.formId);
        Util.doClicks(driver, context.postSearchClicks, null);
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.stratio.qa.specs.WhenGSpec.java

License:Apache License

/**
 * Choose no option from a select webelement found previously
 *
 * @param index/*from ww w .j a v a  2 s  .  c o m*/
 */
@When("^I de-select every item on the element on index '(\\d+?)'$")
public void elementDeSelect(Integer index) {
    Select sel = null;
    sel = new Select(commonspec.getPreviousWebElements().getPreviousWebElements().get(index));

    if (sel.isMultiple()) {
        sel.deselectAll();
    }
}

From source file:com.vilt.minium.impl.actions.SelectAllInteraction.java

License:Apache License

@Override
protected void doPerform() {
    Select select = getSelectElement();
    if (!select.isMultiple()) {
        throw new UnsupportedOperationException("You may only deselect all options of a multi-select");
    }// w  w w . j  a  v a  2 s.c om

    for (WebElement option : select.getOptions()) {
        if (!option.isSelected()) {
            option.click();
        }
    }
}

From source file:io.github.seleniumquery.functions.jquery.forms.ValFunction.java

License:Apache License

/**
 * <p>Gets the value of the given element, if its tag name is INPUT, OPTION, SELECT or TEXTAREA.</p>
 * Otherwise it returns an empty string.
 *
 * @param element The element you want the value of.
 * @return The value of the element.//from  ww  w. ja v  a  2  s . c  om
 * @since 0.9.0
 */
public static String val(WebElement element) {
    String tagName = element.getTagName();
    if (isInputTag(element) || isOptionTag(element)) {
        return element.getAttribute("value");
    } else if (isSelectTag(element)) {
        Select select = new Select(element);
        if (select.isMultiple())
            return select.getAllSelectedOptions().stream().map(we -> we.getAttribute("value"))
                    .collect(Collectors.joining(","));
        else
            return select.getFirstSelectedOption().getAttribute("value");
    } else if (isTextareaTag(element)) {
        // see issue#59 - <textarea> returns wrong value (original value) for getText() in Firefox
        // It used to be element.getText(). We use .getAttribute("value") because it works for everyone.
        return element.getAttribute("value");
    }
    LOGGER.warn("Attempting to call .val() in an element of type '" + tagName + "': " + element
            + ". Returning empty string.");
    return "";
}

From source file:io.spring.initializr.web.project.HomePage.java

License:Apache License

private Object getInputValue(WebElement input) {
    Object value = null;/* w w w  . j ava 2s .  c  o  m*/
    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;
}

From source file:net.sourceforge.jwebunit.webdriver.WebDriverTestingEngineImpl.java

License:Open Source License

public void selectOptions(String selectName, int index, String[] optionValues) {
    Select select = new Select(getWebElementByXPath(
            "//select[@name=" + escapeQuotes(selectName) + "][" + (index + 1) + "]", true, true));
    if (!select.isMultiple() && optionValues.length > 1)
        throw new RuntimeException("Multiselect not enabled");
    for (String option : optionValues) {
        boolean found = false;
        for (WebElement opt : select.getOptions()) {
            if (opt.getAttribute("value").equals(option)) {
                select.selectByValue(option);
                found = true;//from  www .  j a v  a  2s . c o m
                break;
            }
        }
        if (!found) {
            throw new RuntimeException("Option " + option + " not found");
        }
    }
}

From source file:net.sourceforge.jwebunit.webdriver.WebDriverTestingEngineImpl.java

License:Open Source License

public void unselectOptions(String selectName, int index, String[] optionValues) {
    Select select = new Select(getWebElementByXPath(
            "//select[@name=" + escapeQuotes(selectName) + "][" + (index + 1) + "]", true, true));
    if (!select.isMultiple() && optionValues.length > 1)
        throw new RuntimeException("Multiselect not enabled");
    for (String option : optionValues) {
        boolean found = false;
        for (WebElement opt : select.getOptions()) {
            if (opt.getAttribute("value").equals(option)) {
                select.deselectByValue(option);
                found = true;/*from  w  w  w.j ava 2 s .  c  o  m*/
                break;
            }
        }
        if (!found) {
            throw new RuntimeException("Option " + option + " not found");
        }
    }
}