History of the issue:
Communication via WebSocket is used to implement real-time features in web applications (chats, notifications, live statistics). Over time, with the growth of dynamic web UI, the need for automated testing of web-socket connections and message exchange arose. Previously, manual or low-level scripts were used for this, but now more specialized tools have emerged (for example, WebSocket clients for testing frameworks).
Problem:
The main challenge is that the web-socket depends on the real-time state of the server, the stream is bidirectional, making it more difficult to synchronize sending/receiving messages and validating their correctness. There is also the issue of testing handling of unexpected connection drops, reconnection, latencies, order delivery, and proper functionality in the case of competing connections.
Solution:
Use specialized libraries (for example, ws for Node.js, WebSocket API in Python) and integrate them into the autotest pipeline.
Build scenarios that control the stages of handshake, message exchange, error handling, and reconnect validation.
Use mock servers (or emulators) if you need to check not only the front end but also scenarios of "incorrect" server behavior.
Include additional checks for timing, order delivery, and resilience to reconnection.
Key features:
Is one successful connection enough to consider the web-socket server working?
No, it is necessary to test not only the connection start but also the ability to handle correct messaging, improper/incomplete message handling, and unexpected disconnections.
Can HTTP testing tools be used to check the WebSocket API?
No, although the handshake partly implements HTTP, the main exchange occurs over a different protocol, and specialized tools will be required for comprehensive testing.
If the test passes on "localhost", does that mean it will work on the CI server as well?
No, asynchronous behaviors, network timings, and artifacts often only manifest in the CI environment. The differences between environments should always be taken into account.
An engineer implemented web-socket auto-tests at a basic level: just checking the handshake and sending 1 message. The test passes, but one day a bug occurs: after a reconnection, the application stops receiving events. The tests did not catch this.
Pros:
Cons:
Tests are structured as scenarios: handshake, exchange of 10 different messages (correct/broken), delay, forced disconnect, auto-reconnect, resuming the session, and handling of concurrency. All steps are logged and validated by message IDs.
Pros:
Cons: