8000 #97 Completed $().filter("selector") - closes #97 · seleniumQuery/seleniumQuery@a9d2b27 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9d2b27

Browse files
committed
#97 Completed $().filter("selector") - closes #97
1 parent 47d9ef0 commit a9d2b27

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,19 @@ public CompiledCssSelector(CssSelector<Selector, TagComponent> cssSelector, bool
7070

7171
private boolean emptySelector = false;
7272
private List<CompiledCssSelector> compiledCssSelectors;
73+
private boolean hasNegatedPresent = false;
7374

7475
public CompiledSelector(String selector) {
75-
if (selector.trim().isEmpty()) {
76+
if (selector == null || selector.trim().isEmpty()) {
7677
this.emptySelector = true;
7778
} else {
7879
compiledCssSelectors = compileCssSelector(selector);
80+
81+
for (CompiledCssSelector s : compiledCssSelectors) {
82+
if (s.hasNegatedPresent) {
83+
this.hasNegatedPresent = true;
84+
}
85+
}
7986
}
8087
}
8188

@@ -99,22 +106,32 @@ public boolean is(WebDriver driver, List<WebElement> elements) {
99106
return false;
100107
}
101108
// If there is a :not(:present), then having no element is a expected status!
102-
if (elements.isEmpty()) {
103-
for (CompiledCssSelector s : compiledCssSelectors) {
104-
if (s.hasNegatedPresent) {
109+
if (this.hasNegatedPresent && elements.isEmpty()) {
110+
return true;
111+
}
112+
for (CompiledCssSelector s : compiledCssSelectors) {
113+
for (WebElement webElement : elements) {
114+
if (s.cssSelector.is(driver, webElement, s.argumentMap, s.parsedSimpleSelector)) {
105115
return true;
106116
}
107117
}
108118
}
119+
return false;
120+
}
121+
122+
public List<WebElement> filter(WebDriver driver, List<WebElement> elements) {
123+
List<WebElement> filteredElements = new ArrayList<>();
124+
if (emptySelector || hasNegatedPresent) {
125+
return filteredElements;
126+
}
109127
for (CompiledCssSelector s : compiledCssSelectors) {
110128
for (WebElement webElement : elements) {
111-
// if any matches, then it returns true
112129
if (s.cssSelector.is(driver, webElement, s.argumentMap, s.parsedSimpleSelector)) {
113-
return true;
130+
filteredElements.add(webElement);
114131
}
115132
}
116133
}
117-
return false;
134+
return filteredElements;
118135
}
119136

120137
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
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+
117
package io.github.seleniumquery.functions.jquery.traversing.filtering.filterfunction;
218

319
import io.github.seleniumquery.SeleniumQueryObject;
20+
import io.github.seleniumquery.functions.jquery.traversing.filtering.IsFunction;
421
import org.openqa.selenium.WebElement;
522

623
import java.util.List;
724

825
import static io.github.seleniumquery.InternalSeleniumQueryObjectFactory.instance;
9-
import static java.util.Collections.emptyList;
1026

1127
public class FilterSelectorFunction {
1228

1329
public SeleniumQueryObject filter(SeleniumQueryObject seleniumQueryObject, String selector) {
14-
List<WebElement> filteredWebElements = emptyList();
30+
List<WebElement> filteredWebElements = filterElements(seleniumQueryObject, selector);
1531
return instance().create(seleniumQueryObject.getSeleniumQueryFunctions(),
1632
seleniumQueryObje 6DAF ct.getWebDriver(),
1733
null,
1834
filteredWebElements,
1935
seleniumQueryObject);
2036
}
2137

38+
private List<WebElement> filterElements(SeleniumQueryObject seleniumQueryObject, String selector) {
39+
return new IsFunction.CompiledSelector(selector).filter(seleniumQueryObject.getWebDriver(),
40+
seleniumQueryObject.get());
41+
}
42+
2243
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
import com.google.common.base.Function;
2020
import io.github.seleniumquery.SeleniumQueryObject;
2121
import org.junit.Test;
22+
import org.openqa.selenium.WebElement;
2223
import testinfrastructure.testutils.FunctionsTestUtils;
2324

2425
import static org.hamcrest.collection.IsEmptyCollection.empty;
25-
import static org.junit.Assert.*;
26+
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
27+
import static org.junit.Assert.assertThat;
2628
import static testinfrastructure.testdouble.io.github.seleniumquery.SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithAtLeastOneElement;
29+
import static testinfrastructure.testdouble.io.github.seleniumquery.SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithElements;
30+
import static testinfrastructure.testdouble.org.openqa.selenium.WebElementMother.createWebElementWithTag;
2731

2832
public class FilterSelectorFunctionTest {
2933

@@ -62,6 +66,18 @@ public SeleniumQueryObject apply(SeleniumQueryObject targetSQO) {
6266
});
6367
}
6468

65-
// TODO filter(this, "selector") --> should keep everyone that matches the isFunction("selector")
69+
@Test
70+
public void resultSQO_should_onlyKeepElementsThatMatchTheSelector() {
71+
// given
72+
WebElement spanOne = createWebElementWithTag("span");
73+
WebElement notSpan = createWebElementWithTag("div");
74+
WebElement spanTwo = createWebElementWithTag("span");
75+
76+
SeleniumQueryObject targetSQO = createStubSeleniumQueryObjectWithElements(spanOne, notSpan, spanTwo);
77+
// when
78+
SeleniumQueryObject resultSQO = filterSelectorFunction.filter(targetSQO, "span");
79+
// then
80+
assertThat(resultSQO.get(), contains(spanOne, spanTwo));
81+
}
6682

6783
}

0 commit comments

Comments
 (0)
0