Skip to content

Commit 6e6a8fc

Browse files
committed
[java] Adding a bit of Java 8 flavor
1 parent 894d238 commit 6e6a8fc

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

java/client/src/org/openqa/selenium/support/pagefactory/ByChained.java

+10-15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.io.Serializable;
2626
import java.util.ArrayList;
2727
import java.util.List;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
2830

2931
/**
3032
* Mechanism used to locate elements within a document using a series of other lookups. This class
@@ -66,28 +68,21 @@ public List<WebElement> findElements(SearchContext context) {
6668
if (elems.isEmpty()) {
6769
break; // if any one of the bys finds no elements, then return no elements
6870
}
69-
List<WebElement> newElems = new ArrayList<>();
70-
for (WebElement elem : elems) {
71-
newElems.addAll(elem.findElements(bys[i]));
72-
}
73-
elems = newElems;
71+
final By by = bys[i];
72+
elems = elems.stream()
73+
.map(elem -> elem.findElements(by))
74+
.flatMap(List::stream)
75+
.collect(Collectors.toList());
7476
}
7577

7678
return elems;
7779
}
7880

7981
@Override
8082
public String toString() {
81-
StringBuilder stringBuilder = new StringBuilder("By.chained(");
82-
stringBuilder.append("{");
83-
84-
boolean first = true;
85-
for (By by : bys) {
86-
stringBuilder.append((first ? "" : ",")).append(by);
87-
first = false;
88-
}
89-
stringBuilder.append("})");
90-
return stringBuilder.toString();
83+
return String.format(
84+
"By.chained({%s})",
85+
Stream.of(bys).map(By::toString).collect(Collectors.joining(",")));
9186
}
9287

9388
}

java/client/test/org/openqa/selenium/support/pagefactory/ByChainedTest.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void findElementZeroBy() {
4848

4949
ByChained by = new ByChained();
5050
assertThatExceptionOfType(NoSuchElementException.class)
51-
.isThrownBy(() -> by.findElement(driver));
51+
.isThrownBy(() -> by.findElement(driver));
5252
}
5353

5454
@Test
@@ -98,7 +98,7 @@ public void findElementOneByEmpty() {
9898

9999
ByChained by = new ByChained(By.name("cheese"));
100100
assertThatExceptionOfType(NoSuchElementException.class)
101-
.isThrownBy(() -> by.findElement(driver));
101+
.isThrownBy(() -> by.findElement(driver));
102102
}
103103

104104
@Test
@@ -166,7 +166,7 @@ public void findElementTwoByEmptyParent() {
166166

167167
ByChained by = new ByChained(By.name("cheese"), By.name("photo"));
168168
assertThatExceptionOfType(NoSuchElementException.class)
169-
.isThrownBy(() -> by.findElement(driver));
169+
.isThrownBy(() -> by.findElement(driver));
170170
}
171171

172172
@Test
@@ -299,7 +299,13 @@ public void findElementThreeBy_firstFindsTwo_secondEmpty() {
299299
@Test
300300
public void testEquals() {
301301
assertThat(new ByChained(By.id("cheese"), By.name("photo")))
302-
.isEqualTo(new ByChained(By.id("cheese"), By.name("photo")));
302+
.isEqualTo(new ByChained(By.id("cheese"), By.name("photo")));
303+
}
304+
305+
@Test
306+
public void testToString() {
307+
assertThat(new ByChained(By.id("cheese"), By.name("photo")).toString())
308+
.isEqualTo("By.chained({By.id: cheese,By.name: photo})");
303309
}
304310

305311
private interface AllDriver extends SearchContext {

0 commit comments

Comments
 (0)