Example usage for org.openqa.selenium WebElement getTagName

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

Introduction

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

Prototype

String getTagName();

Source Link

Document

Get the tag name of this element.

Usage

From source file:com.hotwire.selenium.desktop.us.billing.onepage.HotelCreditCardFragment.java

License:Open Source License

public HotelCreditCardFragment withSavedState(String state) {
    WebElement displayedElement = null;
    for (WebElement item : this.savedState) {
        if (item.isDisplayed()) {
            displayedElement = item;//from   ww w .  jav  a 2  s .  c  o m
            break;
        }
    }
    if (displayedElement == null) {
        return this;
    }
    ExtendedSelect select = new ExtendedSelect(displayedElement);
    try {
        select.selectByIndex(Integer.parseInt(state));
        return this;
    } catch (NumberFormatException e) {
        // Do nothing. State is non-numeric and continue.
    }
    if (displayedElement != null) {
        if (displayedElement.getTagName().equals("select")) {
            new ExtendedSelect(displayedElement).selectIfVisibleTextStartsWithText(state);
        } else {
            // Default to sendkeys. Most likely element is an input text
            // area.
            displayedElement.sendKeys(state);
        }
    }
    // Else do nothing as this page is doing country specific elements and
    // some countries
    // will not have this element.
    return this;
}

From source file:com.htmlhifive.pitalium.core.selenium.PtlFirefoxDriver.java

License:Apache License

@Override
protected void trimMovePadding(WebElement el, List<BufferedImage> images) {
    LOG.trace("[Trim move element's padding]");
    // firefox?textarea?padding?????????padding?
    if ("textarea".equals(el.getTagName())) {
        for (int i = 0; i < images.size(); i++) {
            images.set(i, trimTargetPadding(el, images.get(i), i, images.size()));
        }//from ww  w .  j  av  a2 s. c  om
    }
}

From source file:com.ibm.sbt.automation.core.environment.TestEnvironment.java

License:Open Source License

/**
 * Dump the specified result page to the trace log
 *//*from  ww  w.  j  av  a2s .  com*/
public void dumpWebElement(WebElement webElement) {
    Trace.log(webElement + " tagName:" + webElement.getTagName() + " text:" + webElement.getText() + " id:"
            + webElement.getAttribute("id") + " displayed:" + webElement.isDisplayed());

    List<WebElement> webElements = webElement.findElements(By.xpath("*"));
    if (webElements.size() > 0) {
        Trace.log("Children size: " + webElements.size());
        for (int i = 0; i < webElements.size(); i++) {
            WebElement nextElement = webElements.get(i);
            Trace.log("[" + i + "]" + nextElement + " tagName:" + webElement.getTagName() + " text:"
                    + nextElement.getText() + " id:" + nextElement.getAttribute("id") + " displayed:"
                    + nextElement.isDisplayed());
        }
    }
}

From source file:com.jaeksoft.searchlib.crawler.web.browser.BrowserDriver.java

License:Open Source License

public WebElement getParent(String tagName, WebElement element) {
    try {/*  w  w  w.  j a  v a2 s. c om*/
        WebElement parent = element.findElement(By.xpath(".."));
        if (parent == null)
            return null;
        if (tagName == null)
            return parent;
        if (tagName.equalsIgnoreCase(parent.getTagName()))
            return parent;
        return getParent(tagName, parent);
    } catch (NoSuchElementException e) {
        Logging.warn(e);
        return null;
    }
}

From source file:com.jaeksoft.searchlib.crawler.web.spider.ClickCapture.java

License:Open Source License

private void locate(BrowserDriver<?> browserDriver) {
    try {//from   w ww. j  ava 2 s. c  o m
        if (CollectionUtils.isEmpty(webElements))
            return;
        for (WebElement webElement : webElements) {
            if ("img".equalsIgnoreCase(webElement.getTagName())) {
                webElement = browserDriver.getParent("a", webElement);
                if (webElement != null)
                    if (locateAimgClickCapture(webElement))
                        return;
            }
            if (locateElement(browserDriver, webElement))
                return;
        }
    } catch (Exception e) {
        Logging.warn(e);
    }
}

From source file:com.liferay.faces.test.selenium.WebDriverMockImpl.java

License:Open Source License

public WebDriverMockImpl(WebElement... webElementsArray) {

    Map<String, WebElement> webElements = new HashMap<String, WebElement>();

    for (WebElement webElement : webElementsArray) {

        String tagName = webElement.getTagName();
        webElements.put(tagName, webElement);
    }/*from   www  .  j av a  2  s  .  c  om*/

    this.webElements = Collections.unmodifiableMap(webElements);
}

From source file:com.liferay.faces.test.showcase.buttonlink.ButtonLinkTester.java

License:Open Source License

private void clickButtonLink(BrowserDriver browserDriver, String xpath) {

    WebElement webElement = browserDriver.findElementByXpath(xpath);
    String tagName = webElement.getTagName();
    String onclick = webElement.getAttribute("onclick");
    browserDriver.clickElement(xpath);/*  w  ww  . j a v  a2  s. c o  m*/

    // If the clicking the button/link will cause the page to reload.
    if (!"button".equals(tagName) || ((onclick != null) && !"".equals(onclick))) {

        browserDriver.waitFor(ExpectedConditions.stalenessOf(webElement));
        waitForShowcasePageReady(browserDriver);
    }
}

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

License:Apache License

/**
 * @throws StepException//w  ww . jav a  2 s.  com
 *             <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  ww w .  j  av a2s . 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   w  w  w  .  ja v a 2s  .  com
        }

    } 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);
        }

    }
}