S.Kras
S.Kras

Reputation: 43

SoapUI - how to assert a JSON element containing a custom property

I use SoapUI NG Pro (ReadyAPI-1.1.0.) and try to use the UI to set all my required assertions.

What I try to do is to check if a list (JSON) contains an element that is defined in a soapUI custom property. To get all elements from the list I use the path: $.devices[*].deviceName. Now I like to check if the string from the customer property ${#Project#devname.1} is part of the list.

To write the assertion I found two possibilities, but both of them do not really work.

  1. Use JsonPath Match: The menu servers an input field for the path and one for the expected result and a check box named “Allow Wild…”. I filled the path with $.devices[*].deviceName and the expected result with ${#Project#devname.1}. I also checked the check box to support wildcards. The result is a positive assertion. SoapUI does what I expect. BUT! If I save the project and reopen it, the check box is not checked any more. I have to go over all my assertions and check the boxes again. This is not a solution because I have about 100 of these assertions.
  2. Use JsonPath RegEx Match: A nice way not use the broken “Allow Wild…” checkboxes should be to use a regular expression instead. Therefore I use the “JsonPath RegEx Match” and put the right path $.devices[*].deviceName into the input field and used this regular expression: ${#Project#devname.1}. Sadly this does not work, because SoapUI does not expand the Custom Properties before applying the regular expression. I did not find anything in the documentation explaining how to use custom properties in regular expressions. Could you help here?

In any case. What is a good solution for this type of assertions?

Upvotes: 1

Views: 5195

Answers (1)

Andrei Hrabun
Andrei Hrabun

Reputation: 11

You can try to use JsonPath Match assertion with expression

 $.devices[?(@.deviceName=='${#Project#devname.1}')].deviceName.

It will try to find device with
deviceName == devname.1. If exists = true, if not - "Comparison failed for path "...", expecting [%your deviceName%], actual was [[]]"

Upvotes: 1

Related Questions