Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] feat: Add method to select options containing the provided text #14426

Merged
merged 14 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions java/src/org/openqa/selenium/support/ui/ISelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public interface ISelect {
*/
void selectByVisibleText(String text);

/**
* Select all options that display text matching or containing the argument. That is, when given "Bar"
* this would select an option like:
*
* <p>&lt;option value="foo"&gt;Bar&lt;/option&gt;
*
* Additionally, if no exact match is found, this will attempt to select options that contain the
* argument as a substring. For example, when given "1년", this would select an option like:
*
* <p>&lt;option value="bar"&gt;1년납&lt;/option&gt;
*
* @param text The visible text to match or partially match against
*/
void selectByContainsVisibleText(String text);

/**
* Select the option at the given index. This is done by examining the "index" attribute of an
* element, and not merely by counting.
Expand Down
61 changes: 61 additions & 0 deletions java/src/org/openqa/selenium/support/ui/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,67 @@ public void selectByVisibleText(String text) {
}
}

/**
* Selects all options that display text matching or containing the provided argument.
* This method first attempts to find an exact match and, if not found, will then attempt
* to find options that contain the specified text as a substring.
*
* For example, when given "Bar", this would select an option like:
*
* <p>&lt;option value="foo"&gt;Bar&lt;/option&gt;
*
* And also select an option like:
*
* <p>&lt;option value="baz"&gt;FooBar&lt;/option&gt; or &lt;option value="baz"&gt;1년납&lt;/option&gt; when "1년" is provided.
*
* @param text The visible text to match against. It can be a full or partial match of the option text.
* @throws NoSuchElementException If no matching option elements are found
*/
@Override
public void selectByContainsVisibleText(String text) {
assertSelectIsEnabled();

// try to find the option via XPATH ...
List<WebElement> options =
element.findElements(
By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));

for (WebElement option : options) {
setSelected(option, true);
if (!isMultiple()) {
return;
}
}

boolean matched = !options.isEmpty();
if (!matched) {
String searchText = text.contains(" ") ? getLongestSubstringWithoutSpace(text) : text;

List<WebElement> candidates;
if (searchText.isEmpty()) {
candidates = element.findElements(By.tagName("option"));
} else {
candidates = element.findElements(
By.xpath(".//option[contains(., " + Quotes.escape(searchText) + ")]"));
}

String trimmed = text.trim();
for (WebElement option : candidates) {
if (option.getText().contains(trimmed)) {
setSelected(option, true);
if (!isMultiple()) {
return;
}
matched = true;
}
}
}

if (!matched) {
throw new NoSuchElementException("Cannot locate option with text: " + text);
}
}

private String getLongestSubstringWithoutSpace(String s) {
String result = "";
StringTokenizer st = new StringTokenizer(s, " ");
Expand Down
17 changes: 17 additions & 0 deletions java/test/org/openqa/selenium/support/ui/SelectElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ void shouldAllowOptionsToBeSelectedByVisibleText() {
assertThat(firstSelected.getText()).isEqualTo("select_2");
}

@Test
void shouldAllowOptionsToBeSelectedByContainsVisibleText() {
WebElement selectElement = driver.findElement(By.name("select_empty_multiple"));

Select select = new Select(selectElement);
select.selectByContainsVisibleText("select");
WebElement firstSelected = select.getFirstSelectedOption();
int selectedOptionCount = select.getAllSelectedOptions().size();

assertThat(firstSelected.getText()).isEqualTo("select_1");
assertThat(selectedOptionCount).isEqualTo(4);

select.deselectAll();
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> select.selectByContainsVisibleText("select_12"));
}

@Test
@Ignore(ALL)
public void shouldNotAllowInvisibleOptionsToBeSelectedByVisibleText() {
Expand Down
22 changes: 22 additions & 0 deletions java/test/org/openqa/selenium/support/ui/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -154,6 +155,24 @@ void shouldAllowOptionsToBeSelectedByVisibleText() {
verify(firstOption).click();
}

@Test
void shouldAllowOptionsToBeSelectedByContainsVisibleText() {
String parameterText = "foo";

final WebElement firstOption = mockOption("first", false);

final WebElement element = mockSelectWebElement("multiple");
when(element.findElements(By.xpath(".//option[contains(., " + Quotes.escape(parameterText) + ")]")))
.thenReturn(Collections.singletonList(firstOption));
when(firstOption.getText()).thenReturn("foo bar");
when(firstOption.isEnabled()).thenReturn(true);

Select select = new Select(element);
select.selectByContainsVisibleText(parameterText);

verify(firstOption).click();
}

@Test
void shouldNotAllowDisabledOptionsToBeSelected() {
final WebElement firstOption = mockOption("first", false);
Expand Down Expand Up @@ -302,5 +321,8 @@ void shouldThrowAnExceptionIfThereAreNoElementsToSelect() {

assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> select.selectByVisibleText("also not there"));

assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> select.selectByContainsVisibleText("also not there"));
}
}