Issue with ContainsString while RegEdit is set to true

Hi

How can I leave the ( parentheses when using the containsstring command. I tried using backslash with ( but this doesn’t compile.

Only way I can get this command to work is if I use .* instead of ( and ).

I want to make sure I can test with (.
WS.containsString(response, “.User (ABC) is inactive.”, true)

Thanks

Set your boolean to false. You really do not have any reason to use Regular Expression (RegEx) since you want to make an exact comparison, although I am not sure about the first period and no space after it. Personally, why bash your head against your monitor trying to use the boolean as true when there is no need.

WS.containsString(response, ".User (ABC) is inactive.", false)
1 Like
  • in a Regular Expression in Java, you need to escape a ( with a backslash \
  • in Java/Groovy String literal, a backslash \ is a special character; You should write a double \\

so you should write

WS.containsString(response, ".*User \\([A-Za-z0-9]+\\) is inactive.*", true)

If the name part (ABC) is fixed, then the following code (which does not use Regex) is simpler.

WS.containsString(response, "User (ABC) is inactive", false)
1 Like

The keyword name containsString suggests to me that it would work with an regular expression
"User \\(ABC\\) is inactive"
without prefixing .* and postfixing .*.

But I felt uncertain, checked the source code of the keyword: https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/com/kms/katalon/core/webservice/keyword/builtin/ContainsStringKeyword.groovy, there I found a line:

            if (useRegex) {
                Pattern p = Pattern.compile(string, Pattern.DOTALL)
                isMatch = p.matcher(responseText).matches()
            } else {
                isMatch = (string != null && responseText.contains(string))
            }

As you see matches() of java.util.regex.Matcher is used. The Javadoc says:

public boolean matches()
Attempts to match the entire region against the pattern.

Therefore, the following code would not work:

WS.containsString(response, "User \\(ABC\\) is inactive", true)

You need a regex for entire match, such as:

WS.containsString(response, ".*User \\(ABC\\) is inactive.*", true)

@ThanhTo

IMHO, the keyword name containsString with regex=true does not express what it actually does. The implementation should rather use the find() method of Matcher.

Or you should describe in the Keyword documentatin in detail.

2 Likes