Java Code Examples for org.openqa.selenium.WebElement.isSelected()

The following are Jave code examples for showing how to use isSelected() of the org.openqa.selenium.WebElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Selenium-Foundation   File: ComponentContainer.java   View Source Code Vote up 6 votes
/**
 * Update the specified element with the indicated value
 * 
 * @param element target element (checkbox)
 * @param value desired value
 * @return 'true' if element value changed; otherwise 'false'
 */
public static boolean updateValue(WebElement element, boolean value) {
    Objects.requireNonNull(element, "[element] must be non-null");
    
    String tagName = element.getTagName().toLowerCase();
    if ("input".equals(tagName) && "checkbox".equals(element.getAttribute("type"))) {
        if (element.isSelected() != value) {
            element.click();
            return true;
        } else {
            return false;
        }
    }
    
    return updateValue(element, Boolean.toString(value));
}
 
Example 2
Project: marathonv5   File: JavaDriverTest.java   View Source Code Vote up 6 votes
public void isSelectedOnNonSelectableComponents() throws Throwable {
    driver = new JavaDriver();
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
    WebElement element1 = driver.findElement(By.name("text-field"));
    try {
        element1.isSelected();
        throw new MissingException(UnsupportedCommandException.class);
    } catch (UnsupportedCommandException e) {

    }
}
 
Example 3
Project: Cognizant-Intelligent-Test-Scripter   File: CheckBox.java   View Source Code Vote up 6 votes
@Action(object = ObjectType.BROWSER, desc = "Check all the check boxes in the context")
public void checkAllCheckBoxes() {
    try {
        List<WebElement> checkboxes = Driver.findElements(By.cssSelector("input[type=checkbox]"));
        if (checkboxes.isEmpty()) {
            Report.updateTestLog(Action, "No Checkbox present in the page", Status.WARNING);
        } else {
            for (WebElement checkbox : checkboxes) {
                if (checkbox.isDisplayed() && !checkbox.isSelected()) {
                    checkbox.click();
                }
            }
            Report.updateTestLog(Action, "All checkboxes are checked", Status.PASS);
        }
    } catch (Exception ex) {
        Report.updateTestLog(Action, "Error while checking checkboxes - " + ex, Status.FAIL);
        Logger.getLogger(CheckBox.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 4
Project: ats-framework   File: MobileCheckBox.java   View Source Code Vote up 6 votes
/**
 * Check the check box
 */
@Override
@PublicAtsApi
public void check() {

    new MobileElementState(this).waitToBecomeExisting();

    try {
        WebElement checkboxElement = MobileElementFinder.findElement(appiumDriver, this);
        if (!checkboxElement.isSelected()) {
            if (appiumDriver instanceof AndroidDriver) {

                // checkboxElement.click(); // throwing exception (on Android) with message: Element is not clickable at point (x,y). Other element would receive the click
                new Actions(appiumDriver).moveToElement(checkboxElement).click().perform();
            } else {

                checkboxElement.click();
            }
        }
    } catch (Exception se) {
        throw new MobileOperationException(this, "check", se);
    }

    UiEngineUtilities.sleep();
}
 
Example 5
Project: ats-framework   File: HiddenHtmlMultiSelectList.java   View Source Code Vote up 6 votes
/**
 * unselect a value
 *
 * @param value the value to unselect
 */
@Override
@PublicAtsApi
public void unsetValue(
                        String value ) {

    new HiddenHtmlElementState(this).waitToBecomeExisting();

    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement el : optionElements) {
        if (el.getText().equals(value)) {
            if (el.isSelected()) {
                ((HtmlUnitWebElement) el).click();

                UiEngineUtilities.sleep();
            }
            return;
        }
    }
    throw new SeleniumOperationException("Option with label '" + value + "' not found. ("
                                         + this.toString() + ")");
}
 
Example 6
Project: ats-framework   File: HiddenHtmlMultiSelectList.java   View Source Code Vote up 6 votes
/**
 * @return the selected value
 */
@Override
@PublicAtsApi
public String[] getValues() {

    new HiddenHtmlElementState(this).waitToBecomeExisting();

    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    List<String> values = new ArrayList<String>();
    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement element : optionElements) {
        if (element.isSelected()) {
            values.add(element.getText());
        }
    }
    if (values.isEmpty()) {

        throw new SeleniumOperationException("There is no selected 'option' in " + this.toString());
    }
    return values.toArray(new String[0]);
}
 
Example 7
Project: ats-framework   File: HiddenHtmlSingleSelectList.java   View Source Code Vote up 6 votes
/**
 * @return the single selection value
 */
@Override
@PublicAtsApi
public String getValue() {

    new HiddenHtmlElementState(this).waitToBecomeExisting();

    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement element : optionElements) {
        if (element.isSelected()) {
            return element.getText();
        }
    }
    throw new SeleniumOperationException("There is no selected 'option' in " + this.toString());
}
 
Example 8
Project: ats-framework   File: RealHtmlCheckBox.java   View Source Code Vote up 6 votes
/**
 * Check the check box
 */
@Override
@PublicAtsApi
public void check() {

    new RealHtmlElementState(this).waitToBecomeExisting();

    WebElement checkBoxElement = RealHtmlElementLocator.findElement(this);
    if (!checkBoxElement.isEnabled()) {
        throw new UnsupportedOperationException("You may not check a disabled element."
                                                + toString());
    }
    if (!checkBoxElement.isSelected()) {
        checkBoxElement.click();
    }

    UiEngineUtilities.sleep();
}
 
Example 9
Project: ats-framework   File: RealHtmlCheckBox.java   View Source Code Vote up 6 votes
/**
 * Uncheck the check box
 */
@Override
@PublicAtsApi
public void unCheck() {

    new RealHtmlElementState(this).waitToBecomeExisting();

    WebElement checkBoxElement = RealHtmlElementLocator.findElement(this);
    if (!checkBoxElement.isEnabled()) {
        throw new UnsupportedOperationException("You may not uncheck a disabled element."
                                                + toString());
    }
    if (checkBoxElement.isSelected()) {
        checkBoxElement.click();
    }

    UiEngineUtilities.sleep();
}
 
Example 10
Project: ats-framework   File: RealHtmlMultiSelectList.java   View Source Code Vote up 6 votes
/**
 * unselect a value
 *
 * @param value the value to unselect
 */
@Override
@PublicAtsApi
public void unsetValue(
                        String value ) {

    new RealHtmlElementState(this).waitToBecomeExisting();

    WebElement element = RealHtmlElementLocator.findElement(this);
    Select select = new Select(element);
    // select.deselectByVisibleText( value ); // this method doesn't throw an exception if the option doesn't exist
    for (WebElement option : select.getOptions()) {
        if (option.getText().equals(value)) {
            if (option.isSelected()) {
                option.click();
                UiEngineUtilities.sleep();
            }
            return;
        }
    }
    throw new SeleniumOperationException("Option with label '" + value + "' not found. ("
                                         + this.toString() + ")");
}
 
Example 11
Project: marathonv5   File: JavaDriverTest.java   View Source Code Vote up 5 votes
public void isSelected() throws Throwable {
    driver = new JavaDriver();
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
    WebElement element1 = driver.findElement(By.name("click-me"));
    element1.isSelected();
}
 
Example 12
Project: NoraUi   File: Step.java   View Source Code Vote up 5 votes
/**
 * Checks that given value is matching the selected radio list button.
 *
 * @param pageElement
 *            The page element
 * @param value
 *            The value to check the selection from
 * @return true if the given value is selected, false otherwise.
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected boolean checkRadioList(PageElement pageElement, String value) throws FailureException {
    try {
        List<WebElement> radioButtons = Context.waitUntil(ExpectedConditions.presenceOfAllElementsLocatedBy(Utilities.getLocator(pageElement)));
        for (WebElement button : radioButtons) {
            if (button.getAttribute(VALUE).equalsIgnoreCase(value) && button.isSelected()) {
                return true;
            }
        }
    } catch (Exception e) {
        new Result.Failure<>(e.getMessage(), Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_FIND_ELEMENT), true, pageElement.getPage().getCallBack());
    }
    return false;
}
 
Example 13
Project: mot-automated-testsuite   File: WebDriverWrapper.java   View Source Code Vote up 5 votes
/**
 * Clears the specified checkbox button. Supports well-formed labels and inputs nested inside the label.
 * @param labelText  The checkbox button label
 */
public void clearCheckbox(String labelText) {
    WebElement checkboxElement = findCheckbox(labelText);

    // click checkbox if already selected, otherwise leave as not selected
    if (checkboxElement.isSelected()) {
        checkboxElement.click();
    }
}
 
Example 14
Project: ats-framework   File: HiddenHtmlMultiSelectList.java   View Source Code Vote up 5 votes
/**
 * select a value
 *
 * @param value the value to select
 */
@Override
@PublicAtsApi
public void setValue(
                      String value ) {

    new HiddenHtmlElementState(this).waitToBecomeExisting();

    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    if (selectElement.getAttribute("multiple") == null) {
        throw new SeleniumOperationException("Not a multi-select. You may only add a selection to a select that supports multiple selections. ("
                                             + this.toString() + ")");
    }

    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement el : optionElements) {
        if (el.getText().equals(value)) {
            if (!el.isSelected()) {
                ((HtmlUnitWebElement) el).click();

                UiEngineUtilities.sleep();
            }
            return;
        }
    }

    throw new SeleniumOperationException("Option with label '" + value + "' not found. ("
                                         + this.toString() + ")");
}
 
Example 15
Project: ats-framework   File: RealHtmlCheckBox.java   View Source Code Vote up 5 votes
/**
 * Tells whether the check box is checked
 */
@Override
@PublicAtsApi
public boolean isChecked() {

    new RealHtmlElementState(this).waitToBecomeExisting();

    WebElement checkBoxElement = RealHtmlElementLocator.findElement(this);
    return checkBoxElement.isSelected();
}
 
Example 16
Project: testing_security_development_enterprise_systems   File: CommentsPageObject.java   View Source Code Vote up 5 votes
public void doModerate(int position, boolean moderate){

        WebElement checkbox = driver.findElement(By.id("commentTable:"+position+":moderateForm:moderate"));
        if(moderate && checkbox.isSelected()){
            return;
        }
        if(!moderate && !checkbox.isSelected()){
            return;
        }
        checkbox.click();
        waitForPageToLoad();
    }
 
Example 17
Project: testing_security_development_enterprise_systems   File: MenuPageObject.java   View Source Code Vote up 5 votes
public void selectDishByName(String name, boolean on){

        WebElement check = getCheckBoxForDish(name);

        if(on != check.isSelected()){
            check.click();
        }
    }
 
Example 18
Project: mot-automated-testsuite   File: WebDriverWrapper.java   View Source Code Vote up 5 votes
/**
 * Selects the specified checkbox button. Supports well-formed labels and inputs nested inside the label.
 * @param labelText  The checkbox button label
 */
public void selectCheckbox(String labelText) {
    WebElement checkboxElement = findCheckbox(labelText);

    // click checkbox if not already selected, otherwise leave as selected
    if (!checkboxElement.isSelected()) {
        checkboxElement.click();
    }
}
 
Example 19
Project: phoenix.webui.framework   File: SeleniumCheck.java   View Source Code Vote up 4 votes
@Override
public void checkByValue(Element element, String value)
{
	WebElement parentWebEle = searchStrategyUtils.findStrategy(WebElement.class, element).search(element);
	if(parentWebEle == null)
	{
		logger.error(String.format("can not found element byText [%s].", value));
		return;
	}
	
	List<Locator> locatorList = element.getLocatorList();
	List<Locator> tmpList = new ArrayList<Locator>(locatorList);
	
	ElementSearchStrategy<WebElement> strategy = context.getBean("zoneSearchStrategy", ElementSearchStrategy.class);
	
	SeleniumValueLocator valueLocator = context.getBean(SeleniumValueLocator.class);
	valueLocator.setHostType("byTagName");
	valueLocator.setHostValue("input");
	
	locatorList.clear();
	locatorList.add(valueLocator);
	valueLocator.setValue(value);
	
	WebElement itemWebEle = null;
	try
	{
		if(strategy instanceof ParentElement)
		{
			((ParentElement) strategy).setParent(parentWebEle);
		}
		
		itemWebEle = strategy.search(element);
		if(itemWebEle != null)
		{
			if(!itemWebEle.isSelected())
			{
				itemWebEle.click();
			}
		}
	}
	catch(ElementNotVisibleException e)
	{
		e.printStackTrace();
		logger.error(String.format("Element [%s] click error, parent [%s], text [%s].",
				itemWebEle, element, value));
	}
	finally
	{
		//清空缓存
		locatorList.clear();
		locatorList.addAll(tmpList);
		
		if(strategy instanceof ParentElement)
		{
			((ParentElement) strategy).setParent(null);
		}
	}
}
 
Example 20
Project: NoraUi   File: Step.java   View Source Code Vote up 3 votes
/**
 * Checks a checkbox type element.
 *
 * @param element
 *            Target page element
 * @param checked
 *            Final checkbox value
 * @param args
 *            list of arguments to format the found selector with
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Failure with {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_UNABLE_TO_CHECK_ELEMENT} message (with screenshot)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void selectCheckbox(PageElement element, boolean checked, Object... args) throws TechnicalException, FailureException {
    try {
        WebElement webElement = Context.waitUntil(ExpectedConditions.elementToBeClickable(Utilities.getLocator(element, args)));
        if (webElement.isSelected() != checked) {
            webElement.click();
        }
    } catch (Exception e) {
        new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_CHECK_ELEMENT), element, element.getPage().getApplication()), true,
                element.getPage().getCallBack());
    }
}