A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/seleniumhq/selenium/commit/25b30ffa6657f263953e628db6fd6a80fe0f981e below:

[java] only allow enabled select lists for Select class · SeleniumHQ/selenium@25b30ff · GitHub

File tree Expand file treeCollapse file tree 3 files changed

+40

-30

lines changed

Filter options

Expand file treeCollapse file tree 3 files changed

+40

-30

lines changed Original file line number Diff line number Diff line change

@@ -45,10 +45,14 @@ public class Select implements ISelect, WrapsElement {

45 45

public Select(WebElement element) {

46 46

String tagName = element.getTagName();

47 47 48 -

if (null == tagName || !"select".equals(tagName.toLowerCase())) {

48 +

if (!"select".equalsIgnoreCase(tagName)) {

49 49

throw new UnexpectedTagNameException("select", tagName);

50 50

}

51 51 52 +

if (!element.isEnabled()) {

53 +

throw new UnsupportedOperationException("Select element is disabled and may not be used.");

54 +

}

55 + 52 56

this.element = element;

53 57 54 58

String value = element.getDomAttribute("multiple");

@@ -109,8 +113,6 @@ public WebElement getFirstSelectedOption() {

109 113

*/

110 114

@Override

111 115

public void selectByVisibleText(String text) {

112 -

assertSelectIsEnabled();

113 - 114 116

// try to find the option via XPATH ...

115 117

List<WebElement> options =

116 118

element.findElements(By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));

@@ -175,7 +177,6 @@ private String getLongestSubstringWithoutSpace(String s) {

175 177

*/

176 178

@Override

177 179

public void selectByIndex(int index) {

178 -

assertSelectIsEnabled();

179 180

setSelectedByIndex(index, true);

180 181

}

181 182

@@ -190,7 +191,6 @@ public void selectByIndex(int index) {

190 191

*/

191 192

@Override

192 193

public void selectByValue(String value) {

193 -

assertSelectIsEnabled();

194 194

for (WebElement option : findOptionsByValue(value)) {

195 195

setSelected(option, true);

196 196

if (!isMultiple()) {

@@ -327,12 +327,6 @@ private void assertOptionIsEnabled(WebElement option, boolean select) {

327 327

}

328 328

}

329 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 - 336 330

@Override

337 331

public boolean equals(Object o) {

338 332

if (!(o instanceof Select)) {

Original file line number Diff line number Diff line change

@@ -43,6 +43,13 @@ public void shouldThrowAnExceptionIfTheElementIsNotASelectElement() {

43 43

.isThrownBy(() -> new Select(selectElement));

44 44

}

45 45 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 + 46 53

@Test

47 54

public void shouldIndicateThatASelectCanSupportMultipleOptions() {

48 55

WebElement selectElement = driver.findElement(By.name("multi"));

@@ -85,7 +92,6 @@ public void shouldReturnAllOptionsWhenAsked() {

85 92 86 93

assertThat(select.getOptions()).extracting(WebElement::getText)

87 94

.containsExactly("One", "Two", "Four", "Still learning how to count, apparently");

88 - 89 95

}

90 96 91 97

@Test

@@ -149,7 +155,16 @@ public void shouldThrowExceptionOnSelectByVisibleTextIfOptionDoesNotExist() {

149 155

Select select = new Select(selectElement);

150 156 151 157

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"));

153 168

}

154 169 155 170

@Test

@@ -170,6 +185,15 @@ public void shouldThrowExceptionOnSelectByIndexIfOptionDoesNotExist() {

170 185

.isThrownBy(() -> select.selectByIndex(10));

171 186

}

172 187 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 + 173 197

@Test

174 198

public void shouldAllowOptionsToBeSelectedByReturnedValue() {

175 199

WebElement selectElement = driver.findElement(By.name("select_empty_multiple"));

@@ -188,6 +212,15 @@ public void shouldThrowExceptionOnSelectByReturnedValueIfOptionDoesNotExist() {

188 212

.isThrownBy(() -> select.selectByValue("not there"));

189 213

}

190 214 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 + 191 224

@Test

192 225

public void shouldAllowUserToDeselectAllWhenSelectSupportsMultipleSelections() {

193 226

WebElement selectElement = driver.findElement(By.name("multi"));

Original file line number Diff line number Diff line change

@@ -172,23 +172,6 @@ public void shouldNotAllowDisabledOptionsToBeSelected() {

172 172

verify(firstOption, never()).click();

173 173

}

174 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 - 192 175

@Test

193 176

public void shouldAllowOptionsToBeSelectedByIndex() {

194 177

final WebElement firstOption = mockOption("first", true, 0);

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4