How to apply if condition on the basis of response header in karate

Viewed 272

I have to call another feature file when a particular response header exists. It is failing as when it goes to if condition and doesn't find that response header it gives error and doesn't execute that feature file. How to achieve this in Karate?

The value of this header is dynamic.

* if (responseHeaders['APP-ID'][0] !=null) karate.call('classpath:resources/userData.feature@test1', { Id: Id , value : 'success' })

Error:

<<<<
org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "0" from undefined
- <js>.:program(Unnamed:1)
1 Answers

This will work:

* if (karate.get("responseHeaders['APP-ID'][0]")) karate.call('called.feature', { id: 'foo' })

Read up on karate.get() in the documentation if you'd like to understand how this works.

That said - don't over-engineer your tests. Read this please: https://stackoverflow.com/a/54126724/143475

Related