/** * Select all options that display text matching the argument. That is, when given "Bar" this * would select an option like: * * <p><option value="foo">Bar</option> * * @param text The visible text to match against * @throws NoSuchElementException If no matching option elements are found */ public void selectByVisibleText(String text) { // try to find the option via XPATH ... List<WebElement> options = element.findElements( By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]")); boolean matched = false; for (WebElement option : options) { setSelected(option, true); if (!isMultiple()) { return; } matched = true; } if (options.isEmpty() && text.contains(" ")) { String subStringWithoutSpace = getLongestSubstringWithoutSpace(text); List<WebElement> candidates; if ("".equals(subStringWithoutSpace)) { // hmm, text is either empty or contains only spaces - get all options ... candidates = element.findElements(By.tagName("option")); } else { // get candidates via XPATH ... candidates = element.findElements( By.xpath(".//option[contains(., " + Quotes.escape(subStringWithoutSpace) + ")]")); } for (WebElement option : candidates) { if (text.equals(option.getText())) { setSelected(option, true); if (!isMultiple()) { return; } matched = true; } } } if (!matched) { throw new NoSuchElementException("Cannot locate element with text: " + text); } }
/** * Deselect all options that have a value matching the argument. That is, when given "foo" this * would deselect an option like: * * <p><option value="foo">Bar</option> * * @param value The value to match against * @throws NoSuchElementException If no matching option elements are found * @throws UnsupportedOperationException If the SELECT does not support multiple selections */ public void deselectByValue(String value) { if (!isMultiple()) { throw new UnsupportedOperationException("You may only deselect options of a multi-select"); } List<WebElement> options = element.findElements(By.xpath(".//option[@value = " + Quotes.escape(value) + "]")); boolean matched = false; for (WebElement option : options) { setSelected(option, false); matched = true; } if (!matched) { throw new NoSuchElementException("Cannot locate option with value: " + value); } }
/** * Select all options that have a value matching the argument. That is, when given "foo" this * would select an option like: * * <p><option value="foo">Bar</option> * * @param value The value to match against * @throws NoSuchElementException If no matching option elements are found */ public void selectByValue(String value) { List<WebElement> options = element.findElements(By.xpath(".//option[@value = " + Quotes.escape(value) + "]")); boolean matched = false; for (WebElement option : options) { setSelected(option, true); if (!isMultiple()) { return; } matched = true; } if (!matched) { throw new NoSuchElementException("Cannot locate option with value: " + value); } }
/** * Deselect all options that display text matching the argument. That is, when given "Bar" this * would deselect an option like: * * <p><option value="foo">Bar</option> * * @param text The visible text to match against * @throws NoSuchElementException If no matching option elements are found * @throws UnsupportedOperationException If the SELECT does not support multiple selections */ public void deselectByVisibleText(String text) { if (!isMultiple()) { throw new UnsupportedOperationException("You may only deselect options of a multi-select"); } List<WebElement> options = element.findElements( By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]")); boolean matched = false; for (WebElement option : options) { setSelected(option, false); matched = true; } if (!matched) { throw new NoSuchElementException("Cannot locate element with text: " + text); } }
public boolean isNotLast() { return getNumber() != quotes.size(); }
boolean hasAtLeastTwoQuote() { return quotes.size() > 1; }
private List<Quote> quotes() { return quotes.getQuotes(); }