Example usage for org.openqa.selenium WebElement isDisplayed

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

Introduction

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

Prototype

boolean isDisplayed();

Source Link

Document

Is this element displayed or not?

Usage

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 {/* w w w . j  a  v a  2 s.c o  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//ww  w  . ja  v  a2 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 a  va  2  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 a2  s . c  om*/
 *             <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.WebElementFinder.java

License:Apache License

private boolean checkElementForList(final WebElement element) {
    if (enabled != null) {
        if (enabled != element.isEnabled()) {
            return false;
        }/* w  w  w.  j  a v  a2  s. c om*/
    }
    if (displayed != null) {
        if (displayed != element.isDisplayed()) {
            return false;
        }
    }
    if (selected != null) {
        if (selected != element.isSelected()) {
            return false;
        }
    }
    return true;
}

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

License:Apache License

private void checkElement(final WebElement element) {
    if (enabled != null) {
        if (enabled) {
            checkElementState(element, element.isEnabled(),
                    "Element '%s' was expected to be enabled but is not.");
        } else {//from w ww  . j a  v  a2  s  .  c o  m
            checkElementState(element, !element.isEnabled(),
                    "Element '%s' was expected to not be enabled but is.");
        }
    }
    if (displayed != null) {
        if (displayed) {
            checkElementState(element, element.isDisplayed(),
                    "Element '%s' was expected to be displayed but is not.");
        } else {
            checkElementState(element, !element.isDisplayed(),
                    "Element '%s' was expected to not be displayed but is.");
        }
    }
    if (selected != null) {
        if (selected) {
            checkElementState(element, element.isSelected(),
                    "Element '%s' was expected to be selected but is not.");
        } else {
            checkElementState(element, !element.isSelected(),
                    "Element '%s' was expected to not be selected but is.");
        }
    }
}

From source file:com.moodle.testmanager.FilesElements.java

License:GNU General Public License

/**
 * Clicks a two state button whether it is switched on or not.
 * @param className//from  w w w  .java 2  s  .c o m
 */
public void stickyButton(String className) {
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    boolean itemVisible = false;
    try {
        WebElement element = driver.findElementByClassName(className);
        itemVisible = element.isDisplayed();
    } catch (NoSuchElementException ex) {
    }
    if (itemVisible) {
        fileManButton(className);
    } else {
        fileManButton(className + " " + "checked");
    }
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

From source file:com.moodle.testmanager.FilesElements.java

License:GNU General Public License

/**
 * Enters a given value in a text field in the file picker. The field is located by the field label text and the location strategy used here
 * will work regardless of whether the field label is above the field or to the left. May not work with rtl sites
 * @param fieldLabel The field label.//from   w  w  w.  jav a 2s  .c  o  m
 * @param keysToSend The value that you aould like to enter in the field.
 */
public void textEntryField(String fieldLabel, CharSequence keysToSend) {
    //TODO RTL sites
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    boolean itemVisible = false;
    try {
        WebElement element = driver
                .findElementByXPath(locLabelToLeftPrefix + fieldLabel + locLabelToLeftSuffix);
        itemVisible = element.isDisplayed();
    } catch (NoSuchElementException ex) {
    }
    if (itemVisible) {
        driver.findElementByXPath(locLabelToLeftPrefix + fieldLabel + locLabelToLeftSuffix)
                .sendKeys(keysToSend);
    } else {
        driver.findElementByXPath(locLabelAbovePrefix + fieldLabel + locLabelAboveSuffix).sendKeys(keysToSend);
    }
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

From source file:com.moodle.testmanager.FormActions.java

License:GNU General Public License

/**
 * Selects an action from a dropdown field on a form by ID. If Javascript is disabled this selects a value and clicks on the
 * Go button.//from w  w  w . j a va 2 s  . c  om
 * @param fieldID The id of the select tag 
 * @param itemToSelect The literal text of the item to be selected.
 * @param timeToWait The time to wait for the Go button to appear onscreen.
 * @param cssSelector The CSS Selector of the Go button.
 */
public void selectDropdownItemByIDHandlesJS(String fieldID, String itemToSelect, String cssSelector,
        int timeToWait) {
    boolean itemVisible = false;
    try {
        driver.manage().timeouts().implicitlyWait(timeToWait, TimeUnit.SECONDS);
        WebElement onscreenElement = driver.findElement(By.cssSelector(cssSelector));
        itemVisible = onscreenElement.isDisplayed();
    } catch (NoSuchElementException ex) {
    }
    if (itemVisible) {
        Select dropdown = new Select(driver.findElement(By.id(fieldID)));
        dropdown.selectByVisibleText(itemToSelect);
        driver.findElement(By.cssSelector(cssSelector)).click();
    } else {
        Select dropdown = new Select(driver.findElement(By.id(fieldID)));
        dropdown.selectByVisibleText(itemToSelect);
    }
}

From source file:com.moodle.testmanager.FormActions.java

License:GNU General Public License

/**
 * Selects an option from a dropdown field on a form by XPath. If Javascript is disabled this selects a value and clicks on the
 * Go button./*from   w  w  w.  j av a2 s . c  o m*/
 * @param xpathField A valid XPath locator to the dropdown field.
 * @param itemToSelect The literal text of the item to be selected.
 * @param xpathGo A valid XPath locator to the Go button.
 * @param timeToWait The time to wait for the Go button to appear onscreen.
 */
public void selectDropdownItemByXPathHandlesJS(String xpathField, String itemToSelect, String xpathGo,
        int timeToWait) {
    boolean itemVisible = false;
    try {
        driver.manage().timeouts().implicitlyWait(timeToWait, TimeUnit.SECONDS);
        WebElement onscreenElement = driver.findElement(By.xpath(xpathGo));
        itemVisible = onscreenElement.isDisplayed();
    } catch (NoSuchElementException ex) {
    }
    if (itemVisible) {
        Select activityDropDown = new Select(driver.findElement(By.xpath(xpathField)));
        activityDropDown.selectByVisibleText(itemToSelect);
        driver.findElement(By.xpath(xpathGo)).click();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    } else {
        Select activityDropDown = new Select(driver.findElement(By.xpath(xpathField)));
        activityDropDown.selectByVisibleText(itemToSelect);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
}