How to implement conditional When method in Karate

Viewed 81

I would like to reuse a feature both for POST-ing and PUT-ing a JSON Object. In order to achieve that I am trying to use a condition in the call:

Given param admin = admin
And request role
When method (role.id == null) ? karate.POST : karate.PUT

The error I get:

no step-definition method match found for: method (role.id == null) ? karate.POST : karate.PUT

I checked the documentation and the examples and search for the solution here, but I did not find an answer to this question. Thanks in advance for the help.

1 Answers

You can use a variable for the method step:

* def action = 'GET'
* url 'https://httpbin.org/get'
* method action

Other than that I have no suggestions. I strongly advise you to not do this kind of "re-use" as it leads to un-readable and un-maintainable tests. Please read this once: https://stackoverflow.com/a/54126724/143475

Related