3

What is the easiest way to attach screenshots to Karate JSON report?

We test REST API and our tests cases require to perform some operation on web side(log into web application, perform verification, take screenshot). We've handled with it writing some static methods which are called from Karate using Java interop mechanism. What I want to do is write some generic, reusable code to attach screenshots taken during test execution. I don't want to repeat steps:

* def bytes = getScreenBytes()
* eval karate.embed(bytes,'image/jpg')

each time when screenshot should be attached to report. It would be perfect to attach taken screenshots directly from java code to not to expose such details in scenarios. Is there such possibility?

Adam
  • 81
  • 2
  • 7

1 Answers1

1

Since you seem to be an advanced user - you can try this. The karate object is of type ScriptBridge and you should be able to pass it into your custom Java code (which we normally do not recommend). Now you can call methods in Java such as embed() etc.

I also see nothing wrong with writing a JS wrapper so that the screen-shot taking becomes a one-liner in your scenarios.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Peter, one more question related to the topic. Is there any possibility to attach more than one screenshot at once and display them all below karate step in report? I've followed your suggestion and it works but when I try to attach more than one screen only the last one is visible in the report. I suppose that it's because Karate store embed attached file as object not a list of object. – Adam Jul 16 '19 at 09:06
  • 1
    @Adam yes this is current limitation of karate: https://github.com/intuit/karate/issues/828 as a workaround you can use html and inline images yourself – Peter Thomas Jul 16 '19 at 17:13