ajoseph
ajoseph

Reputation: 45

Logging Messages from Java Class back to the Karate Report

We have a scenario where we have to post a json request and then validate few actions in the UI. So we have a karate feature file which hits a request and after that we are invoking a java class from within the feature file. The java class would run the Selenium Webdriver test of us. In the java methods we have few Assertions/Info Messages which we would like to log back to the Karate Reports.

Is there a way in Karate with which we can write these messages from my Java Class to the Karate Test Reports?

Upvotes: 2

Views: 6505

Answers (3)

CVA
CVA

Reputation: 1567

It worked for me by passing the karate object from the javascript file to the java code. With this I was able to call the karate.log function from the Java code. This way I could see the messages on the report.

Upvotes: 1

ajoseph
ajoseph

Reputation: 45

We did try logging back to “com.intuit.karate” as shown below and its logging to the ‘overview-features.html’ report

Logger in Java class :

private static final Logger logger = LoggerFactory.getLogger("com.intuit.karate");

However we made an observation that if an exception is thrown from the java class the exception would appear first and then followed by all the information that is logged, both looks to exist as a different piece in the report.

For e.g

This is sample method with logger in the java class which i am invoking from the feature file

 public void myTestMethod() {
    logger.info("Starting Test");
    logger.info("Setting path of chrome driver");
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); //driver doesnt exist in this path
    logger.info("invoking chrome"); // we would expect the exception to be thrown after this line
    driver = new ChromeDriver();
    logger.info("perform search in google");
    driver.get("http://www.google.com");
    driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
    driver.findElement(By.id("lst-ib")).submit();
    driver.quit();
}

But in the reports the exception appears first and then all the information that is logged from the java class. Both looks like two different pieces in the report, please see the report here https://www.screencast.com/t/bBhAIj7WKj

In the above case would it be possible to have the exception thrown after the line where we log” invoking Chrome” i think this would make it easier to identify the test failures from the reports. Please let us know if this would be possible

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58058

If you use the Karate parallel runner it is designed to collect anything logged to package com.intuit.karate, which will then appear in the cucumber-html-report.

So you can try using the same slf4j Logger - and it might work.

A better way might be to just return a Map back to Karate from your Java code, with all the information you need to log. Then all you need to do is use the print keyword. I would recommend this approach actually, it should be less complicated and it does not assume that you are using the Cucumber HTML reporting. You could even do Karate asserts with the match keyword on the JSON that you get, refer to this example: dogs.feature.

Upvotes: 2

Related Questions