Example usage for org.openqa.selenium WebElement isSelected

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

Introduction

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

Prototype

boolean isSelected();

Source Link

Document

Determine whether or not this element is selected or not.

Usage

From source file:com.qulix.ft.teachingSite.Conditions.ExpectedConditions.java

License:Apache License

public static ExpectedCondition<Boolean> elementSelectionStateToBe(final By locator, final boolean selected) {
    return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver from) {
            try {
                WebElement element = from.findElement(locator);
                return element.isSelected() == selected;
            } catch (StaleElementReferenceException e) {
                return null;
            }//from   w  ww .  j  av a2s. c  o  m
        }

        @Override
        public String toString() {
            return String.format("element found by %s to %sbe selected", locator, (selected ? "" : "not "));
        }
    };
}

From source file:com.redskyit.scriptDriver.RunTests.java

License:MIT License

private void info(WebElement element, String selector, boolean verify) throws Exception {
    do {/*from  www  .  j av a 2  s .c  o m*/
        try {
            Point loc = element.getLocation();
            Dimension size = element.getSize();
            String tag = element.getTagName();
            System.out
                    .print(null == selector ? "test-id \"" + element.getAttribute("test-id") + "\"" : selector);
            System.out.print(" info");
            System.out.print(" tag " + tag);
            System.out.print((element.isDisplayed() ? "" : " not") + " displayed");
            System.out.print(" at " + loc.x + "," + loc.y);
            System.out.print(" size " + size.width + "," + size.height);
            System.out.print((element.isEnabled() ? "" : " not") + " enabled");
            System.out.print((element.isSelected() ? "" : " not") + " selected");
            if (tag.equals("input") || tag.equals("select")) {
                System.out.print(" check \"" + element.getAttribute("value") + "\"");
            } else {
                String text = tag.equals("textarea") ? element.getAttribute("value") : element.getText();
                if (text.indexOf('\n') != -1) {
                    CRC32 crc = new CRC32();
                    crc.update(text.getBytes());
                    System.out.print(" checksum \"crc32:" + crc.getValue() + "\"");
                } else {
                    System.out.print(" check \"" + element.getText() + "\"");
                }
            }
            System.out.println();
            return;
        } catch (StaleElementReferenceException e) {
            // If element has gone stale during a dump, ignore it
            if (!verify)
                return;
            // element has gone stale, re-select it
            System.out.println("// EXCEPTION : StaleElementReference");
        } catch (Exception e) {
            if (verify)
                throw e;
            return;
        }
        sleepAndReselect(100);
    } while (_waitFor > 0 && (new Date()).getTime() < _waitFor);
}

From source file:com.redspr.redrobot.WebDriverRobot.java

License:Open Source License

@Override
public boolean isSelected(String... x) {
    WebElement e = locCheckable(x);

    return e.isSelected();
}

From source file:com.screenslicer.core.scrape.QueryForm.java

License:Open Source License

public static void perform(RemoteWebDriver driver, FormQuery context) throws ActionFailed {
    try {//www. j av  a 2  s .c  o  m
        Util.get(driver, context.site, true);
        Util.doClicks(driver, context.preAuthClicks, null);
        QueryCommon.doAuth(driver, context.credentials);
        Util.doClicks(driver, context.preSearchClicks, null);
        Map<String, HtmlNode> formControls = new HashMap<String, HtmlNode>();
        for (int i = 0; i < context.formSchema.length; i++) {
            formControls.put(context.formSchema[i].guid, context.formSchema[i]);
        }
        Map<String, List<String>> formData = context.formModel;
        boolean valueChanged = false;
        int count = 0;
        final int MAX_TRIES = 3;
        Element body = Util.openElement(driver, null, null, null);
        do {
            ++count;
            valueChanged = false;
            for (Map.Entry<String, List<String>> entry : formData.entrySet()) {
                try {
                    HtmlNode formControl = formControls.get(entry.getKey());
                    if (!CommonUtil.isEmpty(entry.getValue())) {
                        if ("select".equalsIgnoreCase(formControl.tagName)) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: select");
                            }
                            Select select = new Select(Util.toElement(driver, formControl, body));
                            if (select.isMultiple()) {
                                select.deselectAll();
                            }
                            List<WebElement> selectedElements = select.getAllSelectedOptions();
                            List<String> selectedStrings = new ArrayList<String>();
                            for (WebElement selectedElement : selectedElements) {
                                String selectedString = selectedElement.getAttribute("value");
                                if (!CommonUtil.isEmpty(selectedString)) {
                                    selectedStrings.add(selectedString);
                                }
                            }
                            boolean matches = true;
                            for (String selectedString : selectedStrings) {
                                if (!entry.getValue().contains(selectedString)) {
                                    matches = false;
                                    break;
                                }
                            }
                            if (!matches || selectedStrings.size() != entry.getValue().size()) {
                                for (String val : entry.getValue()) {
                                    valueChanged = true;
                                    select.selectByValue(val);
                                    Util.driverSleepVeryShort();
                                }
                            }
                        } else if ("input".equalsIgnoreCase(formControl.tagName)
                                && ("text".equalsIgnoreCase(formControl.type)
                                        || "search".equalsIgnoreCase(formControl.type))) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: input[text|search]");
                            }
                            WebElement element = Util.toElement(driver, formControl, body);
                            valueChanged = QueryCommon.typeText(driver, element, entry.getValue().get(0), true,
                                    false);
                        } else if ("input".equalsIgnoreCase(formControl.tagName)
                                && ("checkbox".equalsIgnoreCase(formControl.type)
                                        || "radio".equalsIgnoreCase(formControl.type))) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: input[checkbox|radio]");
                            }
                            WebElement element = Util.toElement(driver, formControl, body);
                            if (entry.getValue() != null && !entry.getValue().isEmpty()) {
                                if ("radio".equalsIgnoreCase(formControl.type)) {
                                    String elementVal = element.getAttribute("value");
                                    String schemaVal = formControl.value;
                                    String modelVal = entry.getValue().get(0);
                                    if (elementVal != null && schemaVal != null
                                            && elementVal.equalsIgnoreCase(schemaVal)
                                            && modelVal.equalsIgnoreCase(schemaVal)) {
                                        if (!element.isSelected()) {
                                            if (WebApp.DEBUG) {
                                                System.out.println("Clicking radio button");
                                            }
                                            valueChanged = Util.click(driver, element);
                                        }
                                    }
                                } else if (!element.isSelected()) {
                                    if (WebApp.DEBUG) {
                                        System.out.println("Clicking [checkbox|radio]");
                                    }
                                    valueChanged = Util.click(driver, element);
                                }
                            } else {
                                if (element.isSelected()) {
                                    if (WebApp.DEBUG) {
                                        System.out.println("Deselecting [checkbox|radio]");
                                    }
                                    valueChanged = true;
                                    element.clear();
                                    Util.driverSleepVeryShort();
                                }
                            }
                        }
                    }
                } catch (Throwable t) {
                    Log.exception(t);
                }
            }
        } while (valueChanged && count < MAX_TRIES);
        doSubmit(driver, context.formId);
        Util.doClicks(driver, context.postSearchClicks, null);
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.sebuilder.interpreter.steptype.SetElementNotSelected.java

License:Apache License

@Override
public boolean run(TestRun ctx) {
    WebElement e = ctx.locator("locator").find(ctx);
    if (e.isSelected()) {
        e.click();/*from   ww  w .j a  va  2s.c om*/
    }
    return true;
}

From source file:com.sebuilder.interpreter.steptype.SetElementSelected.java

License:Apache License

@Override
public boolean run(TestRun ctx) {
    WebElement e = ctx.locator("locator").find(ctx);
    if (!e.isSelected()) {
        e.click();/*from ww w .j  ava 2  s  .  com*/
    }
    return true;
}

From source file:com.seleniumtests.uipage.htmlelements.CachedHtmlElement.java

License:Apache License

public CachedHtmlElement(WebElement elementToCache) {
    try {//from w  ww  .ja v  a2  s .c o m
        rectangle = elementToCache.getRect();
        location = new Point(rectangle.x, rectangle.y);
        size = new Dimension(rectangle.width, rectangle.height);
    } catch (WebDriverException e) {
        location = elementToCache.getLocation();
        size = elementToCache.getSize();
        rectangle = new Rectangle(location, size);
    }

    cachedElement = Jsoup.parseBodyFragment(elementToCache.getAttribute("outerHTML")).body().child(0);
    if ("option".equals(cachedElement.tagName())
            || ("input".equals(cachedElement.tagName())
                    && "checkbox".equals(cachedElement.attributes().getIgnoreCase("type")))
            || ("input".equals(cachedElement.tagName())
                    && "radio".equals(cachedElement.attributes().getIgnoreCase("type")))) {
        selected = elementToCache.isSelected();
    } else {
        selected = false;
    }
    realElement = elementToCache;

}

From source file:com.seleniumtests.uipage.htmlelements.SelectList.java

License:Apache License

/**
 * @return All selected options belonging to this select tag
 */// w w w .j  av a2 s. c o  m
@ReplayOnError
public List<WebElement> getAllSelectedOptions() {
    List<WebElement> toReturn = new ArrayList<>();

    try {
        findElement();
        switch (selectType) {
        case ANGULAR_MATERIAL:
            for (WebElement option : options) {
                if (option.getAttribute("class").contains("mat-selected")) {
                    toReturn.add(option);
                }
            }
        case HTML:
            for (WebElement option : options) {
                if (option.isSelected()) {
                    toReturn.add(option);
                }
            }
        case LIST:
            break;
        default:
            throw new CustomSeleniumTestsException(selectType + "not recognized ");
        }

        return toReturn;
    } finally {
        finalizeAction();
    }
}

From source file:com.seltaf.webelements.SelectList.java

License:Apache License

/**
 * De-selects all options in a multi-select list element.
 *//*from  ww w  .  j a va  2s . co m*/
public void deselectAll() {
    SeltafTestLogger.logWebStep(null, "deselect all options on " + toHTML(), false);
    findElement();
    if (!isMultiple()) {
        throw new UnsupportedOperationException("You may only deselect all options of a multi-select");
    }

    for (WebElement option : options) {
        if (option.isSelected()) {
            option.click();
        }
    }
}

From source file:com.seltaf.webelements.SelectList.java

License:Apache License

public void deselectByIndex(final int index) {
    SeltafTestLogger.logWebStep(null, "deselect index\"" + index + "\" on " + toHTML(), false);
    findElement();//from  w  w w .  ja va  2s. c o  m

    WebElement option = options.get(index);
    if (option.isSelected()) {
        option.click();
    }
}