Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions .idea/misc.xml

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

2 changes: 1 addition & 1 deletion java/java.iml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<component name="ModuleRunConfigurationManager">
<shared />
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_17">
<output url="file://$MODULE_DIR$/build/production" />
<output-test url="file://$MODULE_DIR$/build/test" />
<exclude-output />
Expand Down
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
22 changes: 20 additions & 2 deletions java/test/org/openqa/selenium/support/ui/SelectElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JupiterTestBase;

import java.util.Arrays;
import java.util.List;

class SelectElementTest extends JupiterTestBase {

@BeforeEach
Expand Down Expand Up @@ -176,8 +179,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 +204,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