1

I want to be able to write log statements, that get added to the karate.log file as well as to the Cucumber Reports that get generated when using standalone karate.jar.

When I use karate.log from a javascript function it only adds the log statement to the karate.log file and not the cucumber report.

I have also tried to do this from a java function as well by using both slf4j logger as well as the com.intuit.karate.Logger class. However both of these only add logs to the karate.log file and not to the cucumber reports.

I need this because I am writing some common code for which I don't want my QA-Engineers to write * print <> statements in the karate feature files.

I also looked at the com.intuit.karate.core.ScriptBridge.log(Object... objects) method which is what I am assuming gets called when you call karate.log(..), it looks like it should work, but it isn't working for me.

I am using karate-0.9.4, and here's what my karate-config.js looks like

function conf() {

    var env = karate.env // set the environment that is to be used for executing the test scripts
    var host = '<some-host-name>';
    var port = '443';
    var protocol = 'https';
    var basePath = java.lang.System.getenv('GOPATH') + '/src/karate-tests';

    // a custom 'intelligent' default
    if (!env) {
        env = 'dev';
    }

    var applicationURL = ((!port || port == '') || (port == '80' && protocol == 'http') || (port == '443' && protocol == 'https'))
    ? protocol + '://' + host
    : protocol + '://' + host + ":" + port;

    // Fail quickly if there is a problem establishing connection or if server takes too long to respond
    karate.configure('connectTimeout', 30000);
    karate.configure('readTimeout', 30000);

    // pretty print request and response
    //karate.configure('logPrettyRequest', true);
    //karate.configure('logPrettyResponse', true);
    karate.configure('printEnabled', true);

    // do not print steps starting with * in the reports
    //karate.configure('report',{showLog: true, showAllSteps: true });

    // Turn off SSL certificate check
    karate.configure('ssl', true);

    var config = {
        env: env,
        appBaseURL: applicationURL,
        sharedBasePath: basePath
    };

    karate.log("config.sharedBasePath = ", config.sharedBasePath)
    karate.log('karate.env = ', config.env);
    karate.log('config.appBaseURL = ', config.appBaseURL);

    return config
}
Parth
  • 367
  • 6
  • 18

2 Answers2

1

This is because of a bug in karate-0.9.4 which seems to be partially fixed in karate-0.9.5.RC4 release. I have opened a ticket for it on GitHub - https://github.com/intuit/karate/issues/975

Parth
  • 367
  • 6
  • 18
0

I just tried this in 0.9.5.RC4. If you are looking for something more than this - it needs a change in Karate. You are welcome to contribute. I have to say that I'm surprised (and somewhat annoyed) to see these requests. Why are you so concerned about pretty reports instead of focusing on testing. I'd like you to think about it.

This other discussion may be a related reference: https://github.com/intuit/karate/issues/951 | https://github.com/intuit/karate/issues/965

If you really want to pursue this, you can look at the "hook" interceptor mentioned in this comment: https://github.com/intuit/karate/issues/970#issuecomment-557443551

So in void afterStep(StepResult result, ScenarioContext context); - you can modify the StepResult by calling appendToStepLog(String log).

working in 0.9.5.RC4

EDIT: other references:

https://stackoverflow.com/a/57079152/143475

https://stackoverflow.com/a/47366897/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Hey Perter - thanks for the quick responses. However, the solution that you have provided does not help. I want to call `karate.log` from a javascript function and not directly in the feature file. Is there a way to do that? – Parth Nov 28 '19 at 15:53
  • @parth6 no. and please read the rest of my links. and this is open source, kindly consider contributing – Peter Thomas Nov 28 '19 at 16:57
  • 1
    For everyone's future reference: The reason why this `karate.log` prints only to log file and not to the console (a.k.a cucumber reports) is because of a bug in `karate-0.9.4`, however this seems to be fixed in `karate-0.9.5.RC4` – Parth Nov 28 '19 at 17:42
  • @parth6 In the 0.9.5.RC4 and 0.9.5.RC5 the logs are printed in the console but not on the karate.log file. I use the same xml which was mentioned in official site – Ashok kumar Ganesan Jun 25 '20 at 06:00
  • everyone, please try 0.9.6.RC3 before drawing any conclusions – Peter Thomas Jun 25 '20 at 06:11