Saurav Kumar
Saurav Kumar

Reputation: 91

How to verify substring in a field in array based on condition

There is always multiple ways to do things in karate and this is what I was able to think off but what I am trying to do also doesn't work unfortunately. Would appreciate an alternate approach.

Now what we want is that we have the response which is an array and then we want to verify only for certain fields where based on the id value the event string will be different and we have to verify the substring in event for that particular id.

The first sample works since it is not checking the substring but complete string. The second scenario doesn't work where I am doing the regex of the substring to match . Is there any other alternate way to do this if this is not supported?

Scenario: Matching complete string works (Working)

* def response = [{id:1 , "event":"some data abc something"},{id:2 , "event":"some xyz something"},{id:3 , "event":"some 123 something"},{id:4 , "event":"some 567 something"}]

* def verify = [{id:2, "event" : "some xyz something"},{id:4, "event" : "some 567 something"}]

* match response contains verify

Scenario: Matching via regex doesn't work to check the substring(Not working)

  * def response = [{id:1 , "event":"some data abc something"},{id:2 , "event":"some xyz something"},{id:3 , "event":"some 123 something"},{id:4 , "event":"some 567 something"}]

  * def verify = [{id:2, "event" : "#regex (?i)(xyz)"},{id:4, "event" : "#regex (?i)(567)"}]

  * match response contains verify

Upvotes: 1

Views: 896

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58078

Try this:

* def verify = [{id:2, "event" : "#regex (?i).*xyz.*"},{id:4, "event" : "#regex (?i).*567.*"}]

Upvotes: 1

Related Questions