Forum Discussion

fazlook's avatar
fazlook
Frequent Contributor
8 years ago
Solved

Regular expression XML tags in Contains Assertion

Hi guys,

 

I am trying to match a date format with contains assertion in SoapUI but it does not work for me.

 

Date format I want to match is: <InsertDate>2017-01-03T17:48:45</InsertDate>

 

I tried <InsertDate>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}</InsertDate> with no luck. 

 

Any idea how I do that ?

  • There is a tricky part using Contains Assertion in association with Regex. i.e., the regex that user provides is matched against the entire content of the response. Hence you see the assertion failed.

     

    If I would have to do it, I prefer to do in groovy, and here is the one:

     

    https://github.com/nmrao/groovyScripts/blob/master/regex/dateSample.groovy

     

    If you go for Script Assertion, then remove the xml, instead use context.response in the above script.

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    There is a tricky part using Contains Assertion in association with Regex. i.e., the regex that user provides is matched against the entire content of the response. Hence you see the assertion failed.

     

    If I would have to do it, I prefer to do in groovy, and here is the one:

     

    https://github.com/nmrao/groovyScripts/blob/master/regex/dateSample.groovy

     

    If you go for Script Assertion, then remove the xml, instead use context.response in the above script.

    • fazlook's avatar
      fazlook
      Frequent Contributor

      Thank you much for the help. So because my date is not fixed, I would ignore the XML in your example even though I would love to know why you used it that way (is the envelop something already built in).

       

      Back to my question.....I should be using something like:

       

      //Check if the response has headers
      assert messageExchange.responseContent, "There is no content in the response"

       

      def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

       

      //using the below holder we can get hold of the all elements in response xml of the latest messageExchange.
      def responseHolder = groovyUtils.getXmlHolder(messageExchange.responseContent)

       

      //Get the dates
      def  insertDateFromResponse = responseHolder.getNodeValue("//InsertDate")

       

      def pattern = "(InsertDate>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}<\\/)"

       

      assert insertDateFromResponse =~ pattern, "Response does not have matching pattern"

       

      Now should I just remove the InsertDate from pattern ?

       

      **UPDATE**

       

      I think I can just use:

       

      //Check if the response has headers
      assert messageExchange.responseContent, "There is no content in the response"

      def pattern = "(InsertDate>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}<\\/)"

      assert messageExchange.responseContent =~ pattern, "Response does not have matching pattern"

      • nmrao's avatar
        nmrao
        Champion Level 3
        That is what I gave in the script.

        messageExchange.responseContent, context.response should be same.

        You can check by having:
        assert messageExchange.responseContent == context.response