Skip to content

Commit 5afb77e

Browse files
vicky-ivdiemol
andauthored
[java] Unifying select class (#16220)
* Updated Java Language Level to the JDK_17 for IntelliJ IDEA * [java] Fix select being able to select options hidden by css rules Fixes #15265 * [java] Formatted with ./scripts/format.sh * [java] Change the JDK setting for IntelliJ back to JDK 11 --------- Co-authored-by: Diego Molina <[email protected]>
1 parent 8a3bebc commit 5afb77e

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/src/org/openqa/selenium/support/ui/Select.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,16 @@ public WebElement getFirstSelectedOption() {
123123
@Override
124124
public void selectByVisibleText(String text) {
125125
assertSelectIsEnabled();
126+
assertSelectIsVisible();
126127

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

132133
for (WebElement option : options) {
134+
if (!hasCssPropertyAndVisible(option))
135+
throw new NoSuchElementException("Invisible option with text: " + text);
133136
setSelected(option, true);
134137
if (!isMultiple()) {
135138
return;
@@ -154,6 +157,8 @@ public void selectByVisibleText(String text) {
154157

155158
for (WebElement option : candidates) {
156159
if (trimmed.equals(option.getText().trim())) {
160+
if (!hasCssPropertyAndVisible(option))
161+
throw new NoSuchElementException("Invisible option with text: " + text);
157162
setSelected(option, true);
158163
if (!isMultiple()) {
159164
return;

java/test/org/openqa/selenium/support/ui/SelectElementTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2222
import static org.openqa.selenium.testing.drivers.Browser.ALL;
2323

24+
import java.util.Arrays;
25+
import java.util.List;
2426
import org.junit.jupiter.api.BeforeEach;
2527
import org.junit.jupiter.api.Test;
2628
import org.openqa.selenium.By;
@@ -176,8 +178,11 @@ void shouldNotAllowInvisibleOptionsToBeSelectedByContainsVisibleText() {
176178
WebElement selectElement = driver.findElement(By.id("invisible_multi_select"));
177179
Select select = new Select(selectElement);
178180

179-
assertThatExceptionOfType(NoSuchElementException.class)
180-
.isThrownBy(() -> select.selectByContainsVisibleText("Apples"));
181+
List<String> options = Arrays.asList("Apples", "Pears", "Oranges", "Lemons");
182+
options.forEach(
183+
option ->
184+
assertThatExceptionOfType(NoSuchElementException.class)
185+
.isThrownBy(() -> select.selectByVisibleText(option)));
181186
}
182187

183188
@Test
@@ -198,6 +203,18 @@ void shouldThrowExceptionOnSelectByVisibleTextIfOptionDisabled() {
198203
.isThrownBy(() -> select.selectByVisibleText("Disabled"));
199204
}
200205

206+
@Test
207+
void shouldThrowExceptionOnSelectByVisibleTextIfOptionHidden() {
208+
WebElement selectElement = driver.findElement(By.id("invisible_multi_select"));
209+
Select select = new Select(selectElement);
210+
211+
List<String> options = Arrays.asList("Apples", "Pears", "Oranges", "Lemons");
212+
options.forEach(
213+
option ->
214+
assertThatExceptionOfType(NoSuchElementException.class)
215+
.isThrownBy(() -> select.selectByVisibleText(option)));
216+
}
217+
201218
@Test
202219
void shouldAllowOptionsToBeSelectedByIndex() {
203220
WebElement selectElement = driver.findElement(By.name("select_empty_multiple"));

0 commit comments

Comments
 (0)