'Karate: Is there a http-request hook in karate, that gets called automatically after every API call, and whose behaviour I can modify? [duplicate]
I want a common implementation to write the: request-body, request-method & response-body to an output file for all the HTTP requests that I make in any of the karate feature files.
I have read through the documentation, and the closest option that I found was the afterScenario hook. However this doesn't help me in cases where I want to make multiple API calls in the same scenario.
I also tried using karate.prevRequest object by passing it to java function that I wrote. While this helps me capture the information that I want, I have to keep calling my java function after every API call, which I don't want to do.
I want to be able to write a hook that gets called after every API call and gives me access to request-body, request-headers, response-headers, response-body, request-method and request URL.
This should be fairly straight forward, clearly I seem to be missing something here.
Solution 1:[1]
First, I personally think that this is a mis-use of Karate, and you seem to be more interested in reports than actually doing testing. Take some time to think about it. Karate has excellent report integration and you are just wasting your time doing this in my very honest, sincere opinion. No one has asked for this.
Anyway. There is a new ExecutionHook in 0.9.5.RC4, details here: https://github.com/intuit/karate/issues/970#issuecomment-557443551
You can implement afterStep, see if it is a method step and then do this. If you need changes, open an issue. Also see: https://stackoverflow.com/a/60944060/143475
EDIT: just remembered - in the develop branch we added a way to intercept all requests and responses: https://twitter.com/karatedsl/status/1195240779213496320 - will be happy to get your feedback and see if we can merge this concept with the above hook - or improve the interfaces if needed
EDIT: in many cases, the afterScenario may be sufficient, note that you can call any Karate feature, which means you can do HTTP requests: https://stackoverflow.com/a/51467470/143475
EDIT - the hooks API has changed for 1.0: https://github.com/intuit/karate/wiki/1.0-upgrade-guide
Solution 2:[2]
The ExecutionHook supports quite a lot different use cases.
Steps can be aborted or changed
Request and Response can be sniffed
See ExecutionHookExampleTest on how to use an ExecutionHook.
The API has changed. With karate version 1.1.0 (correct me, if I'm wrong)
It's now com.intuit.karate.RuntimeHook and you can use
com.intuit.karate.Runner.Builder#hookorcom.intuit.karate.Runner.Builder#hooks
method to add a hook to your karate test case execution.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
