Forum Discussion

ryanshoward's avatar
ryanshoward
Occasional Contributor
10 years ago

Contains assertion using Regex failing

Hi,

I'm trying to implement some assertions using regex but I cannot get it working properly

 

in my soap response I have an element which looks like this:

 

<UploadResult>BOPS201511001CDIS-7</UploadResult>

 

All I want to do is locate the element and validate the data inside it.

 

my regex to match the data is: ((BOPS)+(\d{4})+(\d{2})+(\d{3})+(CDIS)+-+(\d{1,3}))

 

The issue is I'm not quite sure how to implement this properly. I've tried to use the Contains assertion but it fails. with error missing tolken in response

 

I'm happy to use the 'Contains' assertion or script assertion.

 

Any help would be greatly appreciated

 

Thanks

Ryan

  • Both of your regular expressions matches intended string, however, you might be experiencing failure.

    It is because:

    1. that is only matching partial string, but not entire response
    2. if you are providing value in the content, special characters needs to be escaped

    Below regex should work i.e., match anything before and after original regex(which you mentioned)

     

    ([\\s\\S]+)(BOPS\\d{9}CDIS-\\d{1,3})([\\s\\S]+)

    You may also use Property Expansion, then no need to escape.

     

    Define Test Case level custom property, say REGEX_CONTENT with value

    ([\s\S]+)(BOPS\d{9}CDIS-\d{1,3})([\s\S]+)

     

    Use ${#TestCase#REGEX_CONTENT} as content in Contains Assertion.

     

  • nmrao's avatar
    nmrao
    Champion Level 3

    Both of your regular expressions matches intended string, however, you might be experiencing failure.

    It is because:

    1. that is only matching partial string, but not entire response
    2. if you are providing value in the content, special characters needs to be escaped

    Below regex should work i.e., match anything before and after original regex(which you mentioned)

     

    ([\\s\\S]+)(BOPS\\d{9}CDIS-\\d{1,3})([\\s\\S]+)

    You may also use Property Expansion, then no need to escape.

     

    Define Test Case level custom property, say REGEX_CONTENT with value

    ([\s\S]+)(BOPS\d{9}CDIS-\d{1,3})([\s\S]+)

     

    Use ${#TestCase#REGEX_CONTENT} as content in Contains Assertion.

     

    • ryanshoward's avatar
      ryanshoward
      Occasional Contributor

      Hi Rao,

       

      Thanks very much for this. I used the property expansion and it worked perfectly

       

      Ryan

      • nmrao's avatar
        nmrao
        Champion Level 3
        Glad to know that helped you.