8000 Added $("").filter(<PREDICATE>) +begun implementing FilterFunction #97 · seleniumQuery/seleniumQuery@d4d72ab · GitHub
[go: up one dir, main page]

Skip to content

Commit d4d72ab

Browse files
committed
Added $("").filter(<PREDICATE>) +begun implementing FilterFunction #97
1 parent d69db6a commit d4d72ab

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

src/main/java/io/github/seleniumquery/SeleniumQueryObject.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.github.seleniumquery;
1818

19+
import com.google.common.base.Predicate;
1920
import io.github.seleniumquery.functions.SeleniumQueryFunctions;
2021
import io.github.seleniumquery.functions.as.SeleniumQueryPlugin;
2122
import io.github.seleniumquery.functions.as.StandardPlugins;
@@ -632,4 +633,15 @@ public String toString() {
632633
return this.by.toString();
633634
}
634635

636+
/**
637+
* Reduce the set of matched elements to those that pass the predicate's test.
638+
*
639+
* @param filterFunction A predicate used as a test for each element in the set.
640+
* @return An object with the elements that passed the predicate's test.
641+
* @since 0.11.0
642+
*/
643+
public SeleniumQueryObject filter(Predicate<WebElement> filterFunction) {
644+
return seleniumQueryFunctions.filterFunction(this, filterFunction);
645+
}
646+
635647
}

src/main/java/io/github/seleniumquery/functions/SeleniumQueryFunctions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package io.github.seleniumquery.functions;
1818

19+
import com.google.common.base.Predicate;
1920
import io.github.seleniumquery.SeleniumQueryObject;
2021
import io.github.seleniumquery.functions.jquery.attributes.PropFunction;
2122
import io.github.seleniumquery.functions.jquery.forms.ValFunction;
23+
import io.github.seleniumquery.functions.jquery.traversing.filtering.FilterFunction;
24+
import org.openqa.selenium.WebElement;
2225

2326
public class SeleniumQueryFunctions {
2427

@@ -42,4 +45,8 @@ public SeleniumQueryObject valWrite(SeleniumQueryObject seleniumQueryObject, Num
4245
return ValFunction.val(seleniumQueryObject, seleniumQueryObject.get(), value);
4346
}
4447

48+
public SeleniumQueryObject filterFunction(SeleniumQueryObject seleniumQueryObject, Predicate<WebElement> filterFunction) {
49+
return new FilterFunction().filter(seleniumQueryObject, filterFunction);
50+
}
51+
4552
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2015 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.seleniumquery.functions.jquery.traversing.filtering;
18+
19+
import com.google.common.base.Predicate;
20+
import io.github.seleniumquery.SeleniumQueryObject;
21+
import org.openqa.selenium.WebElement;
22+
23+
public class FilterFunction {
24+
25+
public SeleniumQueryObject filter(SeleniumQueryObject seleniumQueryObject, Predicate<WebElement> filterFunction) {
26+
return seleniumQueryObject;
27+
}
28+
29+
}

src/test/java/io/github/seleniumquery/SeleniumQueryObjectTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package io.github.seleniumquery;
1818

19+
import com.google.common.base.Predicate;
1920
import io.github.seleniumquery.functions.SeleniumQueryFunctions;
2021
import org.junit.Test;
22+
import org.openqa.selenium.WebElement;
2123

2224
import static org.hamcrest.CoreMatchers.is;
2325
import static org.hamcrest.MatcherAssert.assertThat;
2426
import static org.mockito.BDDMockito.given;
2527
import static org.mockito.Mockito.mock;
28+
import static testinfrastructure.testdouble.Dummies.createDummyWebElementPredicate;
2629
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createDummySeleniumQueryObject;
2730
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithSeleniumQueryFunctions;
2831

@@ -107,4 +110,19 @@ public void valWriteNumber() {
107110
assertThat(returnedObject, is(configuredReturningObject));
108111
}
109112

113+
@Test
114+
public void filterFunction() {
115+
// given
116+
SeleniumQueryFunctions seleniumQueryFunctions = createMockSeleniumQueryFunctions();
117+
SeleniumQueryObject seleniumQueryObject = createStubSeleniumQueryObjectWithSeleniumQueryFunctions(seleniumQueryFunctions);
118+
SeleniumQueryObject configuredReturningObject = createDummySeleniumQueryObject();
119+
120+
Predicate<WebElement> filterFunction = createDummyWebElementPredicate();
121+
given(seleniumQueryFunctions.filterFunction(seleniumQueryObject, filterFunction)).willReturn(configuredReturningObject);
122+
// when
123+
SeleniumQueryObject returnedObject = seleniumQueryObject.filter(filterFunction);
124+
// then
125+
assertThat(returnedObject, is(configuredReturningObject));
126+
}
127+
110128
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2015 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.seleniumquery.functions.jquery.traversing.filtering;
18+
19+
import com.google.common.base.Predicate;
20+
import io.github.seleniumquery.SeleniumQueryObject;
21+
import org.junit.Test;
22+
import org.openqa.selenium.WebElement;
23+
import testinfrastructure.testdouble.Dummies;
24+
import testinfrastructure.testdouble.SeleniumQueryObjectMother;
25+
26+
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
27+
import static org.junit.Assert.assertThat;
28+
29+
public class FilterFunctionTest {
30+
31+
private static final Predicate<WebElement> NULL_PREDICATE = null;
32+
33+
FilterFunction filterFunction = new FilterFunction();
34+
35+
@Test
36+
public void filter_null_should_return_same_elements() {
37+
// given
38+
WebElement dummyWebElement = Dummies.createDummyWebElement();
39+
WebElement dummyWebElement2 = Dummies.createDummyWebElement();
40+
SeleniumQueryObject sqo = SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithElements(dummyWebElement, dummyWebElement2);
41+
// when
42+
SeleniumQueryObject filteredObject = filterFunction.filter(sqo, NULL_PREDICATE);
43+
// then
44+
assertThat(filteredObject.get(), containsInAnyOrder(dummyWebElement, dummyWebElement2));
45+
}
46+
47+
}

src/test/java/testinfrastructure/testdouble/Dummies.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package testinfrastructure.testdouble;
1818

19+
import com.google.common.base.Predicate;
1920
import org.mockito.invocation.InvocationOnMock;
2021
import org.mockito.stubbing.Answer;
2122
import org.openqa.selenium.By;
@@ -60,4 +61,9 @@ public static WebElement createDummyWebElement() {
6061
return createDummy(WebElement.class);
6162
}
6263

64+
@SuppressWarnings("unchecked")
65+
public static Predicate<WebElement> createDummyWebElementPredicate() {
66+
return createDummy(Predicate.class);
67+
}
68+
6369
}

0 commit comments

Comments
 (0)
0