Forum Discussion

Freddy's avatar
Freddy
Occasional Contributor
12 years ago

[Resolved] Using regular expressions in assertions

Hi guys,

I am new to SoapUI and using the free version. So far things have been going quite well and I made a few (contains and not contains) assertions to validate xml responses and they work fine.

Next step is to add an assertion where I first read a (dynamic) value between 2 xml-tags and then check that value against expected values.

For example, let's take the following tags and value:

<CategoryA> 15 </CategorieA>

How would I create an assertion that first reads the value between <CategorieA> and </CategorieA> and then checks if the value is higher than 10 but lower than 20?
Can this be done with only an assertion and a regular expression? I cannot find anything similar in manuals or on the web.

Any help would be highly appreciated.

Kind regards,
Freddy

6 Replies

  • Freddy's avatar
    Freddy
    Occasional Contributor
    Thank u very much. I will have a look at that.
  • Freddy's avatar
    Freddy
    Occasional Contributor
    Ok, I got as far now as using an XPath Match in SoapUI in which I am reading a value from the xml response.

    Basically that looks like:

    declare namespace ns1='...namespace...';
    ns1:Node1[1]/ns1:SubNode[1]/ns1:ElementValue[1]

    This returns the desired value. Final step is that I would like to catch this value into a variable which I would then like to compare to a project property.
    I have been trying to define variables in my XPath Match but have not been succesful so far.

    Could anyone please give me a short example on how to do this?

    Thanks again a lot in advance!
  • You can catch the value using a groovy script and do all validations you need.
    for ex:
    you can try the below script in the script assertion:

    def holder = new XmlHolder( messageExchange.responseContentAsXml )
    holder.namespaces["ns1"] = "your namespace"

    def nodevalue = holder.getNodeValue( "//ns1:Node1[1]//ns1:SubNode[1]//ns1:ElementValue[1])

    this node value will get the element value and you can do validations with the value you got. Groovy is much similar to java or javascript.
  • Freddy's avatar
    Freddy
    Occasional Contributor
    Thank you very much Saranya! I got it to work with Groovy script!