Example usage for org.openqa.selenium WebElement isSelected

List of usage examples for org.openqa.selenium WebElement isSelected

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement isSelected.

Prototype

boolean isSelected();

Source Link

Document

Determine whether or not this element is selected or not.

Usage

From source file:com.induscorp.prime.testing.ui.standard.domobj.RadioButtonGroupValidatorSD.java

License:Open Source License

@Override
public void validateSelectedOption(String value, String displayValue, int numRetries) {
    List<WebElement> webElems = findElements(numRetries);
    String elemVal;/*from ww w . ja  va2s.  co  m*/
    boolean optionFound = false;
    for (WebElement elem : webElems) {
        elemVal = elem.getAttribute("value");
        if (elemVal != null && elemVal.equals(value) && elem.isSelected()) {
            optionFound = true;
            break;
        }
    }
    Assert.assertTrue(optionFound, "Radio button with value '" + displayValue + "' is not selected.");
}

From source file:com.induscorp.prime.testing.ui.standard.domobj.RadioButtonGroupValidatorSD.java

License:Open Source License

@Override
public void validateNotSelectedOptions(ItemMap<String, String> options, int numRetries) {
    List<WebElement> webElems = findElements(numRetries);
    String elemVal;/*from  w  w  w  . j av a 2s .  c om*/
    LinkedHashMap<String, String> selectedOptions = new LinkedHashMap<String, String>();
    for (String optionValue : options.getItems().keySet()) {
        for (WebElement elem : webElems) {
            elemVal = elem.getAttribute("value");
            if (elemVal != null && elemVal.equals(optionValue) && elem.isSelected()) {
                selectedOptions.put(optionValue, options.getItems().get(optionValue));
            }
        }
    }

    if (selectedOptions.size() > 0) {
        Assert.fail("Radio button group '" + uiObject.getDisplayName()
                + "' has some of the options selected. Selected options: " + selectedOptions);
    }
}

From source file:com.induscorp.prime.testing.ui.standard.domobj.RadioButtonValidatorSD.java

License:Open Source License

@Override
public void validateSelected(int numRetries) {
    WebElement webElem = findElement(numRetries);
    Assert.assertTrue(webElem.isSelected(),
            "Radio button '" + uiObject.getDisplayName() + "' is not selected.");
}

From source file:com.induscorp.prime.testing.ui.standard.domobj.RadioButtonValidatorSD.java

License:Open Source License

@Override
public void validateNotSelected(int numRetries) {
    WebElement webElem = findElement(numRetries);
    Assert.assertFalse(webElem.isSelected(), "Radio button '" + uiObject.getDisplayName() + "' is selected.");
}

From source file:com.liferay.cucumber.selenium.BaseWebDriverImpl.java

License:Open Source License

@Override
public boolean isChecked(String locator) {
    WebElement webElement = getWebElement(locator, "1");

    scrollWebElementIntoView(webElement);

    return webElement.isSelected();
}

From source file:com.liferay.cucumber.selenium.WebDriverHelper.java

License:Open Source License

public static void check(WebDriver webDriver, String locator) {
    WebElement webElement = getWebElement(webDriver, locator);

    if (!webElement.isSelected()) {
        webElement.click();/*w w w  .  j  a  v a  2  s.  co m*/
    }
}

From source file:com.liferay.cucumber.selenium.WebDriverHelper.java

License:Open Source License

public static void uncheck(WebDriver webdDriver, String locator) {
    WebElement webElement = getWebElement(webdDriver, locator);

    if (webElement.isSelected()) {
        webElement.click();/*from  ww  w. j  a  va2  s.c o  m*/
    }
}

From source file:com.mgmtp.jfunk.web.step.JFunkWebElement.java

License:Apache License

/**
 * @throws StepException//from  ww w. j ava2 s.c o  m
 *             <ul>
 *             <li>if element specified by {@link By} object in the constructor cannot be found</li>
 *             <li>if a validation error occurred while checking the value of the WebElement
 *             against the desired value</li>
 *             </ul>
 */
@Override
public void execute() throws StepException {
    elementValue = retrieveElementValue();

    final WebDriverWait wait = new WebDriverWait(getWebDriver(), WebConstants.DEFAULT_TIMEOUT);
    final WebElement element = wait.until(new Function<WebDriver, WebElement>() {

        @Override
        public WebElement apply(final WebDriver input) {
            List<WebElement> webElements = input.findElements(by);
            if (webElements.isEmpty()) {
                throw new StepException("Could not find any matching element; By=" + by.toString());
            }
            /*
             * If the search using the By object does find more than one matching element we are
             * looping through all elements if we find at least one which matches the criteria
             * below. If not, an exception is thrown.
             */
            for (WebElement webElement : webElements) {
                if (webElement.isDisplayed()) {
                    if (webElement.isEnabled() || !webElement.isEnabled()
                            && (stepMode == StepMode.CHECK_DEFAULT || stepMode == StepMode.CHECK_VALUE)) {
                        return webElement;
                    }
                }
            }
            throw new StepException("All elements matching by=" + by + " were either invisible or disabled");
        }
    });

    switch (stepMode) {
    case CHECK_DEFAULT:
        // Check only for text input and textarea
        if (element.getTagName().equals(WebConstants.INPUT)
                && element.getAttribute(WebConstants.TYPE).equals(WebConstants.TEXT)
                || element.getTagName().equals(WebConstants.TEXTAREA)) {
            log.info(toString());
            String value = element.getAttribute(WebConstants.VALUE);
            if (!DataUtils.isDefaultValue(value)) {
                throw new ValidationException("Wrong default value=" + value + " of " + this);
            }
        }
        break;
    case CHECK_VALUE:
        String checkValue = elementValue;
        if (checkTrafo != null) {
            checkValue = checkTrafo.trafo(checkValue);
        }

        log.info(this + ", checkValue=" + checkValue);
        if (element.getTagName().equalsIgnoreCase(WebConstants.SELECT)) {
            Select select = new Select(element);
            String value = select.getFirstSelectedOption().getAttribute(WebConstants.VALUE);
            if (!Objects.equal(checkValue, value)) {
                String text = select.getFirstSelectedOption().getText();
                if (!Objects.equal(checkValue, text)) {
                    throw new InvalidValueException(element, checkValue,
                            text + " (option value=" + value + ")");
                }
            }
        } else if (WebConstants.INPUT.equalsIgnoreCase(element.getTagName())
                && WebConstants.RADIO.equals(element.getAttribute(WebConstants.TYPE))) {
            List<WebElement> elements = getWebDriver().findElements(by);
            for (WebElement webElement : elements) {
                if (webElement.isDisplayed() && webElement.isEnabled()) {
                    String elVal = webElement.getAttribute(WebConstants.VALUE);
                    if (elVal.equals(checkValue) && !webElement.isSelected()) {
                        throw new InvalidValueException(element, checkValue, elVal);
                    }
                }
            }
        } else if (WebConstants.CHECKBOX.equals(element.getAttribute(WebConstants.TYPE))) {
            boolean elVal = element.isSelected();
            if (elVal != Boolean.valueOf(checkValue)) {
                throw new InvalidValueException(element, checkValue, String.valueOf(elVal));
            }
        } else {
            String value = element.getAttribute(WebConstants.VALUE);
            if (!Objects.equal(checkValue, value)) {
                throw new InvalidValueException(element, checkValue, value);
            }
        }
        break;
    case SET_VALUE:
        String setValue = elementValue;
        if (setTrafo != null) {
            setValue = setTrafo.trafo(setValue);
        }
        log.info(this + (setTrafo != null ? ", setValue (after trafo)=" + setValue : ""));
        if (element.getTagName().equalsIgnoreCase(WebConstants.SELECT)) {
            Select select = new Select(element);
            // First check if a matching value can be found
            List<WebElement> options = select.getOptions();
            boolean found = false;
            for (WebElement option : options) {
                String optionValue = option.getAttribute(WebConstants.VALUE);
                if (StringUtils.equals(optionValue, setValue)) {
                    /*
                     * WebElement with matching value could be found --> we are finished
                     */
                    found = true;
                    select.selectByValue(setValue);
                    break;
                }
            }
            if (!found) {
                /*
                 * Fallback: look for a WebElement with a matching visible text
                 */
                for (WebElement option : options) {
                    String visibleText = option.getText();
                    if (StringUtils.equals(visibleText, setValue)) {
                        /*
                         * WebElement with matching value could be found --> we are finished
                         */
                        found = true;
                        select.selectByVisibleText(setValue);
                        break;
                    }
                }
            }
            if (!found) {
                throw new StepException(
                        "Could not find a matching option element in " + element + " , By: " + by.toString());
            }
        } else if (WebConstants.INPUT.equalsIgnoreCase(element.getTagName())
                && WebConstants.RADIO.equals(element.getAttribute(WebConstants.TYPE))) {
            List<WebElement> elements = getWebDriver().findElements(by);
            for (WebElement webElement : elements) {
                if (webElement.isDisplayed() && webElement.isEnabled()) {
                    String elVal = webElement.getAttribute(WebConstants.VALUE);
                    if (elVal.equals(setValue) && !webElement.isSelected()) {
                        webElement.click();
                    }
                }
            }
        } else if (WebConstants.CHECKBOX.equals(element.getAttribute(WebConstants.TYPE))) {
            if (Boolean.valueOf(setValue) && !element.isSelected()
                    || !Boolean.valueOf(setValue) && element.isSelected()) {
                element.click();
            }
        } else {
            if (element.isDisplayed() && element.isEnabled() && (element.getAttribute("readonly") == null
                    || element.getAttribute("readonly").equals("false"))) {
                element.clear();
                element.sendKeys(setValue);
            } else {
                log.warn("Element is invisible or disabled, value cannot be set");
            }
        }
        break;
    case NONE:
        // do nothing
        break;
    default:
        throw new IllegalArgumentException("Unhandled StepMode=" + stepMode);
    }
}

From source file:com.mgmtp.jfunk.web.util.FormInputHandler.java

License:Apache License

/**
 * Tries to find the field and sets or checks its value depending on the {@link StepMode}.
 *///from www .j  ava2  s . c om
public void perform() {
    log.info(toString());

    WebElement element = finder.find();

    switch (stepMode) {
    case CHECK_VALUE:
        String checkValue = retrieveValue();
        if (checkTrafo != null) {
            checkValue = checkTrafo.trafo(checkValue);
        }
        checkValue(element, checkValue);
        break;

    case CHECK_DEFAULT:
        checkState(defaultsProvider != null, "No DefaultsProvider set when StepMode is CHECK_DEFAULT.");
        checkValue(element, defaultsProvider.get(element, dataSet, dataKey, dataIndex));
        break;

    case SET_VALUE:
        String setValue = retrieveValue();
        if (setTrafo != null) {
            setValue = setTrafo.trafo(setValue);
        }

        if (element.getTagName().equalsIgnoreCase(WebConstants.SELECT)) {

            Select select = new Select(element);
            // First check if a matching value can be found
            List<WebElement> options = select.getOptions();
            boolean found = false;
            for (WebElement option : options) {
                String optionValue = option.getAttribute(WebConstants.VALUE);
                if (StringUtils.equals(optionValue, setValue)) {
                    /*
                     * WebElement with matching value could be found --> we are finished
                     */
                    found = true;
                    select.selectByValue(setValue);
                    break;
                }
            }

            if (!found) {
                /*
                 * Fallback: look for a WebElement with a matching visible text
                 */
                for (WebElement option : options) {
                    String visibleText = option.getText();
                    if (StringUtils.equals(visibleText, setValue)) {
                        /*
                         * WebElement with matching value could be found --> we are finished
                         */
                        found = true;
                        select.selectByVisibleText(setValue);
                        break;
                    }
                }
            }

            if (!found) {
                throw new JFunkException(
                        "Could not find a matching option element in " + element + " , By: " + finder.getBy());
            }

        } else if (WebConstants.INPUT.equalsIgnoreCase(element.getTagName())
                && WebConstants.RADIO.equals(element.getAttribute(WebConstants.TYPE))) {

            List<WebElement> elements = finder.findAll();
            for (WebElement webElement : elements) {
                String elVal = webElement.getAttribute(WebConstants.VALUE);
                if (elVal.equals(setValue) && !webElement.isSelected()) {
                    webElement.click();
                }
            }

        } else if (WebConstants.CHECKBOX.equals(element.getAttribute(WebConstants.TYPE))) {

            if (Boolean.valueOf(setValue) && !element.isSelected()
                    || !Boolean.valueOf(setValue) && element.isSelected()) {
                element.click();
            }

        } else {

            if (element.getAttribute("readonly") == null || element.getAttribute("readonly").equals("false")) {
                element.clear();
                element.sendKeys(setValue);
            } else {
                log.warn("Element is invisible or disabled, value cannot be set");
            }

        }
        break;

    case NONE:
        // do nothing
        break;

    default:
        throw new IllegalArgumentException("Unhandled StepMode=" + stepMode);
    }
}

From source file:com.mgmtp.jfunk.web.util.FormInputHandler.java

License:Apache License

private void checkValue(final WebElement element, final String checkValue) {
    String elementValue = element.getTagName().equalsIgnoreCase(WebConstants.SELECT)
            ? new Select(element).getFirstSelectedOption().getAttribute(WebConstants.VALUE)
            : element.getAttribute(WebConstants.VALUE);

    if (WebConstants.INPUT.equalsIgnoreCase(element.getTagName())
            && WebConstants.RADIO.equals(element.getAttribute(WebConstants.TYPE))) {

        List<WebElement> elements = finder.findAll();
        for (WebElement webElement : elements) {
            String elVal = webElement.getAttribute(WebConstants.VALUE);
            if (elVal.equals(checkValue) && !webElement.isSelected()) {
                throw new InvalidValueException(element, checkValue, elVal);
            }//from ww  w.  java 2  s . c  o m
        }

    } else if (WebConstants.CHECKBOX.equals(element.getAttribute(WebConstants.TYPE))) {

        boolean elVal = element.isSelected();
        if (elVal != Boolean.valueOf(checkValue)) {
            throw new InvalidValueException(element, checkValue, String.valueOf(elVal));
        }

    } else {

        if (!Objects.equal(checkValue, elementValue)) {
            throw new InvalidValueException(element, checkValue, elementValue);
        }

    }
}