1

I am using ExecutionHook from karate

Need below details like Scenario Name, Tags, Endpoint, Request, Response, Status and Error

@Override
    public void afterScenario(ScenarioResult result, ScenarioContext context) {
        System.out.println("This is into Hook, scenario tags is "+ result.getScenario().getTags());
        System.out.println("This is into Hook, scenario name is "+ result.getScenario().getName());
        System.out.println("This is into Hook, scenario EndPoint is "+ context.getHttpClient());
        System.out.println("This is into Hook, scenario request is "+ context.getPrevRequest());
        System.out.println("This is into Hook, scenario response is "+ context.getPrevResponse());
        System.out.println("This is into Hook, scenario status is "+ result.isFailed());
        System.out.println("This is into Hook, scenario Error is "+ result.getError());
    }

Issue : Unable to get exact details for EndPoint, Request and Response. Any Help?

Update: Execution is on hold when using below methods

System.out.println("This is into Hook, scenario request is "+ context.getRequestBuilder().getBody().getAsJsonDocument());
        System.out.println("This is into Hook, scenario response is "+ context.getPrevResponse().getBody().toString());

enter image description here

Sandeep P
  • 109
  • 1
  • 10

1 Answers1

1

There can be multiple HTTP calls within a Scenario: https://stackoverflow.com/a/46080568/143475

Maybe you are looking for afterStep: https://stackoverflow.com/a/59080128/143475

The short answer to your question: look at the getPrevRequest() and getPrevResponse() methods on ScenarioContext. The info is there, for example the URL is in getPrevRequest().getUri()

EDIT: the ExecutionHook is intended for advanced users and hence is not documented. Also, the methods on ScenarioContext should be deemed as for "internal use" and are subject to change in future versions of Karate. Those looking for specific needs are advised to figure it out on their own or contribute code to Karate (open-source) via pull-requests.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • For EndPoint `context.getRequestBuilder().getUrlAndPath());` is working. No luck for request and response. Strange is below methods are halting execution context.getRequestBuilder().getBody().AnyMethod(),,, context.getPrevResponse().getBody().AnyMethod() – Sandeep P May 03 '20 at 07:27
  • i have updated original post, please have a look on that. – Sandeep P May 03 '20 at 07:32
  • @SandeepPadala I have nothing more to add, these are intended to be for internal use - I tried to give you some pointers since you are so keen on trying to re-invent some framework. please go through the source code and figure this out or contribute code to karate as it is open source. I really don't understand why you need to do all this given that karate has reports and in-line logs built-in. maybe karate is the wrong choice for you, just write your own framework – Peter Thomas May 03 '20 at 07:45
  • Hey Peter, Not like that we have issues in publishing report on jenkins server. We cant disable some security to enable reports with css,js and images to load on jenkins server. This is where we are exploring something to have a standard reporting for Karate Framework. Nothing apart from this, btw Karate is a great framework !! – Sandeep P May 03 '20 at 07:53
  • 1
    @SandeepPadala then you should probably look at the `Results` and `FeatureResult` classes instead: https://github.com/intuit/karate/blob/develop/karate-core/src/main/java/com/intuit/karate/core/HtmlFeatureReport.java – Peter Thomas May 03 '20 at 09:57
  • 1
    @SandeepPadala and also see this: https://twitter.com/KarateDSL/status/1237797240686522369 – Peter Thomas May 03 '20 at 10:11
  • Thanks Peter, we will have a look into that! – Sandeep P May 03 '20 at 14:09