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:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.primefaces.PrimefacesGrid.java

License:Open Source License

@Override
public void tableSelectClick(String value, String l, String c, Element element) {
    String xpathTabela = preparaXPath(element, l, c);
    xpathTabela = xpathTabela.concat("//label");
    WebElement myElement = (WebElement) ((WebDriver) runner.getDriver()).findElement(By.xpath(xpathTabela));
    String selectId = myElement.getAttribute("id");
    selectId = selectId.substring(0, selectId.length() - 6);
    myElement.click();/*from  w w  w. ja  va 2  s  .  c om*/

    try {
        Sleeper.SYSTEM_SLEEPER.sleep(new Duration(2, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    WebElement myElement2 = (WebElement) ((WebDriver) runner.getDriver()).findElement(
            By.xpath("//div[@id='" + selectId.toString() + "_panel']/div/ul/li[text()='" + value + "']"));
    myElement2.click();
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebAutoComplete.java

License:Open Source License

/**
 * Mtodo generalizado para selecionar o valor da lista
 * //  w  w  w. j  a v  a  2 s  .co m
 * @param value Valor a ser informado no campo de texto do autocomplete
 */
protected void select(String value) {

    // Aguarda o primeiro elemento ser clicvel
    waitElement(0);

    List<WebElement> elements = getElements();
    if (elements.size() == 1 && getElementMap().locator().length == 1) {
        throw new BehaveException(
                message.getString("exception-autocomplete-missing-elements", "locator", "@ElementMap"));
    }

    WebElement element = elements.get(0);

    if (element.getTagName().equals("input") && element.getAttribute("type").equals("text")) {
        // Preenche o valor do Autocomplete
        element.clear();
        element.sendKeys(value);
    }

    /*
     *  Tempo do efeito de abertura das opes
     *  
     *  Obs: Utilizada API de Reflection para retrocompatibilidade
     *      com verses anteriores do DBehave (1.3.0 por exemplo) 
     */
    try {
        Method waitElementOnlyVisible = getClass().getMethod("waitElementOnlyVisible",
                new Class[] { Integer.class });
        try {
            waitElementOnlyVisible.invoke(this, new Object[] { 1 });
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (NoSuchMethodException e) {
        waitElement(1);
    }

    if (this.selectValue != null) {
        value = this.selectValue;
    }

    selectOnList(elements.get(1), value);
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebBase.java

License:Open Source License

public void isVisibleDisabled() {
    List<WebElement> elementsFound = getElements();

    if (elementsFound.size() > 0) {
        WebElement e = elementsFound.get(0);

        // Tem que estar Visvel e Desabilitado, se estiver invisvel OU
        // habilitado lana a exception
        if (e.getTagName().toLowerCase().equals("input") || e.getTagName().toLowerCase().equals("select")
                || e.getTagName().toLowerCase().equals("a")) {

            // Verifica tambm se tem o atributo READONLY no elemento
            String readonlyAttribute = e.getAttribute("readonly");
            String disabledAttribute = e.getAttribute("disabled");

            // SE no estiver visivel OU (no possuir o attr DISABLED E no
            // possuir o attr READONLY) ENTO de erro!
            if (!e.isDisplayed() || (disabledAttribute == null && readonlyAttribute == null)) {
                throw new BehaveException(message.getString("exception-element-not-displayed-or-enabled",
                        getElementMap().name()));
            }/*from ww  w. j  ava 2s  . c  o m*/
        } else {
            // Faz a verificao se esta desabilitado por meio das classes
            // de css para os casos de combo estilo Primefaces e Richfaces
            String classes = e.getAttribute("class");
            if (!e.isDisplayed() || !classes.contains("disabled")) {
                throw new BehaveException(message.getString("exception-element-not-displayed-or-enabled",
                        getElementMap().name()));
            }
        }

    } else {
        throw new BehaveException(message.getString("exception-element-not-found", getElementMap().name()));
    }
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebImage.java

License:Open Source License

/**
 * {@inheritDoc}/*from w w w  .j av  a  2s  .  c  o  m*/
 */
@Override
public void checkSource(String src) {

    waitElement(0);

    List<WebElement> elements = getElements();
    WebElement element = elements.get(0);

    if (element.getTagName().equals("img")) {
        if (!element.getAttribute("src").contains(src)) {
            throw new BehaveException(message.getString("exception-attribute-not-contains-value", "src", src,
                    getElementMap().name()));
        }
    } else {
        throw new BehaveException(message.getString("element-is-not-type", getElementMap().name(), "image"));
    }

}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebImage.java

License:Open Source License

/**
 * {@inheritDoc}//  ww w.j  av a  2 s  .c o  m
 */
@Override
public void checkAttributes(HashMap<String, String> attrs) {
    waitElement(0);

    List<WebElement> elements = getElements();
    WebElement element = elements.get(0);

    for (String attrName : attrs.keySet()) {
        if (!element.getAttribute(attrName).contains(attrs.get(attrName))) {
            throw new BehaveException(message.getString("exception-attribute-not-contains-value", attrName,
                    attrs.get(attrName), getElementMap().name()));
        }
    }

}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebSelect.java

License:Open Source License

/**
 * {@inheritDoc}/* w w w  .j  ava2 s .c  o m*/
 */
@Override
public String getText() {
    // Fazer tratamento para SELECT normal e PrimeFaces
    List<WebElement> elements = getElements();
    if (elements.get(0).getTagName().equals("select")) {
        org.openqa.selenium.support.ui.Select lSelect = new org.openqa.selenium.support.ui.Select(
                elements.get(0));
        return lSelect.getFirstSelectedOption().getText();
    } else {
        WebElement element = elements.get(0);
        if (element.getAttribute("class").contains("ui-selectonemenu")
                && !element.getAttribute("class").contains("ui-selectonemenu-label")) {
            org.openqa.selenium.support.ui.Select lSelectInnerElement = new org.openqa.selenium.support.ui.Select(
                    element.findElement(By.tagName("select")));
            return lSelectInnerElement.getFirstSelectedOption().getAttribute("innerHTML");
        } else {
            return element.getText();
        }
    }
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.ui.form.SeleniumSelectField.java

License:Apache License

@Override
public final List<Option> getOptions() {

    SeleniumOption option;/*  www . ja v a2s .c o  m*/
    reloadElement();
    List<WebElement> webElements = getElement().findElements(By.tagName("option"));
    for (WebElement element : webElements) {
        option = new SeleniumOption();
        option.setIndex(Integer.valueOf(element.getAttribute("index")));
        option.setValue(element.getAttribute("value"));
        option.setText(element.getText());
        option.webElement = element;
        options.add(option);
    }
    return this.options;
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.ui.SeleniumComponent.java

License:Apache License

@Override
public void loadByAttribute(final String tagName, final String attributeName, final String value) {

    this.locator = new Locator(tagName, attributeName, value);

    if (this.isDisplayed) {
        WebDriverWait wait = new WebDriverWait(getSeleniumWebDriver(), TIMEOUT);
        ExpectedCondition<Boolean> resultsAreDisplayed = new ExpectedCondition<Boolean>() {

            public Boolean apply(WebDriver arg0) {
                List<WebElement> elements = getSeleniumWebDriver().findElements(By.tagName(tagName));
                for (WebElement el : elements) {
                    if ((el.getAttribute(attributeName) != null)
                            && (el.getAttribute(attributeName).equalsIgnoreCase(value))) {
                        setAttribute(attributeName, value);
                        setElement(el);//from w w  w .ja  v  a2  s  .co  m
                        return true;
                    }
                }
                return false;
            }

        };
        wait.until(resultsAreDisplayed);
    }
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.ui.SeleniumComponent.java

License:Apache License

@Override
public <T extends Component, Y extends T> List<T> loadByAttribute(Class<Y> type, final String IdFather,
        final String tagName, final String attributeName, final String value) {

    List<T> components = new ArrayList<T>();

    this.locator = new Locator(tagName, attributeName, value);

    if (this.isDisplayed) {
        WebDriverWait wait = new WebDriverWait(getSeleniumWebDriver(), TIMEOUT);

        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName(tagName)));
        List<WebElement> elements;

        if (IdFather == null) {
            elements = getSeleniumWebDriver().findElements(By.tagName(tagName));
        } else {//  w w w  .jav a2 s . c om
            elements = getSeleniumWebDriver().findElement(By.id(IdFather)).findElements(By.tagName(tagName));
        }

        for (WebElement el : elements) {
            if ((el.getAttribute(attributeName) != null)
                    && (el.getAttribute(attributeName).equalsIgnoreCase(value))) {
                T sc = null;

                try {
                    // Use of reflection for instantiate sc 
                    // this equivalent the get an instance of Builder.uiComponentBuilderInstance()
                    sc = (T) type.getDeclaredConstructor(WebDriver.class).newInstance(getSeleniumWebDriver());

                } catch (Exception e) {
                    e.printStackTrace();
                }

                ((SeleniumComponent) sc).setAttribute(attributeName, value);
                ((SeleniumComponent) sc).setElement(el);
                components.add(sc);
            }
        }
    }
    return components;
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.WebElementAdapter.java

License:Apache License

@Override
public String getAttribute(final String name) {
    return new StaleExceptionResolver<String>() {

        @Override//from  w w w .  j av a2  s .com
        public String execute(WebElement element) {
            return element.getAttribute(name);
        }
    }.waitForElement();
}