Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions java/src/org/openqa/selenium/support/ui/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,16 @@ public WebElement getFirstSelectedOption() {
@Override
public void selectByVisibleText(String text) {
assertSelectIsEnabled();
assertSelectIsVisible();

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

for (WebElement option : options) {
if (!hasCssPropertyAndVisible(option))
throw new NoSuchElementException("Invisible option with text: " + text);
setSelected(option, true);
if (!isMultiple()) {
return;
Expand All @@ -154,6 +157,8 @@ public void selectByVisibleText(String text) {

for (WebElement option : candidates) {
if (trimmed.equals(option.getText().trim())) {
if (!hasCssPropertyAndVisible(option))
throw new NoSuchElementException("Invisible option with text: " + text);
setSelected(option, true);
if (!isMultiple()) {
return;
Expand Down
21 changes: 19 additions & 2 deletions java/test/org/openqa/selenium/support/ui/SelectElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.openqa.selenium.testing.drivers.Browser.ALL;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
Expand Down Expand Up @@ -176,8 +178,11 @@ void shouldNotAllowInvisibleOptionsToBeSelectedByContainsVisibleText() {
WebElement selectElement = driver.findElement(By.id("invisible_multi_select"));
Select select = new Select(selectElement);

assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> select.selectByContainsVisibleText("Apples"));
List<String> options = Arrays.asList("Apples", "Pears", "Oranges", "Lemons");
options.forEach(
option ->
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> select.selectByVisibleText(option)));
}

@Test
Expand All @@ -198,6 +203,18 @@ void shouldThrowExceptionOnSelectByVisibleTextIfOptionDisabled() {
.isThrownBy(() -> select.selectByVisibleText("Disabled"));
}

@Test
void shouldThrowExceptionOnSelectByVisibleTextIfOptionHidden() {
WebElement selectElement = driver.findElement(By.id("invisible_multi_select"));
Select select = new Select(selectElement);

List<String> options = Arrays.asList("Apples", "Pears", "Oranges", "Lemons");
options.forEach(
option ->
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> select.selectByVisibleText(option)));
}

@Test
void shouldAllowOptionsToBeSelectedByIndex() {
WebElement selectElement = driver.findElement(By.name("select_empty_multiple"));
Expand Down
Loading