8000 #97 Done $("").filter(<PREDICATE>) · seleniumQuery/seleniumQuery@ad89afe · GitHub
[go: up one dir, main page]

Skip to content

Commit ad89afe

Browse files
committed
#97 Done $("").filter(<PREDICATE>)
1 parent 2f95fef commit ad89afe

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/main/java/io/github/seleniumquery/functions/jquery/traversing/filtering/FilterFunction.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,32 @@
1717
package io.github.seleniumquery.functions.jquery.traversing.filtering;
1818

1919
import com.google.common.base.Predicate;
20+
import com.google.common.collect.Iterables;
21+
import com.google.common.collect.Lists;
2022
import io.github.seleniumquery.SeleniumQueryObject;
2123
import org.openqa.selenium.WebElement;
2224

25+
import java.util.List;
26+
2327
import static io.github.seleniumquery.InternalSeleniumQueryObjectFactory.instance;
2428

2529
public class FilterFunction {
2630

2731
public SeleniumQueryObject filter(SeleniumQueryObject seleniumQueryObject, Predicate<WebElement> filterFunction) {
32+
List<WebElement> filteredWebElements = filterWebElements(seleniumQueryObject.get(), filterFunction);
2833
return instance().create(seleniumQueryObject.getSeleniumQueryFunctions(),
2934
seleniumQueryObject.getWebDriver(),
3035
null,
31-
seleniumQueryObject.get(),
36+
filteredWebElements,
3237
seleniumQueryObject);
3338
}
3439

40+
private List<WebElement> filterWebElements(List<WebElement> unfiltered, Predicate<WebElement> filterFunction) {
41+
if (filterFunction == null) {
42+
return unfiltered;
43+
}
44+
Iterable<WebElement> filter = Iterables.filter(unfiltered, filterFunction);
45+
return Lists.newArrayList(filter);
46+
}
47+
3548
}

src/test/java/io/github/seleniumquery/functions/jquery/traversing/filtering/FilterFunctionTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
import io.github.seleniumquery.SeleniumQueryObject;
2121
import org.junit.Test;
2222
import org.openqa.selenium.WebElement;
23+
import testinfrastructure.testdouble.Stubs;
2324

2425
import static org.hamcrest.CoreMatchers.is;
2526
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
27+
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
2628
import static org.junit.Assert.assertThat;
2729
import static testinfrastructure.testdouble.Dummies.createDummyWebElement;
2830
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createStubSeleniumQueryObject;
2931
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithElements;
32+
import static testinfrastructure.testdouble.Stubs.createStubWebElementWithTag;
3033

3134
public class FilterFunctionTest {
3235

@@ -76,4 +79,24 @@ public void resultSQO_should_have_same_WebDriver_as_targetSQO() {
7679
assertThat(resultSQO.getWebDriver(), is(targetSQO.getWebDriver()));
7780
}
7881

82+
@Test
83+
public void resultSQO_should_onlyKeepElementsThatPassThePredicateFunction() {
84+
// given
85+
WebElement spanOne = createStubWebElementWithTag("span");
86+
WebElement notSpan = createStubWebElementWithTag("div");
87+
WebElement spanTwo = createStubWebElementWithTag("span");
88+
89+
SeleniumQueryObject targetSQO = createStubSeleniumQueryObjectWithElements(spanOne, notSpan, spanTwo);
90+
// when
91+
Predicate<WebElement> keepSpansPredicate = new Predicate<WebElement>() {
92+
@Override
93+
public boolean apply(WebElement webElement) {
94+
return "span".equals(webElement.getTagName());
95+
}
96+
};
97+
SeleniumQueryObject resultSQO = filterFunction.filter(targetSQO, keepSpansPredicate);
98+
// then
99+
assertThat(resultSQO.get(), contains(spanOne, spanTwo));
100+
}
101+
79102
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package testinfrastructure.testdouble;
2+
3+
import org.openqa.selenium.WebElement;
4+
5+
import static org.mockito.Mockito.doReturn;
6+
import static testinfrastructure.testdouble.Dummies.createDummyWebElement;
7+
8+
public class Stubs {
9+
10+
public static WebElement createStubWebElementWithTag(String tag) {
11+
WebElement stubWebElement = createDummyWebElement();
12+
doReturn(tag).when(stubWebElement).getTagName();
13+
return stubWebElement;
14+
}
15+
16+
}

0 commit comments

Comments
 (0)
0