21
21
import org .openqa .selenium .NoSuchElementException ;
22
22
import org .openqa .selenium .WebElement ;
23
23
24
- import java .util .ArrayList ;
25
24
import java .util .List ;
26
25
import java .util .StringTokenizer ;
26
+ import java .util .stream .Collectors ;
27
27
28
28
/**
29
29
* Models a SELECT tag, providing helper methods to select and deselect options.
@@ -77,15 +77,7 @@ public List<WebElement> getOptions() {
77
77
*/
78
78
@ Override
79
79
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 ());
89
81
}
90
82
91
83
/**
@@ -95,13 +87,8 @@ public List<WebElement> getAllSelectedOptions() {
95
87
*/
96
88
@ Override
97
89
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" ));
105
92
}
106
93
107
94
/**
@@ -119,16 +106,15 @@ public void selectByVisibleText(String text) {
119
106
List <WebElement > options =
120
107
element .findElements (By .xpath (".//option[normalize-space(.) = " + Quotes .escape (text ) + "]" ));
121
108
122
- boolean matched = false ;
123
109
for (WebElement option : options ) {
124
110
setSelected (option , true );
125
111
if (!isMultiple ()) {
126
112
return ;
127
113
}
128
- matched = true ;
129
114
}
130
115
131
- if (options .isEmpty () && text .contains (" " )) {
116
+ boolean matched = !options .isEmpty ();
117
+ if (!matched && text .contains (" " )) {
132
118
String subStringWithoutSpace = getLongestSubstringWithoutSpace (text );
133
119
List <WebElement > candidates ;
134
120
if ("" .equals (subStringWithoutSpace )) {
@@ -177,15 +163,7 @@ private String getLongestSubstringWithoutSpace(String s) {
177
163
*/
178
164
@ Override
179
165
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 );
189
167
}
190
168
191
169
/**
@@ -199,20 +177,11 @@ public void selectByIndex(int index) {
199
177
*/
200
178
@ Override
201
179
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 )) {
207
181
setSelected (option , true );
208
182
if (!isMultiple ()) {
209
183
return ;
210
184
}
211
- matched = true ;
212
- }
213
-
214
- if (!matched ) {
215
- throw new NoSuchElementException ("Cannot locate option with value: " + value );
216
185
}
217
186
}
218
187
@@ -250,15 +219,8 @@ public void deselectByValue(String value) {
250
219
"You may only deselect options of a multi-select" );
251
220
}
252
221
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 )) {
257
223
setSelected (option , false );
258
- matched = true ;
259
- }
260
- if (!matched ) {
261
- throw new NoSuchElementException ("Cannot locate option with value: " + value );
262
224
}
263
225
}
264
226
@@ -277,15 +239,7 @@ public void deselectByIndex(int index) {
277
239
"You may only deselect options of a multi-select" );
278
240
}
279
241
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 );
289
243
}
290
244
291
245
/**
@@ -307,16 +261,34 @@ public void deselectByVisibleText(String text) {
307
261
308
262
List <WebElement > options = element .findElements (By .xpath (
309
263
".//option[normalize-space(.) = " + Quotes .escape (text ) + "]" ));
264
+ if (options .isEmpty ()) {
265
+ throw new NoSuchElementException ("Cannot locate element with text: " + text );
266
+ }
310
267
311
- boolean matched = false ;
312
268
for (WebElement option : options ) {
313
269
setSelected (option , false );
314
- matched = true ;
315
270
}
271
+ }
316
272
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 );
319
278
}
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 );
320
292
}
321
293
322
294
/**
@@ -329,8 +301,7 @@ public void deselectByVisibleText(String text) {
329
301
* deselected (false)
330
302
*/
331
303
private void setSelected (WebElement option , boolean select ) {
332
- boolean isSelected =option .isSelected ();
333
- if ((!isSelected && select ) || (isSelected && !select )) {
304
+ if (option .isSelected () != select ) {
334
305
option .click ();
335
306
}
336
307
}
0 commit comments