1

Hi Karate community wish you can help me with this doubt ,I'm trying to create some evidence for my test cases ,because the organization where I'm working is not accepting neither cucumber or karate report.

The problem happen when my javaScript function use the karate.prevRequest.body functionality of karate, this brings a null value, but I already try it at feature level and work perfectly, I really don't know what's going on, I hope someone know what I'm doing wrong.

Here the piece of code and responses I'm getting from the feature and from the javaScript.

from Feature

     * def requestBody = karate.prevRequest.body
     * def requestString = new java.lang.String(requestBody, 'utf-8')
     * print requestString

Response

11:09:15.800 [ForkJoinPool-1-worker-3] INFO com.intuit.karate - [print] {"enrollmentTimeStamp":"2021-03-26T11:09:14","customer":{"customer":"2021032611306","fullName":"Elizabeth Chase Olsen"}}

javaScript under the same feature

      And configure afterScenario =
      """
      function() {
            if( karate.prevRequest != null){
            var requestBody = new java.lang.String( karate.prevRequest.body , 'utf-8');
        }
            var requestMethod = karate.prevRequest.method;
            var requestHeaders = karate.prevRequest.headers;
            var request = requestMethod + "\n" +requestHeaders+ requestBody;
            karate.log(requestBody);
            var reportResults = Java.type('Features.reportResults').writeResultsToFilePretty(requestMethod, requestHeaders, requestBody, response, karate.info.scenarioName, karate.info.featureFileName);
      }
      """

Response:

11:09:16.040 [ForkJoinPool-1-worker-3] ERROR com.intuit.karate - javascript function call failed: java.lang.NullPointerException
11:09:16.043 [ForkJoinPool-1-worker-3] ERROR com.intuit.karate - failed function body: function() {
        if( karate.prevRequest != null){
        var requestBody = new java.lang.String( karate.prevRequest.body , 'utf-8');
    }
        var requestMethod = karate.prevRequest.method;
        var requestHeaders = karate.prevRequest.headers;
        var request = requestMethod + "\n" +requestHeaders+ requestBody;
    karate.log(requestBody);
        var reportResults = Java.type('Features.reportResults').writeResultsToFilePretty(requestMethod, requestHeaders, requestBody, response, karate.info.scenarioName, karate.info.featureFileName);
}
11:09:16.044 [ForkJoinPool-1-worker-3] WARN  com.intuit.karate - afterScenario hook failed: javascript function call failed: java.lang.NullPointerException

1 Answers1

0

First please switch to 1.0 - it is quite likely this is fixed or work better: https://github.com/intuit/karate/wiki/1.0-upgrade-guide

Second: if you want custom reports and are willing to do some digging into Java code, see this: https://stackoverflow.com/a/66773839/143475

Third, maybe this will give you the "evidence" your organization is demanding, but again, you should be willing to do some custom code: https://twitter.com/KarateDSL/status/1338892932691070976

Fourthly, there is something called a RuntimeHook for advanced users, so if you understand how to use that - you can get the details of every single HTTP request made by Karate.

Finally, if none of the above work that means you should contribute some code to Karate :)

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248