Skip to content

Commit

Permalink
Revert "[java] only allow enabled select lists for Select class"
Browse files Browse the repository at this point in the history
This reverts commit 25b30ff
  • Loading branch information
titusfortner committed Oct 28, 2022
1 parent bbc1663 commit aeb585b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
16 changes: 11 additions & 5 deletions java/src/org/openqa/selenium/support/ui/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ public class Select implements ISelect, WrapsElement {
public Select(WebElement element) {
String tagName = element.getTagName();

if (!"select".equalsIgnoreCase(tagName)) {
if (null == tagName || !"select".equals(tagName.toLowerCase())) {
throw new UnexpectedTagNameException("select", tagName);
}

if (!element.isEnabled()) {
throw new UnsupportedOperationException("Select element is disabled and may not be used.");
}

this.element = element;

String value = element.getDomAttribute("multiple");
Expand Down Expand Up @@ -113,6 +109,8 @@ public WebElement getFirstSelectedOption() {
*/
@Override
public void selectByVisibleText(String text) {
assertSelectIsEnabled();

// try to find the option via XPATH ...
List<WebElement> options =
element.findElements(By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));
Expand Down Expand Up @@ -177,6 +175,7 @@ private String getLongestSubstringWithoutSpace(String s) {
*/
@Override
public void selectByIndex(int index) {
assertSelectIsEnabled();
setSelectedByIndex(index, true);
}

Expand All @@ -191,6 +190,7 @@ public void selectByIndex(int index) {
*/
@Override
public void selectByValue(String value) {
assertSelectIsEnabled();
for (WebElement option : findOptionsByValue(value)) {
setSelected(option, true);
if (!isMultiple()) {
Expand Down Expand Up @@ -327,6 +327,12 @@ private void assertOptionIsEnabled(WebElement option, boolean select) {
}
}

private void assertSelectIsEnabled() {
if (!element.isEnabled()) {
throw new UnsupportedOperationException("You may not select an option in disabled select");
}
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Select)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ void shouldThrowAnExceptionIfTheElementIsNotASelectElement() {
.isThrownBy(() -> new Select(selectElement));
}

@Test
void shouldThrowAnExceptionIfTheElementIsDisabled() {
WebElement selectElement = driver.findElement(By.name("no-select"));
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> new Select(selectElement));
}

@Test
void shouldIndicateThatASelectCanSupportMultipleOptions() {
WebElement selectElement = driver.findElement(By.name("multi"));
Expand Down

0 comments on commit aeb585b

Please sign in to comment.