All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.thucydides.core.webelements.MultipleSelect Maven / Gradle / Ivy

There is a newer version: 4.2.8
Show newest version
package net.thucydides.core.webelements;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * A convenience class to help handle multiple select fields.
 */
public class MultipleSelect {
    private final WebElement dropdownField;

    public MultipleSelect(final WebElement dropdownField) {
        this.dropdownField = dropdownField;
    }

    public Set getSelectedOptionLabels() {

        List options = new Select(dropdownField).getAllSelectedOptions();

        Set selectedOptions = new HashSet<>();
        for (WebElement option : options) {
            if (option.isSelected()) {
                selectedOptions.add(option.getText());
            }
        }
        return selectedOptions;
    }

    public Set getSelectedOptionValues() {
        Set selectedOptions = new HashSet();

        List options = dropdownField.findElements(By.tagName("option"));
        for (WebElement option : options) {
            if (option.isSelected()) {
                selectedOptions.add(option.getAttribute("value"));
            }
        }
        return selectedOptions;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy