Skip to content

Commit 1f795e0

Browse files
valfirstshs96c
authored andcommitted
[java] Simplify and refactor Select element wrapper (#6501)
Make use of Java 8 streams too.
1 parent 29ee9e9 commit 1f795e0

File tree

1 file changed

+33
-62
lines changed
  • java/client/src/org/openqa/selenium/support/ui

1 file changed

+33
-62
lines changed

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

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import org.openqa.selenium.NoSuchElementException;
2222
import org.openqa.selenium.WebElement;
2323

24-
import java.util.ArrayList;
2524
import java.util.List;
2625
import java.util.StringTokenizer;
26+
import java.util.stream.Collectors;
2727

2828
/**
2929
* Models a SELECT tag, providing helper methods to select and deselect options.
@@ -77,15 +77,7 @@ public List<WebElement> getOptions() {
7777
*/
7878
@Override
7979
public List<WebElement> getAllSelectedOptions() {
80-
List<WebElement> toReturn = new ArrayList<>();
81-
82-
for (WebElement option : getOptions()) {
83-
if (option.isSelected()) {
84-
toReturn.add(option);
85-
}
86-
}
87-
88-
return toReturn;
80+
return getOptions().stream().filter(WebElement::isSelected).collect(Collectors.toList());
8981
}
9082

9183
/**
@@ -95,13 +87,8 @@ public List<WebElement> getAllSelectedOptions() {
9587
*/
9688
@Override
9789
public WebElement getFirstSelectedOption() {
98-
for (WebElement option : getOptions()) {
99-
if (option.isSelected()) {
100-
return option;
101-
}
102-
}
103-
104-
throw new NoSuchElementException("No options are selected");
90+
return getOptions().stream().filter(WebElement::isSelected).findFirst()
91+
.orElseThrow(() -> new NoSuchElementException("No options are selected"));
10592
}
10693

10794
/**
@@ -119,16 +106,15 @@ public void selectByVisibleText(String text) {
119106
List<WebElement> options =
120107
element.findElements(By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));
121108

122-
boolean matched = false;
123109
for (WebElement option : options) {
124110
setSelected(option, true);
125111
if (!isMultiple()) {
126112
return;
127113
}
128-
matched = true;
129114
}
130115

131-
if (options.isEmpty() && text.contains(" ")) {
116+
boolean matched = !options.isEmpty();
117+
if (!matched && text.contains(" ")) {
132118
String subStringWithoutSpace = getLongestSubstringWithoutSpace(text);
133119
List<WebElement> candidates;
134120
if ("".equals(subStringWithoutSpace)) {
@@ -177,15 +163,7 @@ private String getLongestSubstringWithoutSpace(String s) {
177163
*/
178164
@Override
179165
public void selectByIndex(int index) {
180-
String match = String.valueOf(index);
181-
182-
for (WebElement option : getOptions()) {
183-
if (match.equals(option.getAttribute("index"))) {
184-
setSelected(option, true);
185-
return;
186-
}
187-
}
188-
throw new NoSuchElementException("Cannot locate option with index: " + index);
166+
setSelectedByIndex(index, true);
189167
}
190168

191169
/**
@@ -199,20 +177,11 @@ public void selectByIndex(int index) {
199177
*/
200178
@Override
201179
public void selectByValue(String value) {
202-
List<WebElement> options = element.findElements(By.xpath(
203-
".//option[@value = " + Quotes.escape(value) + "]"));
204-
205-
boolean matched = false;
206-
for (WebElement option : options) {
180+
for (WebElement option : findOptionsByValue(value)) {
207181
setSelected(option, true);
208182
if (!isMultiple()) {
209183
return;
210184
}
211-
matched = true;
212-
}
213-
214-
if (!matched) {
215-
throw new NoSuchElementException("Cannot locate option with value: " + value);
216185
}
217186
}
218187

@@ -250,15 +219,8 @@ public void deselectByValue(String value) {
250219
"You may only deselect options of a multi-select");
251220
}
252221

253-
List<WebElement> options = element.findElements(By.xpath(
254-
".//option[@value = " + Quotes.escape(value) + "]"));
255-
boolean matched = false;
256-
for (WebElement option : options) {
222+
for (WebElement option : findOptionsByValue(value)) {
257223
setSelected(option, false);
258-
matched = true;
259-
}
260-
if (!matched) {
261-
throw new NoSuchElementException("Cannot locate option with value: " + value);
262224
}
263225
}
264226

@@ -277,15 +239,7 @@ public void deselectByIndex(int index) {
277239
"You may only deselect options of a multi-select");
278240
}
279241

280-
String match = String.valueOf(index);
281-
282-
for (WebElement option : getOptions()) {
283-
if (match.equals(option.getAttribute("index"))) {
284-
setSelected(option, false);
285-
return;
286-
}
287-
}
288-
throw new NoSuchElementException("Cannot locate option with index: " + index);
242+
setSelectedByIndex(index, false);
289243
}
290244

291245
/**
@@ -307,16 +261,34 @@ public void deselectByVisibleText(String text) {
307261

308262
List<WebElement> options = element.findElements(By.xpath(
309263
".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));
264+
if (options.isEmpty()) {
265+
throw new NoSuchElementException("Cannot locate element with text: " + text);
266+
}
310267

311-
boolean matched = false;
312268
for (WebElement option : options) {
313269
setSelected(option, false);
314-
matched = true;
315270
}
271+
}
316272

317-
if (!matched) {
318-
throw new NoSuchElementException("Cannot locate element with text: " + text);
273+
private List<WebElement> findOptionsByValue(String value) {
274+
List<WebElement> options = element.findElements(By.xpath(
275+
".//option[@value = " + Quotes.escape(value) + "]"));
276+
if (options.isEmpty()) {
277+
throw new NoSuchElementException("Cannot locate option with value: " + value);
319278
}
279+
return options;
280+
}
281+
282+
private void setSelectedByIndex(int index, boolean select) {
283+
String match = String.valueOf(index);
284+
285+
for (WebElement option : getOptions()) {
286+
if (match.equals(option.getAttribute("index"))) {
287+
setSelected(option, select);
288+
return;
289+
}
290+
}
291+
throw new NoSuchElementException("Cannot locate option with index: " + index);
320292
}
321293

322294
/**
@@ -329,8 +301,7 @@ public void deselectByVisibleText(String text) {
329301
* deselected (false)
330302
*/
331303
private void setSelected(WebElement option, boolean select) {
332-
boolean isSelected=option.isSelected();
333-
if ((!isSelected && select) || (isSelected && !select)) {
304+
if (option.isSelected() != select) {
334305
option.click();
335306
}
336307
}

0 commit comments

Comments
 (0)