1

The goal is to do soft assertion on each JSON field.

    * def KAFKA_TOPIC = "topic1"
    * def kafkaExpected = {field1:"value1",field2:"value2",field3:"value3"}
    * def kafkaActual = {"topic1":[{field1:"value1",field2:"x",field3:"y"}]}

    * configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }

    * match (kafkaActual[KAFKA_TOPIC]) == ['#(kafkaExpected)'] <-- do we have one-liner like this to do soft assertions on all fields?

    * configure continueOnStepFailure = false

Output:
$[0].field2 | not equal (STRING:STRING)
      'x'
      'value2'

Instead of doing it 1 by 1.

    * match (kafkaActual[KAFKA_TOPIC])[0].field1 == kafkaExpected.field1
    * match (kafkaActual[KAFKA_TOPIC])[0].field2 == kafkaExpected.field2
    * match (kafkaActual[KAFKA_TOPIC])[0].field3 == kafkaExpected.field3

Output:
match failed: EQUALS
  $ | not equal (STRING:STRING)
  'x'
  'value2'
match failed: EQUALS
  $ | not equal (STRING:STRING)
  'y'
  'value3'

And whats weird is that on terminal logs it only printed one assertion, either on both approach.

  $ | not equal (STRING:STRING)
  'y'
  'value3'

enter image description here enter image description here

Trying to use Karate.forEach but seems like not the right path. enter image description here

Don
  • 163
  • 12
  • 1
    making sure you read this, that may answer some, if not all questions here: https://stackoverflow.com/a/54108755/143475 – Peter Thomas Feb 07 '22 at 04:12
  • thanks for the link but in this case I dont want to use scenario outline to loop as well as table as the data is dynamic. I just want toop in each json field and validate with soft assertion. Im thinking karate.forEach but seems like not the right path. – Don Feb 07 '22 at 20:20
  • @PeterThomas do we have plans to implement a one-liner for this? something like match response contains json or a list but with soft assertion enabled? – Don Feb 08 '22 at 23:44
  • no we don't. I request you to please think about this. is this really the most important thing you need to be happy with your test automation ? I've written a lot about my opinion at the link. finally, you are most welcome to contribute code to improve things, this is all open-source. – Peter Thomas Feb 09 '22 at 04:32

1 Answers1

1

Found a solution from this link provided by Peter. I just need to transform the JSON to list of key, value format and use it as data source.

From:
{field1:"value1",field2:"value2",field3:"value3"}

Transformed To: 
[{key:"field1",value:"value1"},{key:"field2",value:"value2"},{key:"field3",value:"value3"}]

Function used and usage:

 * def input = INPUT
 * def func = 
  """
    function(obj){
      var output = [];
      for (var i in obj) {
        output.push({key: i, value: obj[i]});
      }
      return output
    }
  """

    * json kafkaAttributes = func(input)
    * configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }
    * karate.call('kafka.feature@validateFieldsAndValues',kafkaAttributes) 
    * configure continueOnStepFailure = false

@validateFieldsAndValues
Scenario:
    * match (response[KAFKA_TOPIC][0][key]) contains value
Don
  • 163
  • 12