Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Filters and add textContent() filter.
- Loading branch information
Showing
31 changed files
with
413 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
fluentlenium-core/src/main/java/org/fluentlenium/core/filter/AttributeFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,72 @@ | |||
package org.fluentlenium.core.filter; | |||
|
|||
import com.google.common.collect.Collections2; | |||
import org.fluentlenium.core.domain.FluentWebElement; | |||
import org.fluentlenium.core.filter.matcher.AbstractMatcher; | |||
import org.fluentlenium.core.filter.matcher.EqualMatcher; | |||
import org.fluentlenium.core.search.SearchFilter; | |||
|
|||
import java.util.Collection; | |||
import java.util.Locale; | |||
|
|||
/** | |||
* Search filter based on attribute value. | |||
*/ | |||
public class AttributeFilter implements SearchFilter { | |||
private final String attributeName; | |||
private final AbstractMatcher matcher; | |||
|
|||
/** | |||
* Construct a filter with an attribute name and an associated value | |||
* | |||
* @param attributeName attribute name | |||
* @param value value to filter | |||
*/ | |||
public AttributeFilter(String attributeName, final String value) { | |||
this.attributeName = attributeName; | |||
this.matcher = new EqualMatcher(value); | |||
} | |||
|
|||
/** | |||
* Construct a filter with a custom attribute and an associated matcher | |||
* | |||
* @param customAttribute custom attribute name | |||
* @param matcher matcher | |||
*/ | |||
public AttributeFilter(final String customAttribute, final AbstractMatcher matcher) { | |||
this.attributeName = customAttribute; | |||
this.matcher = matcher; | |||
} | |||
|
|||
public String getAttribut() { | |||
return attributeName.toLowerCase(Locale.ENGLISH); | |||
} | |||
|
|||
public String toString() { | |||
return getCssFilter(); | |||
} | |||
|
|||
public AbstractMatcher getMatcher() { | |||
return matcher; | |||
} | |||
|
|||
@Override | |||
public String getCssFilter() { | |||
String matcherAttribute = matcher == null ? null : matcher.getMatcherSymbol(); | |||
if (matcherAttribute == null) { | |||
matcherAttribute = ""; | |||
} | |||
return "[" + attributeName.toLowerCase(Locale.ENGLISH) + matcherAttribute + "=\"" + matcher.getValue() + "\"]"; | |||
} | |||
|
|||
@Override | |||
public boolean isCssFilterSupported() { | |||
return matcher != null && matcher.isCssFilterSupported() && !"text".equalsIgnoreCase(getAttribut()) && !"textContent" | |||
.equalsIgnoreCase(getAttribut()); | |||
} | |||
|
|||
@Override | |||
public Collection<FluentWebElement> applyFilter(final Collection<FluentWebElement> elements) { | |||
return Collections2.filter(elements, new AttributeFilterPredicate(this)); | |||
} | |||
} |
28 changes: 28 additions & 0 deletions
28
fluentlenium-core/src/main/java/org/fluentlenium/core/filter/AttributeFilterPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
package org.fluentlenium.core.filter; | |||
|
|||
import com.google.common.base.Predicate; | |||
import org.fluentlenium.core.domain.FluentWebElement; | |||
|
|||
/** | |||
* Filter a FluentWebElement collection to return only the elements with the same text | |||
*/ | |||
public class AttributeFilterPredicate implements Predicate<FluentWebElement> { | |||
private final AttributeFilter filter; | |||
|
|||
public AttributeFilterPredicate(final AttributeFilter text) { | |||
this.filter = text; | |||
} | |||
|
|||
public boolean apply(final FluentWebElement element) { | |||
|
|||
final String attribute = getAttributeValue(element); | |||
return filter != null && filter.getMatcher().isSatisfiedBy(attribute); | |||
} | |||
|
|||
private String getAttributeValue(final FluentWebElement element) { | |||
return "text".equalsIgnoreCase(filter.getAttribut()) | |||
? element.text() | |||
: element.attribute(filter.getAttribut()); | |||
} | |||
|
|||
} |
Oops, something went wrong.