-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#389 Support More Complex CSS Selectors
Create predicates for css selectors in separate class. Configure supported css selectors via regex expression and factory method.
- Loading branch information
Showing
8 changed files
with
293 additions
and
105 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
43 changes: 43 additions & 0 deletions
43
src/main/java/de/retest/web/selenium/css/DefaultSelectors.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.retest.web.selenium.css; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import de.retest.recheck.ui.descriptors.Element; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
public class DefaultSelectors { | ||
|
||
@RequiredArgsConstructor | ||
private static class Tupel { | ||
private final String pattern; | ||
private final Function<String, Predicate<Element>> factory; | ||
|
||
private Transformer createTransformer() { | ||
final Pattern cssTag = Pattern.compile( START_OF_LINE + pattern + REMAINING ); | ||
return new Transformer( cssTag, factory ); | ||
} | ||
} | ||
|
||
private static final String TAG_PATTERN = "([a-zA-Z0-9\\-]+)"; | ||
private static final String ID_PATTERN = "\\#([a-zA-Z0-9\\-]+)"; | ||
private static final String CLASS_PATTERN = "\\.([a-zA-Z0-9\\-]+)"; | ||
private static final String ATTRIBUTE_PATTERN = "\\[([a-zA-Z0-9\\-=\"]+)\\]"; | ||
private static final String REMAINING = "(.*)$"; | ||
private static final String START_OF_LINE = "^"; | ||
|
||
public static List<Transformer> all() { | ||
final LinkedList<Tupel> tupels = new LinkedList<>(); | ||
tupels.add( new Tupel( TAG_PATTERN, Has::cssTag ) ); | ||
tupels.add( new Tupel( ID_PATTERN, Has::cssId ) ); | ||
tupels.add( new Tupel( CLASS_PATTERN, Has::cssClass ) ); | ||
tupels.add( new Tupel( ATTRIBUTE_PATTERN, Has::cssAttribute ) ); | ||
return tupels.stream().map( Tupel::createTransformer ).collect( toList() ); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/de/retest/web/selenium/css/EmptySelector.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.retest.web.selenium.css; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import de.retest.recheck.ui.descriptors.Element; | ||
|
||
public class EmptySelector implements Selector { | ||
|
||
private final String selector; | ||
|
||
public EmptySelector( final String selector ) { | ||
this.selector = selector; | ||
} | ||
|
||
@Override | ||
public boolean matches() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Predicate<Element> predicate() { | ||
return e -> false; | ||
} | ||
|
||
@Override | ||
public String remainingSelector() { | ||
return selector; | ||
} | ||
|
||
} |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package de.retest.web.selenium.css; | ||
|
||
import static de.retest.web.AttributesUtil.CLASS; | ||
import static de.retest.web.AttributesUtil.ID; | ||
import static de.retest.web.AttributesUtil.NAME; | ||
import static de.retest.web.AttributesUtil.TEXT; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import de.retest.recheck.ui.descriptors.Element; | ||
import de.retest.recheck.ui.descriptors.IdentifyingAttributes; | ||
|
||
public class Has { | ||
|
||
private static final String TYPE = IdentifyingAttributes.TYPE_ATTRIBUTE_KEY; | ||
|
||
public static Predicate<Element> cssAttribute( final String withoutBrackets ) { | ||
final String attribute = getAttribute( withoutBrackets ); | ||
final String attributeValue = getAttributeValue( withoutBrackets ); | ||
return hasAttribute( attribute, attributeValue ); | ||
} | ||
|
||
private static Predicate<Element> hasAttribute( final String attribute, final String attributeValue ) { | ||
return element -> element.getAttributeValue( attribute ).toString().equals( attributeValue ); | ||
} | ||
|
||
private static String getAttribute( final String withoutBrackets ) { | ||
if ( withoutBrackets.contains( "=" ) ) { | ||
return withoutBrackets.substring( 0, withoutBrackets.lastIndexOf( "=" ) ); | ||
} | ||
return withoutBrackets; | ||
} | ||
|
||
private static String getAttributeValue( final String withoutBrackets ) { | ||
if ( !withoutBrackets.contains( "=" ) ) { | ||
return "true"; | ||
} | ||
String result = withoutBrackets.substring( withoutBrackets.lastIndexOf( "=" ) + 1 ); | ||
if ( result.contains( "\"" ) || result.contains( "'" ) ) { | ||
result = result.substring( 1, result.length() - 1 ); | ||
} | ||
return result; | ||
} | ||
|
||
public static Predicate<Element> linkText( final String linkText ) { | ||
return element -> "a".equalsIgnoreCase( element.getIdentifyingAttributes().getType() ) | ||
&& linkText.equals( element.getAttributes().get( TEXT ) ) | ||
|| linkText.equals( element.getIdentifyingAttributes().get( TEXT ) ); | ||
} | ||
|
||
public static Predicate<Element> partialLinkText( final String linkText ) { | ||
return element -> "a".equalsIgnoreCase( element.getIdentifyingAttributes().getType() ) | ||
&& element.getAttributeValue( TEXT ).toString().contains( linkText ); | ||
} | ||
|
||
public static Predicate<Element> cssClass( final String cssClass ) { | ||
return element -> element.getIdentifyingAttributes().get( CLASS ) != null | ||
? ((String) element.getIdentifyingAttributes().get( CLASS )).contains( cssClass ) : false; | ||
} | ||
|
||
public static Predicate<Element> cssName( final String name ) { | ||
return element -> name.equals( element.getIdentifyingAttributes().get( NAME ) ); | ||
} | ||
|
||
public static Predicate<Element> cssTag( final String tag ) { | ||
return element -> element.getIdentifyingAttributes().get( TYPE ).equals( tag ); | ||
} | ||
|
||
public static Predicate<Element> cssId( final String id ) { | ||
return element -> id.equals( element.getIdentifyingAttributes().get( ID ) ); | ||
} | ||
} |
Oops, something went wrong.