Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 5db0303

Browse files
committed
Added performance enhanced implementation of Selenium's Select
1 parent 7b82713 commit 5db0303

File tree

4 files changed

+64
-20
lines changed

4 files changed

+64
-20
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/pagefragments/GenericSelect.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.openqa.selenium.support.ui.Select;
1010

1111
import info.novatec.testit.webtester.pagefragments.annotations.Mapping;
12+
import info.novatec.testit.webtester.pagefragments.utils.EnhancedSelect;
1213

1314

1415
@Mapping(tag = "select")
@@ -41,7 +42,7 @@ default List<String> getOptionTexts() {
4142
* @since 2.0
4243
*/
4344
default Stream<String> streamOptionTexts() {
44-
return new Select(webElement()).getOptions().stream().map(WebElement::getText);
45+
return new EnhancedSelect(webElement()).getOptions().stream().map(WebElement::getText);
4546
}
4647

4748
/**
@@ -71,7 +72,7 @@ default List<String> getOptionValues() {
7172
* @since 2.0
7273
*/
7374
default Stream<String> streamOptionValues() {
74-
return new Select(webElement()).getOptions().stream().map(option -> option.getAttribute("value"));
75+
return new EnhancedSelect(webElement()).getOptions().stream().map(option -> option.getAttribute("value"));
7576
}
7677

7778
/**
@@ -83,7 +84,7 @@ default Stream<String> streamOptionValues() {
8384
* @since 2.0
8485
*/
8586
default Integer getOptionCount() {
86-
return new Select(webElement()).getOptions().size();
87+
return new EnhancedSelect(webElement()).getOptions().size();
8788
}
8889

8990
}

webtester-core/src/main/java/info/novatec/testit/webtester/pagefragments/MultiSelect.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import info.novatec.testit.webtester.pagefragments.annotations.Mapping;
2424
import info.novatec.testit.webtester.pagefragments.annotations.Mark;
2525
import info.novatec.testit.webtester.pagefragments.annotations.As;
26+
import info.novatec.testit.webtester.pagefragments.utils.EnhancedSelect;
27+
2628

2729
@Mapping(tag = "select", attribute = "multiple")
2830
public interface MultiSelect extends GenericSelect<MultiSelect> {
@@ -39,7 +41,7 @@ public interface MultiSelect extends GenericSelect<MultiSelect> {
3941
@Mark(As.USED)
4042
@Produces(DeselectedAllEvent.class)
4143
default MultiSelect deselectAll() {
42-
new Select(webElement()).deselectAll();
44+
new EnhancedSelect(webElement()).deselectAll();
4345
return this;
4446
}
4547

@@ -72,7 +74,7 @@ default MultiSelect deselectByTexts(String... texts) throws NoSuchElementExcepti
7274
@Mark(As.USED)
7375
@Produces(DeselectedByTextsEvent.class)
7476
default MultiSelect deselectByTexts(Collection<String> texts) throws NoSuchElementException {
75-
Select select = new Select(webElement());
77+
Select select = new EnhancedSelect(webElement());
7678
texts.forEach(select::deselectByVisibleText);
7779
return this;
7880
}
@@ -106,7 +108,7 @@ default MultiSelect deselectByValues(String... values) throws NoSuchElementExcep
106108
@Mark(As.USED)
107109
@Produces(DeselectedByValuesEvent.class)
108110
default MultiSelect deselectByValues(Collection<String> values) throws NoSuchElementException {
109-
Select select = new Select(webElement());
111+
Select select = new EnhancedSelect(webElement());
110112
values.forEach(select::deselectByValue);
111113
return this;
112114
}
@@ -140,7 +142,7 @@ default MultiSelect deselectByIndices(Integer... indices) throws NoSuchElementEx
140142
@Mark(As.USED)
141143
@Produces(DeselectedByIndicesEvent.class)
142144
default MultiSelect deselectByIndices(Collection<Integer> indices) throws NoSuchElementException {
143-
Select select = new Select(webElement());
145+
Select select = new EnhancedSelect(webElement());
144146
indices.forEach(select::deselectByIndex);
145147
return this;
146148
}
@@ -174,7 +176,7 @@ default MultiSelect selectByTexts(String... texts) throws NoSuchElementException
174176
@Mark(As.USED)
175177
@Produces(SelectedByTextsEvent.class)
176178
default MultiSelect selectByTexts(Collection<String> texts) throws NoSuchElementException {
177-
Select select = new Select(webElement());
179+
Select select = new EnhancedSelect(webElement());
178180
texts.forEach(select::selectByVisibleText);
179181
return this;
180182
}
@@ -208,7 +210,7 @@ default MultiSelect selectByValues(String... values) throws NoSuchElementExcepti
208210
@Mark(As.USED)
209211
@Produces(SelectedByValuesEvent.class)
210212
default MultiSelect selectByValues(Collection<String> values) throws NoSuchElementException {
211-
Select select = new Select(webElement());
213+
Select select = new EnhancedSelect(webElement());
212214
values.forEach(select::selectByValue);
213215
return this;
214216
}
@@ -242,7 +244,7 @@ default MultiSelect selectByIndices(Integer... indices) throws NoSuchElementExce
242244
@Mark(As.USED)
243245
@Produces(SelectedByIndicesEvent.class)
244246
default MultiSelect selectByIndices(Collection<Integer> indices) throws NoSuchElementException {
245-
Select select = new Select(webElement());
247+
Select select = new EnhancedSelect(webElement());
246248
indices.forEach(select::selectByIndex);
247249
return this;
248250
}
@@ -273,7 +275,7 @@ default List<String> getSelectionTexts() {
273275
*/
274276
@Mark(As.READ)
275277
default Stream<String> streamSelectionTexts() {
276-
return new Select(webElement()).getAllSelectedOptions().stream().map(WebElement::getText);
278+
return new EnhancedSelect(webElement()).getAllSelectedOptions().stream().map(WebElement::getText);
277279
}
278280

279281
/**
@@ -302,7 +304,7 @@ default List<String> getSelectionValues() {
302304
*/
303305
@Mark(As.READ)
304306
default Stream<String> streamSelectionValues() {
305-
return new Select(webElement()).getAllSelectedOptions().stream().map(option -> option.getAttribute("value"));
307+
return new EnhancedSelect(webElement()).getAllSelectedOptions().stream().map(option -> option.getAttribute("value"));
306308
}
307309

308310
/**
@@ -331,7 +333,7 @@ default List<Integer> getSelectionIndices() {
331333
*/
332334
@Mark(As.READ)
333335
default Stream<Integer> streamSelectionIndices() {
334-
return new Select(webElement()).getAllSelectedOptions()
336+
return new EnhancedSelect(webElement()).getAllSelectedOptions()
335337
.stream()
336338
.map(option -> option.getAttribute("index"))
337339
.map(Integer::parseInt);
@@ -347,7 +349,7 @@ default Stream<Integer> streamSelectionIndices() {
347349
* @since 2.0
348350
*/
349351
default Integer getSelectionCount() {
350-
return new Select(webElement()).getAllSelectedOptions().size();
352+
return new EnhancedSelect(webElement()).getAllSelectedOptions().size();
351353
}
352354

353355
}

webtester-core/src/main/java/info/novatec/testit/webtester/pagefragments/SingleSelect.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import info.novatec.testit.webtester.pagefragments.annotations.Mapping;
1515
import info.novatec.testit.webtester.pagefragments.annotations.Mark;
1616
import info.novatec.testit.webtester.pagefragments.annotations.As;
17+
import info.novatec.testit.webtester.pagefragments.utils.EnhancedSelect;
1718

1819

1920
@Mapping(tag = "select", attribute = "!multiple")
@@ -36,7 +37,7 @@ public interface SingleSelect extends GenericSelect<SingleSelect> {
3637
@Mark(As.USED)
3738
@Produces(SelectedByTextEvent.class)
3839
default SingleSelect selectByText(String text) throws NoSuchElementException {
39-
new Select(webElement()).selectByVisibleText(text);
40+
new EnhancedSelect(webElement()).selectByVisibleText(text);
4041
return this;
4142
}
4243

@@ -57,7 +58,7 @@ default SingleSelect selectByText(String text) throws NoSuchElementException {
5758
@Mark(As.USED)
5859
@Produces(SelectedByValueEvent.class)
5960
default SingleSelect selectByValue(String value) throws NoSuchElementException {
60-
new Select(webElement()).selectByValue(value);
61+
new EnhancedSelect(webElement()).selectByValue(value);
6162
return this;
6263
}
6364

@@ -78,7 +79,7 @@ default SingleSelect selectByValue(String value) throws NoSuchElementException {
7879
@Mark(As.USED)
7980
@Produces(SelectedByIndexEvent.class)
8081
default SingleSelect selectByIndex(Integer index) throws NoSuchElementException {
81-
new Select(webElement()).selectByIndex(index);
82+
new EnhancedSelect(webElement()).selectByIndex(index);
8283
return this;
8384
}
8485

@@ -96,7 +97,7 @@ default SingleSelect selectByIndex(Integer index) throws NoSuchElementException
9697
@Mark(As.READ)
9798
default Optional<String> getSelectionText() {
9899
try {
99-
String text = new Select(webElement()).getFirstSelectedOption().getText();
100+
String text = new EnhancedSelect(webElement()).getFirstSelectedOption().getText();
100101
return Optional.ofNullable(text);
101102
} catch (NoSuchElementException e) {
102103
return Optional.empty();
@@ -117,7 +118,7 @@ default Optional<String> getSelectionText() {
117118
@Mark(As.READ)
118119
default Optional<String> getSelectionValue() {
119120
try {
120-
String value = new Select(webElement()).getFirstSelectedOption().getAttribute("value");
121+
String value = new EnhancedSelect(webElement()).getFirstSelectedOption().getAttribute("value");
121122
return Optional.ofNullable(value);
122123
} catch (NoSuchElementException e) {
123124
return Optional.empty();
@@ -138,7 +139,7 @@ default Optional<String> getSelectionValue() {
138139
@Mark(As.READ)
139140
default Optional<Integer> getSelectionIndex() {
140141
try {
141-
String index = new Select(webElement()).getFirstSelectedOption().getAttribute("index");
142+
String index = new EnhancedSelect(webElement()).getFirstSelectedOption().getAttribute("index");
142143
return Optional.ofNullable(index).map(Integer::parseInt);
143144
} catch (NoSuchElementException e) {
144145
return Optional.empty();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package info.novatec.testit.webtester.pagefragments.utils;
2+
3+
import java.util.List;
4+
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.WebElement;
7+
import org.openqa.selenium.support.ui.Select;
8+
9+
10+
/**
11+
* This implementation of Selenium's {@link Select} helper class fixes some issues with the original implementation.
12+
* <p>
13+
* <b>Fixed Issues:</b>
14+
* <ul>
15+
* <li>{@link #getFirstSelectedOption()} has very bad performance in case of great number of options</li>
16+
* <li>{@link #getAllSelectedOptions()} has very bad performance in case of great number of options</li>
17+
* </ul>
18+
*
19+
* @since 2.0.1
20+
*/
21+
public class EnhancedSelect extends Select {
22+
23+
private final WebElement element;
24+
25+
public EnhancedSelect(WebElement element) {
26+
super(element);
27+
this.element = element;
28+
}
29+
30+
@Override
31+
public WebElement getFirstSelectedOption() {
32+
return element.findElement(By.cssSelector("option:checked"));
33+
}
34+
35+
@Override
36+
public List<WebElement> getAllSelectedOptions() {
37+
return element.findElements(By.cssSelector("option:checked"));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)