Skip to content

Commit ff88671

Browse files
asolntsevdiemol
andauthored
#10812 don't allow selecting a disabled option (#10814)
Co-authored-by: Diego Molina <[email protected]>
1 parent 4b19fa4 commit ff88671

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public WebElement getFirstSelectedOption() {
109109
*/
110110
@Override
111111
public void selectByVisibleText(String text) {
112+
assertSelectIsEnabled();
113+
112114
// try to find the option via XPATH ...
113115
List<WebElement> options =
114116
element.findElements(By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));
@@ -173,6 +175,7 @@ private String getLongestSubstringWithoutSpace(String s) {
173175
*/
174176
@Override
175177
public void selectByIndex(int index) {
178+
assertSelectIsEnabled();
176179
setSelectedByIndex(index, true);
177180
}
178181

@@ -187,6 +190,7 @@ public void selectByIndex(int index) {
187190
*/
188191
@Override
189192
public void selectByValue(String value) {
193+
assertSelectIsEnabled();
190194
for (WebElement option : findOptionsByValue(value)) {
191195
setSelected(option, true);
192196
if (!isMultiple()) {
@@ -311,11 +315,24 @@ private void setSelectedByIndex(int index, boolean select) {
311315
* deselected (false)
312316
*/
313317
private void setSelected(WebElement option, boolean select) {
318+
assertOptionIsEnabled(option, select);
314319
if (option.isSelected() != select) {
315320
option.click();
316321
}
317322
}
318323

324+
private void assertOptionIsEnabled(WebElement option, boolean select) {
325+
if (select && !option.isEnabled()) {
326+
throw new UnsupportedOperationException("You may not select a disabled option");
327+
}
328+
}
329+
330+
private void assertSelectIsEnabled() {
331+
if (!element.isEnabled()) {
332+
throw new UnsupportedOperationException("You may not select an option in disabled select");
333+
}
334+
}
335+
319336
@Override
320337
public boolean equals(Object o) {
321338
if (!(o instanceof Select)) {

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

+39
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.Collections.emptyList;
2121
import static org.assertj.core.api.Assertions.assertThat;
2222
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
23+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2324
import static org.mockito.Mockito.mock;
2425
import static org.mockito.Mockito.never;
2526
import static org.mockito.Mockito.verify;
@@ -74,6 +75,7 @@ private WebElement mockSelectWebElement(String multiple) {
7475
final WebElement element = mock(WebElement.class);
7576
when(element.getTagName()).thenReturn("select");
7677
when(element.getDomAttribute("multiple")).thenReturn(multiple);
78+
when(element.isEnabled()).thenReturn(true);
7779
return element;
7880
}
7981

@@ -93,12 +95,14 @@ public void shouldReturnAllOptionsWhenAsked() {
9395

9496
private WebElement mockOption(String name, boolean isSelected) {
9597
final WebElement optionBad = mock(WebElement.class, name);
98+
when(optionBad.isEnabled()).thenReturn(true);
9699
when(optionBad.isSelected()).thenReturn(isSelected);
97100
return optionBad;
98101
}
99102

100103
private WebElement mockOption(String name, boolean isSelected, int index) {
101104
WebElement option = mockOption(name, isSelected);
105+
when(option.isEnabled()).thenReturn(true);
102106
when(option.getAttribute("index")).thenReturn(String.valueOf(index));
103107
return option;
104108
}
@@ -151,6 +155,40 @@ public void shouldAllowOptionsToBeSelectedByVisibleText() {
151155
verify(firstOption).click();
152156
}
153157

158+
@Test
159+
public void shouldNotAllowDisabledOptionsToBeSelected() {
160+
final WebElement firstOption = mockOption("first", false);
161+
when(firstOption.isEnabled()).thenReturn(false);
162+
163+
final WebElement element = mockSelectWebElement("multiple");
164+
when(element.findElements(By.xpath(".//option[normalize-space(.) = \"fish\"]")))
165+
.thenReturn(Collections.singletonList(firstOption));
166+
167+
Select select = new Select(element);
168+
assertThatThrownBy(() -> select.selectByVisibleText("fish"))
169+
.isInstanceOf(UnsupportedOperationException.class)
170+
.hasMessage("You may not select a disabled option");
171+
172+
verify(firstOption, never()).click();
173+
}
174+
175+
@Test
176+
public void shouldNotAllowOptionsToBeSelectedInDisabledSelect() {
177+
final WebElement firstOption = mockOption("first", false);
178+
179+
final WebElement element = mockSelectWebElement("multiple");
180+
when(element.isEnabled()).thenReturn(false);
181+
when(element.findElements(By.xpath(".//option[normalize-space(.) = \"fish\"]")))
182+
.thenReturn(Collections.singletonList(firstOption));
183+
184+
Select select = new Select(element);
185+
assertThatThrownBy(() -> select.selectByVisibleText("fish"))
186+
.isInstanceOf(UnsupportedOperationException.class)
187+
.hasMessage("You may not select an option in disabled select");
188+
189+
verify(firstOption, never()).click();
190+
}
191+
154192
@Test
155193
public void shouldAllowOptionsToBeSelectedByIndex() {
156194
final WebElement firstOption = mockOption("first", true, 0);
@@ -252,6 +290,7 @@ public void shouldFallBackToSlowLooksUpsWhenGetByVisibleTextFailsAndThereIsASpac
252290
when(element.findElements(xpath1)).thenReturn(emptyList());
253291
when(element.findElements(xpath2)).thenReturn(Collections.singletonList(firstOption));
254292
when(firstOption.getText()).thenReturn("foo bar");
293+
when(firstOption.isEnabled()).thenReturn(true);
255294

256295
Select select = new Select(element);
257296
select.selectByVisibleText("foo bar");

0 commit comments

Comments
 (0)