Models a SELECT tag, providing helper methods to select and deselect options.
| 29 | |
| 30 | /** Models a SELECT tag, providing helper methods to select and deselect options. */ |
| 31 | public class Select implements ISelect, WrapsElement { |
| 32 | |
| 33 | private final WebElement element; |
| 34 | private final boolean isMulti; |
| 35 | |
| 36 | /** |
| 37 | * Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not, |
| 38 | * then an UnexpectedTagNameException is thrown. |
| 39 | * |
| 40 | * @param element SELECT element to wrap |
| 41 | * @throws UnexpectedTagNameException when element is not a SELECT |
| 42 | */ |
| 43 | public Select(WebElement element) { |
| 44 | String tagName = element.getTagName(); |
| 45 | |
| 46 | if (!"select".equalsIgnoreCase(tagName)) { |
| 47 | throw new UnexpectedTagNameException("select", tagName); |
| 48 | } |
| 49 | |
| 50 | this.element = element; |
| 51 | |
| 52 | String value = element.getDomAttribute("multiple"); |
| 53 | |
| 54 | // The atoms normalize the returned value, but check for "false" |
| 55 | isMulti = (value != null && !"false".equals(value)); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public WebElement getWrappedElement() { |
| 60 | return element; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return Whether this select element support selecting multiple options at the same time? This |
| 65 | * is done by checking the value of the "multiple" attribute. |
| 66 | */ |
| 67 | @Override |
| 68 | public boolean isMultiple() { |
| 69 | return isMulti; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return This is done by checking the value of attributes in "visibility", "display", "opacity" |
| 74 | * Return false if visibility is set to 'hidden', display is 'none', or opacity is 0 or 0.0. |
| 75 | */ |
| 76 | private boolean hasCssPropertyAndVisible(WebElement webElement) { |
| 77 | Set<String> cssValueCandidates = Set.of("hidden", "none", "0", "0.0"); |
| 78 | List<String> cssPropertyCandidates = List.of("visibility", "display", "opacity"); |
| 79 | |
| 80 | for (String property : cssPropertyCandidates) { |
| 81 | String cssValue = webElement.getCssValue(property); |
| 82 | if (cssValueCandidates.contains(cssValue)) return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
no outgoing calls