1

Apologies if this has been answered before, I spent most of today looking and found some similar issues but nothing that answered my specific problem.

I am posting to a TransactionAPI with 'TransactionID'-> sleeping thread for 10 minutes (because this is ensures that the transaction posts in the next step) -> calling read api with

Then match response.results[*].trans.id contains TransactionID

this currently works, but sleep isn't great.. and I wanted to take full advantage of karate. The [*] is because the new TransactionID appears in the final index of the returned array response. so the first time it calls the readAPI it gets an array of [5], and the new TransactionID will appear in [6] (and I couldn't figure out how to make it get array.length + 1 and wait for that one to appear)

Reading the responses here Karate framework retry until not working as expected and the linked ones inside i tried a few things:

And retry until response.results[-1].trans.id.includes('TransactionID')
And retry until response.results[(@.length-1)].trans.id.includes('TransactionID')

These return an error:

org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:18 Expected an operand but found error

I tried the in-line javascript function mentioned in the above link, but could not get it to work either.

Javascript function mentioned:

* def isValid = function(x){ return karate.match(x, { tokens: '##[_ > 0]' }).pass }
# ...
And retry until isValid(response)

1 Answers1

1

So indexing to -1 will not work because that is JsonPath and not JavaScript.

Try this:

# set this by doing a call before if needed
* def prevLength = response.length
* def isValid = function(x){ return x.length > prevLength }
# ...
* retry until isValid(response)

My guess is that it is more than enough to achieve what you want.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks for the reply @peter-thomas! That doesn't seem to work.. I am positive I am doing something incorrectly but can't seem to figure it out. ```Given url readTransactionsEndPoint And headers And request """ """ * def prevLength = response.length * print 'prevLength is ' + prevLength * def isValid = function(x){ return x.length > prevLength } # ... And retry until isValid(response) When method post Then status 200``` returns "JS Failed, response is not defined" karate.get('response.length') is also not working – HopefullyHelpful Feb 02 '23 at 00:19
  • 1
    @HopefullyHelpful I give up. follow this process if you can: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Feb 02 '23 at 02:11