Forum Discussion

sunilkumarcm's avatar
sunilkumarcm
New Contributor
7 years ago

Wild card in Contains assertion

I have lot of "contains" assertions in my scripts. like assertion content as  <ns3:rte>100</ns3:rte>

 

But unfortunately because of schema changes ,namespace prefix have changed.I have to change ns3 to ns4.And chances are there it may change again to ns7 or ns10 in future

 

Can I use wild care for namespace prefix  like <*:rte>100</*:rte>  or please tell me how I can use regular expression in contains assertion

  • JHunt's avatar
    JHunt
    Community Hero

    With Contains, if you select regular expression, then there needs to be a complete match to the whole response body, so something like::

     

    .*<\w+:rte>100<\/\w+:rte>.*

    Rather than using contains, use XPath or another type of assertion (script assertion?)

     

    When you use XPath and declare the namespaces, the prefixes are yours to make up (or let SoapUI make them up for you), and then those declarations are responsible for mapping the prefixes to the right namespaces. So even if more namespaces are added, as long as you don't change your declarations, then your existing assertions will still work.

     

    //myns1:rte[1]

    And as for script assertions, the default when using XmlSlurper is to ignore namespaces and just assume there's no conflicts.

     

     

    assert new XmlSlurper()
    .parseText(messageExchange.responseContent)
    .Body.something.someThingElse.rte
    .find {it == '100'} == true

     

     

     

    • sunilkumarcm's avatar
      sunilkumarcm
      New Contributor

      I have checked "Use token as regular expression".But even then if I use regular expression in contains assertion, it is not working

  • JHunt's avatar
    JHunt
    Community Hero

    OK, I discovered that the Contains assertion uses

     

     

    content.matches(replToken)

    i.e. java's String.matches(String), and the default RegEx behaviour for Java is different to Groovy:

     

     

    String content = '''
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:sam="http://www.soapui.org/sample/"> <soapenv:Header/> <soapenv:Body> <sam:buyResponse> <sam:buyResponseContent> <purchasestatus> <rte>100</rte> <stockStatus>?</stockStatus> <expectedDelivery>?</expectedDelivery> </purchasestatus> </sam:buyResponseContent> </sam:buyResponse> </soapenv:Body> </soapenv:Envelope>''' String replToken = /.*rte.*/ "java: ${content.matches(replToken)}, groovy: ${(content =~ replToken).find()}"
    java: false, groovy: true

    The difference is in whether . matches to newline character. You can specify that it should in your Regex with (?s):

     

    String replToken = /(?s).*rte.*/
    "java: ${content.matches(replToken)}, groovy: ${(content =~ replToken).find()}"
    java: true, groovy: true

    Remember to write a pattern that matches to the whole response.