karate icon indicating copy to clipboard operation
karate copied to clipboard

Unable to user handler in websocket tests in 1.4.1

Open jrx-sjg opened this issue 8 months ago • 5 comments

Hi all.

I'm unable to use a handler when using karate.websocket(), as intented in the documentation.

The test is:

@ws-test
Feature: public test
    
@websocket
Scenario: only listening to websocket messages
    * def handler = function(msg) { karate.log('received:', msg); return msg.startsWith('hello'); }
    * def socket = karate.webSocket('wss://ws.postman-echo.com/raw', handler)
    * socket.send('hello world')
    * listen 5000
    * print listenResult

And the error I'm getting is:

[ERROR] Failures: 
[ERROR]   ExamplesTest.testParallel:15 js failed:
>>>>
01: karate.webSocket('wss://ws.postman-echo.com/raw', handler)
<<<<
org.graalvm.polyglot.PolyglotException: Cannot invoke "org.graalvm.polyglot.SourceSection.getCharacters()" because the return value of "org.graalvm.polyglot.Value.getSourceLocation()" is null
- com.intuit.karate.graal.JsFunction$Executable.<init>(JsFunction.java:95)
- com.intuit.karate.graal.JsFunction$Executable.<init>(JsFunction.java:89)
- com.intuit.karate.graal.JsFunction$Instantiable.<init>(JsFunction.java:132)
- com.intuit.karate.graal.JsLambda.<init>(JsLambda.java:37)
- com.intuit.karate.core.ScenarioBridge.webSocket(ScenarioBridge.java:1110)
- com.intuit.karate.core.ScenarioBridge.webSocket(ScenarioBridge.java:1101)
- <js>.:program(Unnamed:1)

classpath:examples/websocket/websocket.feature:7 ==> expected: <0> but was: <1>

This is something that only happens in karate 1.4.1, using 1.4.0 works normally and it calls the handler function and retrieve the messages as intended.

I've tried to use karate.toJava to call the handler, but didn't help.

I've tested everything in a new minimal maven karate template as explained in https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue, and you can replicate everything in the code in the attached .zip using the command: mvn compile test

karate-template.zip

jrx-sjg avatar Jun 11 '24 12:06 jrx-sjg