Karate has native support for the Websocket API as an optional dependency (non-open source and commercial). Enterprise users can find more information here: Karate-Websocket.
A license is required for running (for e.g. in CI/CD) and a Karate Labs IDE subscription is required per developer seat.
This sample project shows how to handle the async nature of Websocket communication and handle a custom protocol such as STOMP. Make sure you have a .karate/karate.lic
file in place before running the example.
This is a normal Java / Maven project. To run the simple echo.feature
you can do: mvn clean test
.
There is also json.feature
that can be run as follows: mvn test -Dtest=JsonRunner
. This shows how the built-in io.karatelabs.websocket.JsonAdapter
can be used if all messages are known to be JSON.
There is an advanced example: stomp.feature
that shows how to fully control the flow of messages and the conversion of text (or bytes) to and from JSON in the test. Concepts such as how to implement a message adapter i.e. StompAdapter.java
are explained in the documentation.
To run the STOMP example, you need to download and start this Spring-Boot based project: gs-messaging-stomp-websocket
. The /complete
directory has a ready to run Maven project which you can start from the command-line like this:
mvn exec:java -Dexec.mainClass="com.example.messagingstompwebsocket.MessagingStompWebsocketApplication"
You can also open the complete
project within a Java IDE and run the main class referred in the command above as an alternative.
This will start a local web-app on port 8080 and you can also view a demo web-page at http://localhost:8080
. Now you can run the Karate test that communicates to the just-started server via STOMP over Websocket like this:
mvn test -Dtest=StompRunner
Karate has had very basic support for websockets in the open-source version which may suffice for some teams. The following information is available: