Skip to content

Commit 25b30ff

Browse files
committed
[java] only allow enabled select lists for Select class
1 parent b0925af commit 25b30ff

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

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

+5-11
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ public class Select implements ISelect, WrapsElement {
4545
public Select(WebElement element) {
4646
String tagName = element.getTagName();
4747

48-
if (null == tagName || !"select".equals(tagName.toLowerCase())) {
48+
if (!"select".equalsIgnoreCase(tagName)) {
4949
throw new UnexpectedTagNameException("select", tagName);
5050
}
5151

52+
if (!element.isEnabled()) {
53+
throw new UnsupportedOperationException("Select element is disabled and may not be used.");
54+
}
55+
5256
this.element = element;
5357

5458
String value = element.getDomAttribute("multiple");
@@ -109,8 +113,6 @@ public WebElement getFirstSelectedOption() {
109113
*/
110114
@Override
111115
public void selectByVisibleText(String text) {
112-
assertSelectIsEnabled();
113-
114116
// try to find the option via XPATH ...
115117
List<WebElement> options =
116118
element.findElements(By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));
@@ -175,7 +177,6 @@ private String getLongestSubstringWithoutSpace(String s) {
175177
*/
176178
@Override
177179
public void selectByIndex(int index) {
178-
assertSelectIsEnabled();
179180
setSelectedByIndex(index, true);
180181
}
181182

@@ -190,7 +191,6 @@ public void selectByIndex(int index) {
190191
*/
191192
@Override
192193
public void selectByValue(String value) {
193-
assertSelectIsEnabled();
194194
for (WebElement option : findOptionsByValue(value)) {
195195
setSelected(option, true);
196196
if (!isMultiple()) {
@@ -327,12 +327,6 @@ private void assertOptionIsEnabled(WebElement option, boolean select) {
327327
}
328328
}
329329

330-
private void assertSelectIsEnabled() {
331-
if (!element.isEnabled()) {
332-
throw new UnsupportedOperationException("You may not select an option in disabled select");
333-
}
334-
}
335-
336330
@Override
337331
public boolean equals(Object o) {
338332
if (!(o instanceof Select)) {

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public void shouldThrowAnExceptionIfTheElementIsNotASelectElement() {
4343
.isThrownBy(() -> new Select(selectElement));
4444
}
4545

46+
@Test
47+
public void shouldThrowAnExceptionIfTheElementIsDisabled() {
48+
WebElement selectElement = driver.findElement(By.name("no-select"));
49+
assertThatExceptionOfType(UnsupportedOperationException.class)
50+
.isThrownBy(() -> new Select(selectElement));
51+
}
52+
4653
@Test
4754
public void shouldIndicateThatASelectCanSupportMultipleOptions() {
4855
WebElement selectElement = driver.findElement(By.name("multi"));
@@ -85,7 +92,6 @@ public void shouldReturnAllOptionsWhenAsked() {
8592

8693
assertThat(select.getOptions()).extracting(WebElement::getText)
8794
.containsExactly("One", "Two", "Four", "Still learning how to count, apparently");
88-
8995
}
9096

9197
@Test
@@ -149,7 +155,16 @@ public void shouldThrowExceptionOnSelectByVisibleTextIfOptionDoesNotExist() {
149155
Select select = new Select(selectElement);
150156

151157
assertThatExceptionOfType(NoSuchElementException.class)
152-
.isThrownBy(() -> select.selectByVisibleText("not there"));
158+
.isThrownBy(() -> select.selectByVisibleText("not there"));
159+
}
160+
161+
@Test
162+
public void shouldThrowExceptionOnSelectByVisibleTextIfOptionDisabled() {
163+
WebElement selectElement = driver.findElement(By.name("single_disabled"));
164+
Select select = new Select(selectElement);
165+
166+
assertThatExceptionOfType(UnsupportedOperationException.class)
167+
.isThrownBy(() -> select.selectByVisibleText("Disabled"));
153168
}
154169

155170
@Test
@@ -170,6 +185,15 @@ public void shouldThrowExceptionOnSelectByIndexIfOptionDoesNotExist() {
170185
.isThrownBy(() -> select.selectByIndex(10));
171186
}
172187

188+
@Test
189+
public void shouldThrowExceptionOnSelectByIndexIfOptionDisabled() {
190+
WebElement selectElement = driver.findElement(By.name("single_disabled"));
191+
Select select = new Select(selectElement);
192+
193+
assertThatExceptionOfType(UnsupportedOperationException.class)
194+
.isThrownBy(() -> select.selectByIndex(1));
195+
}
196+
173197
@Test
174198
public void shouldAllowOptionsToBeSelectedByReturnedValue() {
175199
WebElement selectElement = driver.findElement(By.name("select_empty_multiple"));
@@ -188,6 +212,15 @@ public void shouldThrowExceptionOnSelectByReturnedValueIfOptionDoesNotExist() {
188212
.isThrownBy(() -> select.selectByValue("not there"));
189213
}
190214

215+
@Test
216+
public void shouldThrowExceptionOnSelectByReturnedValueIfOptionDisabled() {
217+
WebElement selectElement = driver.findElement(By.name("single_disabled"));
218+
Select select = new Select(selectElement);
219+
220+
assertThatExceptionOfType(UnsupportedOperationException.class)
221+
.isThrownBy(() -> select.selectByValue("disabled"));
222+
}
223+
191224
@Test
192225
public void shouldAllowUserToDeselectAllWhenSelectSupportsMultipleSelections() {
193226
WebElement selectElement = driver.findElement(By.name("multi"));

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

-17
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,6 @@ public void shouldNotAllowDisabledOptionsToBeSelected() {
172172
verify(firstOption, never()).click();
173173
}
174174

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-
192175
@Test
193176
public void shouldAllowOptionsToBeSelectedByIndex() {
194177
final WebElement firstOption = mockOption("first", true, 0);

0 commit comments

Comments
 (0)