-1

I have a UI test where I want to capture the screenshot when the UI test fails. I have explored the driver.screenshot() functionality and it works well whenever I want to take a screenshot during the test. However I just want to capture the screenshot at the point when the test fails.

I had a look at the solution in the afterScenario hook mentioned in:

Karate UI standalone - can there be screenshots attached to reports on failure?

However this expects INFO.errorMessage to be present in the log files.

Here is the sample of what we see in my karate.log file and I cannot find the error being logged in under INFO.errorMessage. The structure is slightly different:

09:49:22.192 [ForkJoinPool-1-worker-3] DEBUG c.intuit.karate.driver.DriverOptions - >> {"method":"Runtime.evaluate","params":{"expression":"document.evaluate(\"\/\/div[@role='option']\", document, null, 9, null).singleNodeValue.click()","returnByValue":true},"id":361}
09:49:22.200 [nioEventLoopGroup-2-1] DEBUG c.intuit.karate.driver.DriverOptions - << {"id":361,"result":{"result":{"type":"object","subtype":"error","className":"TypeError","description":"TypeError: Cannot read property 'click' of null\n    at <anonymous>:1:84","objectId":"{\"injectedScriptId\":2,\"id\":3}"},"exceptionDetails":{"exceptionId":2,"text":"Uncaught","lineNumber":0,"columnNumber":83,"scriptId":"19","stackTrace":{"callFrames":[{"functionName":"","scriptId":"19","url":"","lineNumber":0,"columnNumber":83}]},"exception":{"type":"object","subtype":"error","className":"TypeError","description":"TypeError: Cannot read property 'click' of null\n    at <anonymous>:1:84","objectId":"{\"injectedScriptId\":2,\"id\":4}"}}}}
09:49:22.201 [nioEventLoopGroup-2-1] WARN  c.intuit.karate.driver.DriverOptions - devtools error: [id: 361, result: [type: MAP, value: {type=object, subtype=error, className=TypeError, description=TypeError: Cannot read property 'click' of null
    at <anonymous>:1:84, objectId={"injectedScriptId":2,"id":3}}]]
09:49:22.202 [ForkJoinPool-1-worker-3] ERROR c.intuit.karate.driver.DriverOptions - js eval failed twice:document.evaluate("//div[@role='option']", document, null, 9, null).singleNodeValue.click(), error: {"type":"object","subtype":"error","className":"TypeError","description":"TypeError: Cannot read property 'click' of null\n    at <anonymous>:1:84","objectId":"{\"injectedScriptId\":2,\"id\":3}"}
**09:49:22.207 [ForkJoinPool-1-worker-3] ERROR com.intuit.karate - feature call failed: classpath:AMUI/EndToEndTests/CreateAgreement.feature
arg: {"shared_agrname":"TEST_57059","local_agrname":"TEST_57059","agr_type":"CSA"}
CreateAgreement.feature:76 - evaluation (js) failed: click("//div[@role='option']"), java.lang.RuntimeException: js eval failed twice:document.evaluate("//div[@role='option']", document, null, 9, null).singleNodeValue.click(), error: {"type":"object","subtype":"error","className":"TypeError","description":"TypeError: Cannot read property 'click' of null\n    at <anonymous>:1:84","objectId":"{\"injectedScriptId\":2,\"id\":3}"}
stack trace: com.intuit.karate.driver.DevToolsDriver.eval(DevToolsDriver.java:300)**
09:49:22.596 [pool-1-thread-1] INFO  com.intuit.karate.Runner - <<fail>> feature 41 of 42: classpath:AMUI/EndToEndTests/CSA.feature

Looking at the above, I modified the afterScenario hook to:

 * configure afterScenario = function(){ if (karate.ERROR.arg) driver.screenshot() }

but that does not help. It does not take the screenshot.

Any suggestions here would be very helpful.

Also is this expected in the log file? Not having UI errors captured under the info.errorMessage structure? I have not seen any errors captured in this structure so far in my tests. Does the framework capture it this way or is it the application under test that is responsible for it?

Mihir
  • 491
  • 5
  • 21

1 Answers1

1

karate.info.errorMessage has nothing to do with log-levels, read the example please: hooks.info

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I think the issue is I am trying to capture the screenshot in a feature that is invoked using a call and karate.info does not work in such cases as clearly mentioned in the documentation. – Mihir Dec 01 '20 at 10:48
  • Thanks for the response @Peter. Can there be a workaround in such cases if we want to capture the screenshot on error in the called features? – Mihir Dec 01 '20 at 10:49
  • @Mihir may be possible if you use hooks, but have opened a feature request: https://github.com/intuit/karate/issues/1387 – Peter Thomas Dec 01 '20 at 11:04
  • Thanks for this :) @Peter – Mihir Dec 01 '20 at 11:11
  • @Mihir but I have a question - why should it matter whether it is a call or not if the top-level feature takes the screenshot ? – Peter Thomas Dec 01 '20 at 11:26
  • In my case, though the driver config is defined in the karate-config.js, when I call the afterScenario hook in the top level feature it fails with ReferenceError:"driver" not defined. Possibly because we need to handle config variables differently in the afterScenario js function?@Peter – Mihir Dec 01 '20 at 14:43
  • 1
    @Mihir hmm. anyway, this feature is implemented, so do try it out from the `develop` branch if possible. unfortunately did not make it into 0.9.9.RC1 – Peter Thomas Dec 01 '20 at 15:08