Ask a Question

Contains assertion with option "Use Token as regular expression" not working

SOLVED
mmoser18
Frequent Contributor

Contains assertion with option "Use Token as regular expression" not working

I reported a similar case a while ago but never got a response there - so again:

The "Contains Assertion" with "Use token as regular expression" is not working!

 

E.g. I have a case where I need to assert that the response contains either:

  <status>OK</status>

or

  <status>WARNING</status>

 

Phrased as a regular expression I would describe this as:

<status>((OK)|(WARNING))</status>

...  but if I enter that into the content field of a "Contains assertion" the assertion NEVER matches.

 

 

I also tried:

.*<status>((OK)|(WARNING))</status>.*

... but same result: no match. 😞

 

I verified that regexp with a tool (RegEx-Buddy) and that regexp *does* match the above strings.

So, what am I missing here? How can one specify a reg-exp that does match in a contains assertion?

5 REPLIES 5
nmrao
Community Hero

Regular expression in Contains Assertion works little different.

Because, the expression should match the entire response, not just the element provided.

Instead you may use Script Assertion. A sample below for your reference:
https://github.com/nmrao/groovyScripts/blob/master/regex/dateSample.groovy


Regards,
Rao.
HKosova
SmartBear Alumni (Retired)

I guess your response body is multiline? In this case <status>(OK|WARNING)</status> alone won't work because the regex needs to match the entire response body.

 

Try this:

(?s).*<status>(OK|WARNING)</status>.*

(?s) is the flag that enables . to match line breaks, so that the pattern can match a multiline response.

 

 

Alternatively, you can use the XPath Match assertion with this expression:

matches(//status/text(), 'OK|WARNING')

and expected result = true.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
mmoser18
Frequent Contributor

Ah - so this is actually not a "contains" but a "matches", i.e. one needs to write an expression that matches the *entire* message - not just a contained substring!

 

Later addition: indeed - now things work! Thanks!

 

You may want to provide a corresponding hint in the check-box's text (or at least its hover-text), because that's not exactly intuitive. Without the reg-exp checkbox being ticket one needs to provide a substring only that has to be contained somewhere in the response. With the check box ticked, it needs to match the entire response.

HKosova
SmartBear Alumni (Retired)

Glad you got it working!

 


@mmoser18 wrote:

 

You may want to provide a corresponding hint in the check-box's text (or at least its hover-text), because that's not exactly intuitive. Without the reg-exp checkbox being ticket one needs to provide a substring only that has to be contained somewhere in the response. With the check box ticked, it needs to match the entire response.


That's a good idea! You can submit it as an enhancement request here. We also accept pull requests on GitHub if you are willing to contribute to the code base directly.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
mmoser18
Frequent Contributor

> You can submit...

done.

cancel
Showing results for 
Search instead for 
Did you mean: