3

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.

Parth
  • 367
  • 6
  • 18

2 Answers2

3

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

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Will the `afterStep` hook work to log the value from `prevRequest.url` OR is the variable out of scope at that point?? @Peter Thomas – djangofan Aug 05 '21 at 23:47
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#hook or
  • com.intuit.karate.Runner.Builder#hooks

method to add a hook to your karate test case execution.

Peter
  • 4,752
  • 2
  • 20
  • 32
  • Is ExecutionHook still recommended to use as a startup hook? I want to generate some JSON resources using Java code and load these into a resources folder so they're available to my BDD's. I can't see the class in the karate-core Maven dependency version 1.1.0?? – Orby Apr 07 '22 at 15:22
  • 1
    @Orby : See my update. – Peter Apr 07 '22 at 18:31