Example usage for org.openqa.selenium WebElement isEnabled

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

Introduction

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

Prototype

boolean isEnabled();

Source Link

Document

Is the element currently enabled or not?

Usage

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

License:Open Source License

@Override
public boolean isEditable(String locator) {
    WebElement webElement = getWebElement(locator);

    return webElement.isEnabled();
}

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

License:Open Source License

@Override
public void type(String locator, String value) {
    WebElement webElement = getWebElement(locator);

    if (!webElement.isEnabled()) {
        return;//from w  ww . j ava  2s.  c om
    }

    webElement.clear();

    typeKeys(locator, value);
}

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

License:Open Source License

@Override
public void typeKeys(String locator, String value) {
    WebElement webElement = getWebElement(locator);

    if (!webElement.isEnabled()) {
        return;//from  w ww. j  a  v  a 2 s.  c om
    }

    if (value.contains("line-number=")) {
        value = value.replaceAll("line-number=\"\\d+\"", "");
    }

    int i = 0;

    Set<Integer> specialCharIndexes = getSpecialCharIndexes(value);

    for (int specialCharIndex : specialCharIndexes) {
        webElement.sendKeys(value.substring(i, specialCharIndex));

        String specialChar = String.valueOf(value.charAt(specialCharIndex));

        if (specialChar.equals("-")) {
            webElement.sendKeys(Keys.SUBTRACT);
        } else if (specialChar.equals("\t")) {
            webElement.sendKeys(Keys.TAB);
        } else {
            webElement.sendKeys(Keys.SHIFT, _keysSpecialChars.get(specialChar));
        }

        i = specialCharIndex + 1;
    }

    webElement.sendKeys(value.substring(i, value.length()));
}

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

License:Open Source License

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

    if (!webElement.isEnabled()) {
        return;/* w w w .  j a  v  a  2 s  .com*/
    }

    webElement.clear();

    webElement.sendKeys(value);
}

From source file:com.liferay.faces.portal.test.showcase.inputrichtext.InputRichTextTester.java

License:Open Source License

private static boolean isCheckboxDisplayedAndEnabled(BrowserDriver browserDriver, String checkboxType) {

    String checkboxXpath = "//input[contains(@id,':{0}Checkbox')]".replace("{0}", checkboxType);
    WebElement checkbox = null;
    List<WebElement> webElements = browserDriver.findElementsByXpath(checkboxXpath);

    if (!webElements.isEmpty()) {
        checkbox = webElements.get(0);/*  w w w . j  a  v a2 s  . co m*/
    }

    return (checkbox != null) && checkbox.isDisplayed() && checkbox.isEnabled();
}

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

License:Apache License

/**
 * @throws StepException// ww w .j a  v  a  2  s  . c o m
 *             if element specified by {@link By} object in the constructor cannot be found
 */
@Override
public void execute() throws StepException {
    if (mustExist) {
        log.info("Checking if " + this + " is present");
    } else {
        log.info("Checking that " + this + " does not exist");
    }

    List<WebElement> webElements = getWebDriver().findElements(by);
    if (webElements.isEmpty()) {
        if (mustExist) {
            throw new StepException("Could not find any matching element");
        }
    } else {
        // webElements is not empty
        if (!mustExist) {
            throw new StepException("Matching element could be found although mustExist=false");
        }
        /*
         * 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 element : webElements) {
            if (element.isDisplayed()) {
                if (element.isEnabled()) {
                    if (!mustExist) {
                        throw new StepException("Matching element could be found although mustExist=false");
                    }
                    return;
                }
            }
        }
        throw new StepException("All elements matching by=" + by + " were either invisible or disabled");
    }
}

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

License:Apache License

private void doTest(final boolean elementFound, final boolean mustExist) {
    By by = By.id("foo");
    WebDriver webDriver = mock(WebDriver.class);
    if (elementFound) {
        WebElement webElement = mock(WebElement.class);
        when(webElement.isEnabled()).thenReturn(true);
        when(webElement.isDisplayed()).thenReturn(true);
        when(webDriver.findElements(by)).thenReturn(asList(webElement));
    } else {// ww  w  .j a v a 2  s.  co m
        when(webDriver.findElements(by)).thenReturn(ImmutableList.<WebElement>of());
    }
    Check4Element step = new Check4Element(by, mustExist);
    step.setWebDriver(webDriver);
    step.execute();
}

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

License:Apache License

/**
 * @throws StepException/*from   ww w  .j  a  v a 2  s . c  om*/
 *             if element specified by {@link By} object in the constructor cannot be found or the regex does not match
 */
@Override
public void execute() throws StepException {
    log.info("Executing: " + this);

    List<WebElement> webElements = getWebDriver().findElements(by);
    if (webElements.isEmpty()) {
        throw new StepException("Could not find any matching element");
    }

    /*
     * 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 element : webElements) {
        if (element.isDisplayed()) {
            if (element.isEnabled()) {
                String actualValue = element.getText();
                Matcher m = pattern.matcher(actualValue);
                if (!m.matches()) {
                    throw new StepException(
                            "Found a matching element, but the regex '" + pattern + "'does not match.");
                }
                log.debug("Found matching element");
                return;
            }
        }
    }

    throw new StepException("All elements matching by=" + by + " were either invisible or disabled");
}

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

License:Apache License

@Override
public void execute() {
    // wait for rendering the table
    final WebDriverWait tableWait = new WebDriverWait(getWebDriver(), WebConstants.DEFAULT_TIMEOUT);
    tableWait.until(new Function<WebDriver, Boolean>() {

        @Override/*from w  w  w .  j av a2 s .  c o  m*/
        public Boolean apply(final WebDriver input) {
            final WebElement el = input.findElement(tableBy);
            if (el != null && el.isDisplayed() && el.isEnabled()) {
                if (el.getText().contains(value) && isPresent) {
                    return true;
                }
                if (!el.getText().contains(value) && !isPresent) {
                    return true;
                }
            }
            return false;
        }
    });

    final List<WebElement> column = getWebDriver().findElements(columnBy);
    log.debug("Checking column values with flag isPresent = " + isPresent);
    for (WebElement columnElement : column) {
        log.debug("Checking text: " + columnElement.getText());
        if (value.equals(columnElement.getText())) {
            if (isPresent) {
                log.info("Value '" + value + "' was found in HTML column specified by '" + columnBy);
                return;
            }
            throw new ValidationException(
                    "Value '" + value + "' was found in HTML column specified by '" + columnBy);
        }
    }

    if (isPresent) {
        throw new ValidationException(
                "Value '" + value + "' was not found in HTML column specified by '" + columnBy);
    }
    log.info("Value '" + value + "' was not found in HTML column specified by '" + columnBy);
}

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

License:Apache License

/**
 * @throws StepException//from ww  w  .j a v a  2 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);
    }
}