Skip to content

Instantly share code, notes, and snippets.

@matthias-dirickx
Last active February 4, 2019 11:12
Show Gist options
  • Save matthias-dirickx/4cb5f9eb9dd59b3c2b4c1d10a0e0f9fb to your computer and use it in GitHub Desktop.
Save matthias-dirickx/4cb5f9eb9dd59b3c2b4c1d10a0e0f9fb to your computer and use it in GitHub Desktop.
Matching text of Selneium WebElements - Redundant with constructor because sometimes I wanted to check a list of x elements against the same value.
/*
Test utilities
Copyright (C) 2019
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import org.openqa.selenium.WebElement;
/**
*
* @author mdirickx
*
* Helper class -- creating matches against objects based on text.
*
* Goal: collecting text matching logic and delegating the matching to this subclass rather than repeating logic.
*
* @usage
* Create a matcher to handle to match value and logic.
* Match with the {@link #isMatchFor(WebElement) isMatchFor} method.
*
* Have it all in one go:
* ElementTextMatcher etm = new ElementTextMatcher("find this").setContains().isMatchFor(webElement);
*
* First configure:
* ElementTextMatcher etm = new ElementTextMatcher("find this").setContains(true);
* etm.isMatch(myWebElement);
*
* --> Returns true if conditions true.
* --> Returns false if conditions false.
*
* The checks are a series of IFs, ordered as:
* <ul>
* <li>Contains</i>
* <li>Starts with</i>
* <li>Exact match</li>
* <li>Is regex</li>
* <ul>
*
* The checks are done in that order, and the blooean that is returned is overwritten on each consequent check.
* It was a choice at the time. You can rewrite this.
*
* The ignore case boolean is treated separately. So you can have that wit all of them.
* It might screw with the regex implementation, so be careful there. I'll update this when I change it for myself.
*/
public class ElementTextMatcher {
private String name;
private boolean contains;
private boolean isRegex;
private boolean startsWith;
private boolean ignoreCase;
private boolean exactMatch;
/**
* Initialization of class will set the attributes.
*
* Settings can be done in the setter functions.
* If you use an IDE the options will be offered.
*
* Possibilities:
* <ul>
* <li>Ignore case (setIgnoreCase(boolean))
* <ul><li>This ignores the case of both target and what it is matched to. All is set to lower case.</li>
* <li>default: false</li>
* </ul>
* </li>
* <li>Is Regex (setIsRegex(boolean))
* <ul><li>This sets the match term 'name' to be used as regex.
* It needs to be valid regex and it will be matched as such.
* When set true, contains and starts with are automatically disengaged.</li>
* <li>default: false</li>
* </ul>
* </li>
* <li>Contains (setContains(boolean))
* <ul><li>This makes the matcher look for a match within the target.</li>
* <li>default: false</li>
* </ul>
* </li>
* </ul>
*
* @param name String The text to search for
*/
public ElementTextMatcher(String name) {
this.name = name;
this.exactMatch = false;
this.contains = false;
this.isRegex = false;
this.startsWith = false;
this.ignoreCase = false;
}
public ElementTextMatcher setIgnoreCase(boolean ignoreCase) {
this.ignoreCase = ignoreCase;
return this;
}
public ElementTextMatcher setIsRegex(boolean isRegex) {
this.isRegex = isRegex;
return this;
}
public ElementTextMatcher setContains(boolean contains) {
this.contains = contains;
return this;
}
public ElementTextMatcher setStartsWith(boolean startsWith) {
this.startsWith = startsWith;
return this;
}
/**
* Matches element against configured ElementTextMatcher attributes.
* No reconfiguration possible.
* When you need a new configuration you must destroy and recreate the ElementTextMatcher.
*
* @param el - WebElement
* @return boolean
*/
public boolean isMatchFor(WebElement el) {
return isMatchFor(el.getText());
}
public boolean isMatchFor(String text) {
boolean result
result = null;
//Ignore case setting
//If ignore case - reduce both text and name to lower case.
if(ignoreCase) {
text = text.toLowerCase();
name = name.toLowerCase();
}
//Check for type and apply
if(exactMatch) {
result = text.equals(name);
}
if (startsWith){
result = text.startsWith(name);
}
if (contains) {
result = text.contains(name);
}
if (isRegex) {
result = text.matches(name);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment