Forum Discussion

mmoser18's avatar
mmoser18
Frequent Contributor
8 years ago

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?

  • 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.

  • HKosova's avatar
    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.

    • mmoser18's avatar
      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's avatar
        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.