1
+ /*
2
+ * Copyright (c) 2016 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 endtoend .functions .jquery .traversing .filtering ;
18
+
19
+ import com .google .common .base .Predicate ;
20
+ import io .github .seleniumquery .SeleniumQueryObject ;
21
+ import org .junit .Before ;
22
+ import org .junit .ClassRule ;
23
+ import org .junit .Rule ;
24
+ import org .junit .Test ;
25
+ import org .openqa .selenium .WebElement ;
26
+ import testinfrastructure .junitrule .SetUpAndTearDownDriver ;
27
+
28
+ import static io .github .seleniumquery .SeleniumQuery .$ ;
29
+ import static org .hamcrest .Matchers .is ;
30
+ import static org .junit .Assert .assertThat ;
31
+
32
+ public class FilterFunctionTest {
33
+
34
+ private static final String MY_DIV_ID = "#myDiv" ;
35
+ @ ClassRule @ Rule public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver ();
36
+
37
+ private SeleniumQueryObject $myDiv ;
38
+ private WebElement myDivWebElement ;
39
+
40
+ @ Before
41
+ public void setUp () {
42
+ this .$myDiv = $ (MY_DIV_ID );
43
+ this .myDivWebElement = $myDiv .get (0 );
44
+ assertThat ($myDiv .size (), is (1 ));
45
+ }
46
+
47
+ @ Test
48
+ public void filterSelector_into_emptySet () {
49
+ assertThat ($myDiv .filter ("" ).size (), is (0 ));
50
+ assertThat ($myDiv .filter ((String ) null ).size (), is (0 ));
51
+ assertThat ($myDiv .filter (".non-existant-class" ).size (), is (0 ));
52
+ }
53
+
54
+ @ Test
55
+ public void filterPredicate_into_emptySet () {
56
+ assertThat ($myDiv .filter ((Predicate <WebElement >) null ).size (), is (0 ));
57
+ assertThat ($myDiv .filter (new Predicate <WebElement >() {
58
+ @ Override
59
+ public boolean apply (WebElement input ) {
60
+ assertThat (input , is (myDivWebElement ));
61
+ return false ;
62
+ }
63
+ }).size (), is (0 ));
64
+ }
65
+
66
+ @ Test
67
+ public void filterSelector_regularUse () {
68
+ assertThat ($myDiv .filter (MY_DIV_ID ).get (0 ), is (myDivWebElement ));
69
+ assertThat ($ ("*" ).filter (MY_DIV_ID ).get (0 ), is (myDivWebElement ));
70
+ }
71
+
72
+ @ Test
73
+ public void filterPredicate_regularUse () {
74
+ assertThat ($myDiv .filter (new Predicate <WebElement >() {
75
+ @ Override
76
+ public boolean apply (WebElement input ) {
77
+ return input == myDivWebElement ;
78
+ }
79
+ }).get (0 ), is (myDivWebElement ));
80
+ assertThat ($ ("*" ).filter (new Predicate <WebElement >() {
81
+ @ Override
82
+ public boolean apply (WebElement input ) {
83
+ return $ (input ).is (MY_DIV_ID );
84
+ }
85
+ }).get (0 ), is (myDivWebElement ));
86
+ }
87
+
88
+ @ Test
89
+ public void filterSelector_with_pseudo () {
90
+ assertThat ($ ("div" ).filter (":empty" ).attr ("id" ), is ("id-empty" ));
91
+ }
92
+
93
+ }
0 commit comments