Example usage for org.openqa.selenium WebElement getAttribute

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

Introduction

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

Prototype

String getAttribute(String name);

Source Link

Document

Get the value of the given attribute of the element.

Usage

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

License:Open Source License

/**
 * Wait the specified interval for the specified web element to be available
 *///w  ww.  j  av a  2  s  .co m
public WebElement waitForElement(final String match, final int secs, final String condition) {
    try {
        return (new WebDriverWait(getPageObject().getWebDriver(), secs))
                .until(new ExpectedCondition<WebElement>() {
                    @Override
                    public WebElement apply(WebDriver webDriver) {
                        failIfPageCrashed(webDriver);
                        webDriver = getPageObject(webDriver).getWebDriver();
                        if (condition.equalsIgnoreCase("id")) {
                            return webDriver.findElement(By.id(match));
                        } else if (condition.equalsIgnoreCase("linkText")) {
                            return webDriver.findElement(By.linkText(match));
                        } else if (condition.equalsIgnoreCase("tagName")) {
                            return webDriver.findElement(By.tagName(match));
                        } else if (condition.equalsIgnoreCase("name")) {
                            return webDriver.findElement(By.name(match));
                        } else if (condition.equalsIgnoreCase("idWithText")) {
                            WebElement element = webDriver.findElement(By.id(match));
                            String text = element.getText();
                            if (StringUtil.isNotEmpty(text)) {
                                return element;
                            }
                            String value = element.getAttribute("value");
                            if (StringUtil.isNotEmpty(value)) {
                                return element;
                            }
                            return null;
                        } else if (condition.equalsIgnoreCase("idWithChild")) {
                            WebElement element = webDriver.findElement(By.id(match));
                            List<WebElement> children = element.findElements(By.xpath("*"));
                            if (!children.isEmpty()) {
                                return element;
                            }
                            return null;
                        } else {
                            return webDriver.findElement(By.name(match));
                        }
                    }
                });
    } catch (Exception e) {
        return null;
    }
}

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 a 2 s  .  co  m
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.ibm.sbt.automation.core.test.BaseSampleFrameworkTest.java

License:Open Source License

public boolean checkIframe(SampleFrameworkResultPage resultPage) {
    WebDriverWait wait = new WebDriverWait(resultPage.getWebDriver(), 20l);
    WebElement iframeBody = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body")));
    String bodyClass = iframeBody.getAttribute("class");

    return bodyClass != null;
}

From source file:com.ibm.sbt.automation.core.test.pageobjects.PanelResultPage.java

License:Open Source License

public String getPhotoUrl() {
    WebElement resultEl = getWebElement();
    WebElement img = resultEl.findElement(By.tagName("img"));
    return (img == null) ? null : img.getAttribute("src");
}

From source file:com.ibm.sbt.test.sample.framework.SampleFrameworkJava.java

License:Open Source License

private boolean checkCodeDivs(SampleFrameworkResultPage resultPage) {
    WebElement jspDiv = resultPage.getJspSnippetDiv();
    String jspContent = jspDiv.getAttribute("innerHTML");
    WebElement ulNav = resultPage.getCodeNav();
    List<WebElement> tabList = ulNav.findElements(By.xpath(".//a"));
    WebDriverWait wait = new WebDriverWait(resultPage.getWebDriver(), 2);
    tabList.get(1).click();/* ww w . ja va 2  s  .  c  o m*/
    boolean docDivDisplayed = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.id(SampleFrameworkResultPage.DOCSNIPPETDIV)))
            .isDisplayed();

    return jspContent != null && docDivDisplayed;
}

From source file:com.ibm.sbt.test.sample.framework.SampleFrameworkJavaScript.java

License:Open Source License

private boolean checkCodeDivs(SampleFrameworkResultPage resultPage) {
    WebElement jsDiv = resultPage.getJsSnippetDiv();
    String jsContent = jsDiv.getAttribute("innerHTML");
    WebElement ulNav = resultPage.getCodeNav();
    List<WebElement> tabList = ulNav.findElements(By.xpath(".//a"));
    WebDriverWait wait = new WebDriverWait(resultPage.getWebDriver(), 5l);
    tabList.get(1).click();/*from   ww  w  . jav  a2  s. com*/
    boolean htmlDivDisplayed = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.id(SampleFrameworkResultPage.HTMLSNIPPETDIV)))
            .isDisplayed();
    tabList.get(2).click();
    boolean cssDivDisplayed = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.id(SampleFrameworkResultPage.CSSSNIPPETDIV)))
            .isDisplayed();
    tabList.get(3).click();
    boolean docDivDisplayed = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.id(SampleFrameworkResultPage.DOCSNIPPETDIV)))
            .isDisplayed();

    return jsContent != null && htmlDivDisplayed && cssDivDisplayed && docDivDisplayed;
}

From source file:com.ibm.watson.movieapp.dialog.fvt.webui.BaseUI.java

License:Open Source License

/**
 * fluentWaitAttrChange - Method to wait for attribute change
 * @param locator - Use .attribute()/* w  w w.  j  av a  2  s  .  c  o  m*/
 * @see fluentWaitPresent(final WebElement element)
 * @return
 */
public boolean fluentWaitAttrChange(final String locator, final String attrib, final String value) {

    logger.info("INFO: Fluentwait for " + locator + " " + attrib + " to change to " + value);

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(TIMEOUT, TimeUnit.SECONDS)
            .pollingEvery(POLL, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

    boolean foo = wait.until(new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            WebElement element = findElement(locator);
            String attribValue = element.getAttribute(attrib);
            logger.info("INFO: Current Attribute value: " + attribValue);
            return attribValue.contains(value);
        }
    });
    return foo;
}

From source file:com.induscorp.prime.testing.ui.core.objects.DOMObjectValidator.java

License:Open Source License

/**
 * Returns the attribute value of the first element.
 * //  ww  w .  jav a2  s .c  om
 * @param attributeName
 * @param numRetries
 * @return
 */
public String getAttributeValue(String attributeName, int numRetries) {
    try {
        WebElement webElem = findElement(numRetries);
        return webElem.getAttribute(attributeName);
    } catch (Throwable th) {
        Assert.fail("Failed to get attribute (name='" + attributeName + "') value for element '"
                + domObject.getDisplayName() + "'.", th);
    }
    return null;
}

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

License:Open Source License

@Override
public void validateName(String expectedName, TextValidationMechanism validationMechanism, int numRetries) {
    WebElement webElem = domObjValidator.findElement(numRetries);
    String actualValue = webElem.getAttribute("value");

    validateTextValue(actualValue, expectedName, validationMechanism);
}

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

License:Open Source License

@Override
public void validateDisabled(int numRetries) {
    List<WebElement> webElems = findElements(numRetries);
    List<String> enabledItems = new LinkedList<String>();
    for (WebElement elem : webElems) {
        if (elem.isEnabled()) {
            enabledItems.add(elem.getAttribute("value"));
        }//from  w  w  w. jav a 2 s . com
    }
    if (enabledItems.size() > 0) {
        Assert.fail("Radio button group '" + uiObject.getDisplayName()
                + "' is not disabled. It has the following enabled items: " + enabledItems);
    }
}