Checking to see if search results contain the search criteria

One of the tests I’ve been working on is to search for a particular record, then confirm the results contain the search criteria. For example, if I enter the word, “limited” as the name for a company in my search, each record returned needs to contain the word, “limited” in the title.

It turns out to be quite simple using the “.contains” syntax of Groovy. Quite literally, see if the text contains the string you’re looking for. To make my example more robust, all the text is converted to lower case so there’s no confusion between “limited” and “Limited”.

After entering the search criteria, the results are read from a table. The text is read into a variable, converted to lowercase and checked to see if it contains the result. The evaluation can be done in a single line and looks like:

//Convert the name and search criteria to lowercase to make sure they match
tempResult=recordName.toLowerCase().contains(searchCriteria.toLowerCase())

For this example, “searchCriteria” is the variable that contains my string, “limited”

I then put that to a simple test to confirm the results and output an error if the text didn’t match.

//Confirm the company name contains the search string
if(tempResult!=true){
log.logError('ERROR: The search result does not match the search criteria')
KeywordUtil.markFailed('ERROR: The search result does not match the search criteria')
} else {
log.logWarning('SUCCESS: The search result matches the search criteria')
}

It’s simple, but a very easy way to confirm search results match the search criteria.

<Festive>

Author Signature for Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.