Reputation: 388
I am trying to write Web socket test cases using Karate. In my application the client will be subscribing to various destinations ("/topic/notifs", "/topic/alerts", "/topic/errors") etc.
Scenario: text messages
And def socket = karate.webSocket('ws://my websocket url')
When socket.send('hello world!')
And def result = socket.listen(5000)
Then match result == 'hello world!'
When socket.send('another test')
And def result = socket.listen(5000)
Then match result == 'another test'
I have tried using the above code snippet. but the result is null.. can you please help me how to subscribe to destination and write test cases for that ?
Upvotes: 1
Views: 1162
Reputation: 58098
You can create any number of websocket instances like this:
* def socket1 = karate.webSocket('ws://echo.websocket.org')
* def socket2 = karate.webSocket('ws://echo.websocket.org')
* socket1.send('hello world!')
* socket2.send('another test')
* def result1 = socket1.listen(5000)
* match result1 == 'hello world!'
* def result2 = socket2.listen(5000)
* match result2 == 'another test'
So now it is up to you how to manage what to send and when to wait for the listen()
method to un-block. And please read the docs: https://github.com/intuit/karate#websocket
Upvotes: 1