diff --git a/.idea/misc.xml b/.idea/misc.xml index c052579f90a0f..43262c34163c2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,4 +6,4 @@ - \ No newline at end of file + diff --git a/java/src/org/openqa/selenium/support/ui/Select.java b/java/src/org/openqa/selenium/support/ui/Select.java index 264585e5417f6..9942b7057832e 100644 --- a/java/src/org/openqa/selenium/support/ui/Select.java +++ b/java/src/org/openqa/selenium/support/ui/Select.java @@ -123,6 +123,7 @@ public WebElement getFirstSelectedOption() { @Override public void selectByVisibleText(String text) { assertSelectIsEnabled(); + assertSelectIsVisible(); // try to find the option via XPATH ... List options = @@ -130,6 +131,8 @@ public void selectByVisibleText(String text) { 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; @@ -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; diff --git a/java/test/org/openqa/selenium/support/ui/SelectElementTest.java b/java/test/org/openqa/selenium/support/ui/SelectElementTest.java index 0587b630b3261..729f902b753e8 100644 --- a/java/test/org/openqa/selenium/support/ui/SelectElementTest.java +++ b/java/test/org/openqa/selenium/support/ui/SelectElementTest.java @@ -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; @@ -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 options = Arrays.asList("Apples", "Pears", "Oranges", "Lemons"); + options.forEach( + option -> + assertThatExceptionOfType(NoSuchElementException.class) + .isThrownBy(() -> select.selectByVisibleText(option))); } @Test @@ -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 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"));