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.consol.citrus.selenium.action.ExtractAction.java

License:Apache License

@Override
public void doExecute(TestContext context) {
    super.doExecute(context);

    if (elements != null) {
        for (By by : elements.keySet()) {
            String variable = elements.get(by).getVariable();
            String attribute = elements.get(by).getAttribute();
            String value = null;/*from   w  w w  . j  a v a  2 s .com*/
            logger.info("extracting the element by <{}>", by);

            WebElement element = webClient.findElement(by);

            // check if the by has attribute selection
            if (by instanceof By.ByXPath) {
                // TODO: refactor the following
                String xpathExpression = by.toString().replaceAll("By.xpath:\\s*", "");
                if (xpathExpression.contains("/@")) {
                    // we found attribute selection.
                    String[] parts = xpathExpression.split("/@");
                    attribute = parts[1];
                    String newXpathExpresseion = parts[0];
                    element = webClient.findElement(By.xpath(newXpathExpresseion));
                }
            }

            if (element != null) {
                if (StringUtils.hasText(attribute)) {
                    value = element.getAttribute(attribute);
                } else {
                    value = element.getAttribute("value");
                    if (StringUtils.isEmpty(value)) {
                        value = element.getText();
                    }
                }
            }
            context.setVariable(variable, value);
        }
    }

    if (StringUtils.hasText(pageName)) {
        WebPage pageObj;
        try {
            logger.debug("Initializing the page object {}", pageName);
            Class pageClass = Class.forName(pageName);
            pageObj = webClient.createPage(pageClass);
            logger.debug("page object {} is succesfully created.", pageName);
            webClient.verifyPage(pageObj);
            for (String pageAction : pageActions.keySet()) {
                String variable = pageActions.get(pageAction);
                logger.debug("running the action {} on the page object {}", pageAction, pageName);
                logger.info("Invoking method '" + pageAction + "' on instance '" + pageClass + "'");
                Method actionMethod = pageObj.getClass().getMethod("get" + pageAction);
                String value = (String) actionMethod.invoke(pageObj);
                context.setVariable(variable, value);
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            throw new CitrusRuntimeException(ex);
        }
    }
}

From source file:com.consol.citrus.selenium.action.ValidateAction.java

License:Apache License

@Override
public void doExecute(TestContext context) {
    super.doExecute(context);
    if (validations != null) {
        for (By by : validations.keySet()) {
            String controlValue = validations.get(by);
            if (ValidationMatcherUtils.isValidationMatcherExpression(controlValue)) {
                String actualValue = null;
                WebElement element = webClient.findElement(by);

                String attribute = null;
                // check if the by has attribute selection
                if (by instanceof By.ByXPath) {
                    // TODO: refactor the following
                    String xpathExpression = by.toString().replaceAll("By.xpath:\\s*", "");
                    if (xpathExpression.contains("/@")) {
                        // we found attribute selection.
                        String[] parts = xpathExpression.split("/@");
                        attribute = parts[1];
                        String newXpathExpresseion = parts[0];
                        element = webClient.findElement(By.xpath(newXpathExpresseion));
                    }/*w w w. j  ava 2  s .c  om*/
                }

                if (element != null) {
                    if (attribute != null) {
                        actualValue = element.getAttribute(attribute);
                    } else {
                        actualValue = element.getAttribute("value");
                        if (actualValue == null || actualValue.isEmpty()) {
                            actualValue = element.getText();
                        }
                    }
                }
                ValidationMatcherUtils.resolveValidationMatcher(by.toString(), actualValue, controlValue,
                        context);
            } else {
                webClient.validate(by, controlValue, null);
            }
        }
    }

    if (StringUtils.hasText(pageName)) {
        WebPage pageObj;
        try {
            logger.debug("Initializing the page object {}", pageName);
            Class pageClass = Class.forName(pageName);
            pageObj = webClient.createPage(pageClass);
            logger.debug("page object {} is succesfully created.", pageName);
            webClient.verifyPage(pageObj);
            for (String pageAction : pageValidations.keySet()) {
                String controlValue = pageValidations.get(pageAction);
                logger.debug("running the action {} on the page object {}", pageAction, pageName);
                logger.info("Invoking method '" + pageAction + "' on instance '" + pageClass + "'");
                Method actionMethod = pageObj.getClass().getMethod("get" + pageAction);
                String actualValue = (String) actionMethod.invoke(pageObj);
                if (ValidationMatcherUtils.isValidationMatcherExpression(controlValue)) {
                    ValidationMatcherUtils.resolveValidationMatcher(pageAction, actualValue, controlValue,
                            context);
                } else {
                    webClient.validate(actualValue, controlValue, null);
                }
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            throw new CitrusRuntimeException(ex);
        }
    }
}

From source file:com.consol.citrus.selenium.actions.DropDownSelectAction.java

License:Apache License

private boolean isSameValue(WebElement option, String value) {
    if (StringUtils.hasText(option.getText())) {
        return value.equals(option.getText());
    } else {//from w w w  .  ja  va 2 s .c om
        return value.equals(option.getAttribute("value"));
    }
}

From source file:com.consol.citrus.selenium.actions.FindElementAction.java

License:Apache License

/**
 * Validates found web element with expected content.
 * @param element/*from   w  ww  .  j  a v a2  s .  c o m*/
 * @param browser
 * @param context
 */
protected void validate(WebElement element, SeleniumBrowser browser, TestContext context) {
    validateElementProperty("tag-name", tagName, element.getTagName(), context);
    validateElementProperty("text", text, element.getText(), context);

    Assert.isTrue(displayed == element.isDisplayed(),
            String.format(
                    "Selenium web element validation failed, "
                            + "property 'displayed' expected '%s', but was '%s'",
                    displayed, element.isDisplayed()));
    Assert.isTrue(enabled == element.isEnabled(), String.format(
            "Selenium web element validation failed, " + "property 'enabled' expected '%s', but was '%s'",
            enabled, element.isEnabled()));

    for (Map.Entry<String, String> attributeEntry : attributes.entrySet()) {
        validateElementProperty(String.format("attribute '%s'", attributeEntry.getKey()),
                attributeEntry.getValue(), element.getAttribute(attributeEntry.getKey()), context);
    }

    for (Map.Entry<String, String> styleEntry : styles.entrySet()) {
        validateElementProperty(String.format("css style '%s'", styleEntry.getKey()), styleEntry.getValue(),
                element.getCssValue(styleEntry.getKey()), context);
    }
}

From source file:com.continuuity.test.page.CreatePage.ClustertemplatesInstancePage.java

License:Apache License

public ClusterDefaults getClusterDefaults() throws JSONException {
    Select select = new Select(globalDriver.findElement(PROVIDER));
    String provider = select.getFirstSelectedOption().getAttribute(Constants.TEXT);

    select = new Select(globalDriver.findElement(HARDWARETYPE));
    String hardwaretype = select.getFirstSelectedOption().getAttribute(Constants.TEXT);

    select = new Select(globalDriver.findElement(IMAGETYPE));
    String imagetype = select.getFirstSelectedOption().getAttribute(Constants.TEXT);

    String dnsSuffix = globalDriver.findElement(DNS_SUFFIX).getAttribute(Constants.VALUE);

    String config = globalDriver.findElement(CONFIG).getAttribute(Constants.VALUE);
    JsonObject configJson = gson.fromJson(config, JsonObject.class);

    Set<String> services = Sets.newHashSet();
    for (WebElement element : globalDriver.findElement(DEFAULT_SERVICE).findElements(DEFAULT_SERVIE_ENTRY)) {
        services.add(element.getAttribute(Constants.INNER_HTML));
    }/*from   ww w. j a v  a 2  s. co m*/

    return new ClusterDefaults(services, provider, hardwaretype, imagetype, dnsSuffix, configJson);
}

From source file:com.continuuity.test.page.CreatePage.ClustertemplatesInstancePage.java

License:Apache License

public Compatibilities getCompatibility() {
    Set<String> services = Sets.newHashSet();
    for (WebElement element : globalDriver.findElement(ALLOWED_SERVICE).findElements(DEFAULT_SERVIE_ENTRY)) {
        services.add(element.getAttribute(Constants.INNER_HTML));
    }//from w  w w.  j  a  v  a2  s.c om
    Set<String> hardwaretypes = Sets.newHashSet();
    for (WebElement element : globalDriver.findElement(ALLOWED_HARDWARETYPE)
            .findElements(DEFAULT_SERVIE_ENTRY)) {
        hardwaretypes.add(element.getAttribute(Constants.INNER_HTML));
    }
    Set<String> imagetypes = Sets.newHashSet();
    for (WebElement element : globalDriver.findElement(ALLOWED_IMAGETYPES).findElements(DEFAULT_SERVIE_ENTRY)) {
        imagetypes.add(element.getAttribute(Constants.INNER_HTML));
    }
    return new Compatibilities(hardwaretypes, imagetypes, services);
}

From source file:com.continuuity.test.page.CreatePage.ClustertemplatesInstancePage.java

License:Apache License

private Set<Set<String>> getLayoutConstraint(By group, By entry) {
    Set<Set<String>> constraints = Sets.newHashSet();
    for (WebElement groupElement : globalDriver.findElements(group)) {
        Set<String> constraint = Sets.newHashSet();
        for (WebElement element : groupElement.findElements(entry)) {
            constraint.add(element.getAttribute(Constants.INNER_HTML));
        }//from w w w.  ja  v  a  2s .c om
        if (!constraint.isEmpty()) {
            constraints.add(constraint);
        }
    }
    return constraints;
}

From source file:com.continuuity.test.page.CreatePage.ClustertemplatesInstancePage.java

License:Apache License

public Set<String> getRequiredTypes(WebElement element, By type) {
    Set<String> types = Sets.newHashSet();
    for (WebElement select : new Select(element.findElement(type)).getAllSelectedOptions()) {
        String text = select.getAttribute(Constants.TEXT);
        if (text != null && !text.isEmpty()) {
            types.add(select.getAttribute(Constants.TEXT));
        }//from   ww w  .ja v a 2 s.com
    }
    return types;
}

From source file:com.continuuity.test.page.CreatePage.ServicesInstancePage.java

License:Apache License

public Set<String> getDependsOn() {
    Set<String> dependsOn = Sets.newHashSet();
    for (WebElement element : globalDriver.findElements(SERVICE_ENTRIES).get(4).findElements(SERVICE_NAME)) {
        dependsOn.add(element.getAttribute(Constants.INNER_HTML));
    }//from www.  j  av a 2  s.  c  om
    return dependsOn;
}

From source file:com.continuuity.test.page.CreatePage.ServicesInstancePage.java

License:Apache License

public Map<ProvisionerAction, ServiceAction> getProvisionerActions() {
    Map<ProvisionerAction, ServiceAction> provisionerActions = Maps.newHashMap();
    List<WebElement> actions = globalDriver.findElements(By.cssSelector(".action-entry"));
    for (WebElement actionEntry : actions) {
        Select actionSelect = new Select(actionEntry.findElement(CATEGORY));
        ProvisionerAction category = ProvisionerAction
                .valueOf(actionSelect.getFirstSelectedOption().getText().toUpperCase());
        Select actionType = new Select(actionEntry.findElement(TYPE));
        String type = actionType.getFirstSelectedOption().getText();
        List<WebElement> automatorDetails = actionEntry.findElements(By.cssSelector(".automator-field input"));
        Map<String, String> fields = Maps.newHashMap();
        for (WebElement automator : automatorDetails) {
            String value = automator.getAttribute(Constants.VALUE);
            if (!value.equals("")) {
                fields.put(automator.getAttribute("name"), automator.getAttribute(Constants.VALUE));
            }/*from  www. j ava  2 s. c  o  m*/
        }
        provisionerActions.put(category, new ServiceAction(type, fields));
    }
    return provisionerActions;
}