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:org.sugarcrm.voodoodriver.EventLoop.java

License:Apache License

/**
 * Handle a <select> event
 *
 * @param event  the <select> event
 * @param parent this element's parent/*from  w  w w  . ja v  a 2 s .c om*/
 * @return the a {@link WebElement} or null
 */

private WebElement selectEvent(VDDHash event, WebElement parent) {
    boolean required = true, multiselect = true;
    WebElement element = null;

    this.report.Log("Select event Started.");

    if (event.containsKey("required")) {
        required = this.clickToBool(event.get("required").toString());
    }

    try {
        Select sel = null;
        element = this.findElement(event, parent, required);
        if (element == null) {
            this.report.Log("Select event finished.");
            return null;
        }

        sel = new Select(element);
        this.firePlugin(element, Elements.SELECT, PluginEvent.AFTERFOUND);

        this.checkDisabled(event, element);
        handleVars(element.getAttribute("value"), event);

        if (event.containsKey("assert") || event.containsKey("assertnot")) {
            boolean wantSel = event.containsKey("assert");
            String val = this.replaceString(event.get(wantSel ? "assert" : "assertnot").toString());
            boolean optionFound = false;

            for (WebElement opt : sel.getOptions()) {
                if (opt.getText().contains(val)) {
                    boolean issel = opt.isSelected();
                    this.report.Assert("Select option '" + val + "' is " + (issel ? "" : "not ") + "selected",
                            wantSel ^ issel, false);
                    optionFound = true;
                    break;
                }
            }

            if (!optionFound) {
                this.report.ReportError("Failed to find select option '" + val + "'!");
            }
        }

        if (event.containsKey("assertselected")) {
            boolean anySelected = sel.getAllSelectedOptions().size() > 0;
            boolean shouldBeSelected = clickToBool(event.get("assertselected").toString());

            report.Assert("Option " + (anySelected ? "" : "not ") + "selected", anySelected, shouldBeSelected);
        }

        if (event.containsKey("included") || event.containsKey("notincluded")) {
            boolean wantOpt = event.containsKey("included");
            String val = this.replaceString(event.get(!wantOpt ? "notincluded" : "included").toString());
            boolean haveOpt = false;

            for (WebElement opt : sel.getOptions()) {
                if (opt.getText().contains(val)) {
                    haveOpt = true;
                    break;
                }
            }

            String m = String.format("Select option %s%s found and%s expected", val, haveOpt ? "" : " not",
                    wantOpt ? "" : " not");
            this.report.Assert(m, wantOpt ^ haveOpt, false);
        }

        if (event.containsKey("multiselect") && !this.clickToBool(event.get("multiselect").toString())
                && sel.isMultiple()) {
            multiselect = false;
        }

        if (event.containsKey("clear") && this.clickToBool(event.get("clear").toString()) && sel.isMultiple()) {
            this.report.Log("Clearing select element.");
            sel.deselectAll();
        }

        try {
            if (event.containsKey("set") || event.containsKey("setreal")) {
                boolean useVal = event.containsKey("setreal");
                String val = this.replaceString(event.get(useVal ? "setreal" : "set").toString());
                this.report
                        .Log("Setting option by " + (useVal ? "value" : "visible text") + ": '" + val + "'.");
                if (multiselect == false) {
                    /*
                     * isMultiple() must be true in order for
                     * multiselect to be false
                     */
                    sel.deselectAll();
                }

                try {
                    if (useVal) {
                        sel.selectByValue(val);
                    } else {
                        sel.selectByVisibleText(val);
                    }
                    this.firePlugin(element, Elements.SELECT, PluginEvent.AFTERSET);
                } catch (NoSuchElementException e) {
                    this.report.ReportError("Option with " + (useVal ? "value" : "visible text") + " '" + val
                            + "' does not exist");
                }
            }

            if (event.containsKey("jscriptevent")) {
                this.report.Log("Firing Javascript Event: " + event.get("jscriptevent").toString());
                this.Browser.fire_event(element, event.get("jscriptevent").toString());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
                this.report.Log("Javascript event finished.");
            }

            if (event.containsKey("click") && this.clickToBool(event.get("click").toString())) {
                this.firePlugin(element, Elements.SELECT, PluginEvent.BEFORECLICK);
                element.click();
                this.firePlugin(element, Elements.SELECT, PluginEvent.AFTERCLICK);
            }

            if (element.isDisplayed() && event.containsKey("children")) {
                this.processEvents((Events) event.get("children"), element);
            }
        } catch (StaleElementReferenceException e) {
            /*
             * Selecting a value has the potential to refresh the page
             * (Bug 49533).
             */
            this.report.Log("Page refreshed; select element no longer exists.");
            element = null;
        }

    } catch (ElementNotVisibleException exp) {
        logElementNotVisible(required, event);
    } catch (Exception exp) {
        this.report.ReportException(exp);
    }

    this.report.Log("Select event finished.");
    return element;
}

From source file:org.sugarcrm.voodoodriver.EventLoop.java

License:Apache License

private WebElement checkboxEvent(VDDHash event, WebElement parent) {
    boolean click = false;
    boolean required = true;
    WebElement element = null;

    this.resetThreadTime();

    if (event.containsKey("required")) {
        required = this.clickToBool(event.get("required").toString());
    }// ww w  .jav a 2s . c  o  m

    try {
        element = this.findElement(event, parent, required);
        if (element == null) {
            return element;
        }

        this.firePlugin(element, Elements.CHECKBOX, PluginEvent.AFTERFOUND);

        this.checkDisabled(event, element);

        if (event.containsKey("click")) {
            click = this.clickToBool(event.get("click").toString());
            if (click) {
                this.firePlugin(element, Elements.CHECKBOX, PluginEvent.BEFORECLICK);
                element.click();
                this.firePlugin(element, Elements.CHECKBOX, PluginEvent.AFTERCLICK);
            }
        }

        if (event.containsKey("set")) {
            String msg = "";
            String set = event.get("set").toString();
            set = this.replaceString(set);

            if (Boolean.valueOf(set) == element.isSelected()) {
                msg = String.format("Checkbox current checked state is already: '%s', skipping click.", set);
            } else {
                msg = String.format("Checkbox's state is '%s', clicking to set state to '%s'.",
                        element.isSelected(), set);
                element.click();
                this.firePlugin(element, Elements.CHECKBOX, PluginEvent.AFTERCLICK);
            }
            this.report.Log(msg);
        }

        if (event.containsKey("assert")) {
            String src = String.valueOf(element.isSelected());
            String value = event.get("assert").toString();
            value = this.replaceString(value);
            this.report.Assert(value, src);
        }

        if (event.containsKey("assertnot")) {
            String src = String.valueOf(element.isSelected());
            String value = event.get("assertnot").toString();
            value = this.replaceString(value);
            this.report.AssertNot(value, src);
        }

        String value = element.getAttribute("value");
        handleVars(value, event);

    } catch (ElementNotVisibleException exp) {
        logElementNotVisible(required, event);
    } catch (Exception exp) {
        this.report.ReportException(exp);
    }

    this.resetThreadTime();
    return element;
}

From source file:org.superbiz.todos.TodoListPageObject.java

License:Apache License

public List<TodoItem> openTasks() {
    final List<WebElement> currentTodos = todoRepeat;
    List<TodoItem> todoItems = currentTodos.stream().map((input) -> {
        final WebElement checkbox = input.findElement(By.tagName("input"));
        final WebElement description = input.findElement(By.tagName("span"));
        return new TodoItem(description.getText(), checkbox.isSelected());
    }).collect(Collectors.toList());

    return todoItems;
}

From source file:org.testeditor.fixture.web.HtmlWebFixture.java

License:Open Source License

/**
 * Returns the current selection of a radio-button or check-box.
 * //from  w  w w  .j a  va2s. c o m
 * @param elementListKey
 *            key in the element list to find the technical locator
 * @param replaceArgs
 *            values to replace the place holders in the element list entry
 * @return the current selection or {@code null} if nothing is selected
 * @throws StopTestException
 *             if a timeout occurred
 */
private String getSelectionOfRadioButtonOrCheckBox(String elementListKey, String... replaceArgs)
        throws StopTestException {
    List<WebElement> elements = findAllAvailableWebElements(elementListKey, replaceArgs);
    for (WebElement webElement : elements) {
        if (webElement.isSelected()) {
            return webElement.getAttribute("value");
        }
    }
    return null;
}

From source file:org.testeditor.fixture.web.WebFixture.java

License:Open Source License

/**
 * Finds a checkbox by its Element List key and gets its value. <br />
 * <br />//ww  w .  j  ava2s  .  co m
 * 
 * FitNesse usage..: |$var=|read checkbox;|arg1|[arg2, arg3, ...]| <br />
 * FitNesse example: |$result=|read checkbox;|CheckboxInRow{0}Col{1}|[5, 3]|
 * <br />
 * <br />
 * 
 * @param elementListKey
 *            key to find the technical locator
 * @param replaceArgs
 *            values to replace the place holders in the element list entry
 *            with
 * @return true, if the target element is checked; false, if the element is
 *         not checked or the element could not be found or is not visible
 *         (i.e. hidden using CSS, etc.).
 */
public boolean readCheckbox(String elementListKey, String... replaceArgs) {
    boolean result = false;

    WebElement element = findWebelement(elementListKey, replaceArgs);

    if (element != null && element.isDisplayed()) {
        result = element.isSelected();
    }

    return result;
}

From source file:org.uiautomation.ios.selenium.FormHandlingTest.java

License:Apache License

@Test
public void testIsSelectedShouldWorkRightAfterSelecting() {
    driver.get(pages.formPage);/*from w  w w . j av a2s.c om*/

    WebElement checkbox = driver.findElement(By.id("checky"));
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
    checkbox.click();
    assertTrue(checkbox.isSelected());
}

From source file:org.usapi.nodetypes.AbstractNode.java

License:Apache License

public boolean nativeIsSelected() throws USAPIException {
    WebElement webElement = findElement(getLocator());
    return webElement.isSelected();
}

From source file:org.vaadin.testbenchsauce.BaseTestBenchTestCase.java

protected void assertRadioButtonSelected(String formFieldCaption, String radioButtonCaption) {
    logStep.info("Asserting radio button is selected: with form caption " + formFieldCaption
            + " and radio button caption " + radioButtonCaption);
    WebElement radioButton = getRadioButton(formFieldCaption, radioButtonCaption);
    assertNotNull("Could not find radio button with form caption " + formFieldCaption
            + " and radio button caption " + radioButtonCaption, radioButton);
    assertThat(radioButton.isSelected());
}

From source file:org.webtestingexplorer.state.VisibleElementsState.java

License:Open Source License

/**
 * Get information of a WebElement.//from ww w.  j a  va 2  s .c om
 */
private Map<WebElementIdentifier, Map<String, String>> collectProperties(WebDriverWrapper driver,
        List<WebElementWithIdentifier> elements) {
    Map<WebElementIdentifier, Map<String, String>> allProperties = new HashMap<WebElementIdentifier, Map<String, String>>();

    for (WebElementWithIdentifier elementWithId : elements) {
        // Get all properties
        WebElement e = elementWithId.getElement();
        Map<String, String> properties = new HashMap<String, String>();
        String value = e.getTagName();
        if (value != null) {
            properties.put(TAG_NAME, value);
        }

        value = e.getText();
        if (value != null) {
            properties.put(VALUE, value);
        }

        properties.put(ENABLED, Boolean.toString(e.isEnabled()));
        properties.put(SELECTED, Boolean.toString(e.isSelected()));

        for (String attribute : ELEMENT_ATTRIBUTES) {
            String attributeValue = e.getAttribute(attribute);
            if (attributeValue != null) {
                properties.put(attribute, attributeValue);
            }
        }

        allProperties.put(elementWithId.getIdentifier(), properties);
    }

    return allProperties;
}

From source file:org.wso2.am.integration.ui.tests.APIMANAGER3366MultipleGatewayPublishTestCase.java

License:Open Source License

@Test(groups = "wso2.am", description = "published with select environments section", enabled = false)
public void testPublishApiWithEnvironmentTabSelection() throws Exception {
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username"))
            .sendKeys(gatewayContext.getContextTenant().getContextUser().getUserName());
    driver.findElement(By.id("pass")).clear();
    driver.findElement(By.id("pass"))
            .sendKeys(gatewayContext.getContextTenant().getContextUser().getPassword());
    driver.findElement(By.id("loginButton")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Add")));
    driver.findElement(By.linkText("Add")).click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("create-new-api")));
    driver.findElement(By.id("create-new-api")).click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("designNewAPI")));
    driver.findElement(By.id("designNewAPI")).click();

    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys("publishWithEnvironments2");
    driver.findElement(By.id("context")).clear();
    driver.findElement(By.id("context")).sendKeys("publishWithEnvironments2");
    driver.findElement(By.id("version")).clear();
    driver.findElement(By.id("version")).sendKeys("1.0.0");
    driver.findElement(By.id("description")).clear();
    driver.findElement(By.id("description")).sendKeys(API_DESCRIPTION);
    driver.findElement(By.id("resource_url_pattern")).clear();
    driver.findElement(By.id("resource_url_pattern")).sendKeys("*");
    /*driver.findElement(By.id("inputResource")).clear();
    driver.findElement(By.id("inputResource")).sendKeys("default");*/
    driver.findElement(By.cssSelector("input.http_verb_select")).click();
    driver.findElement(By.id("add_resource")).click();
    driver.findElement(By.id("go_to_implement")).click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@value='#managed-api']")));
    driver.findElement(By.xpath("//div[@value='#managed-api']")).click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("go_to_manage")));
    driver.findElement(By.id("jsonform-0-elt-production_endpoints")).clear();
    driver.findElement(By.id("jsonform-0-elt-production_endpoints")).sendKeys(API_URL);
    driver.findElement(By.id("go_to_manage")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("publish_api")));
    driver.findElement(By.xpath("//button[@type='button']")).click();
    driver.findElement(By.xpath("//input[@value='Gold']")).click();
    driver.findElement(By.cssSelector("legend.legend-with-hidden-info.js_hidden_section_title")).click();
    WebElement checkBox = driver.findElement(By.cssSelector("input.env"));
    if (!checkBox.isSelected()) {
        checkBox.click();//from   w ww.  j  av  a 2 s  .c  o  m
    }
    driver.findElement(By.id("publish_api")).click();
    Assert.assertTrue(isAPIPublished("publishWithEnvironments2", "1.0.0"),
            "API is Successfully published with select environments section");
    driver.findElement(By.id("userMenu")).click();
    driver.findElement(By.cssSelector("button.btn.btn-danger")).click();
}