Karate call embedded function conditionally

29 Views Asked by At

I wrote an embedded function inside my feature file and on a conditional basis, I want to call the function only if the first object of the data array doesn't match dataNotFound definition.

Aprreciate your help.

Scenario: xxxxxx

#Getting data array from DB

  • def lenArray = data.length
  • def deleteTokens = """ function(lenArray) { for (var i = 0; i<lenArray; i++) { karate.call('deleteTokensCreated.feature', {ownerId: data[i].owner_id, token: data[i].token}); } } """
    • def dataNotFound = {"message": "Data not found!"}
    • def deletedTokens = call deleteTokens lenArray

" eval if (data[0] != dataNotFound ) call deleteTokens lenArray"* doesn't work.

Please, consider I am a business analyst who is trusted with test automation task and has no prior experience with either Java or Karate.

1

There are 1 best solutions below

1
Peter Thomas On BEST ANSWER

Karate deliberately limits the amount of "logic" you can introduce, please read this for some context, and it may help you navigate this new world better: https://stackoverflow.com/a/54126724/143475

Here's how I would write your scenario, I hope it gets you going:

* def fun = function(){ karate.log('hello world') }
* def data = [{ message: 'not found' }, {message: 'foo'}, {message: 'bar'}]
* if (data[0].message === 'not found') fun()