WebSocket testing

A WebSocket is a standard protocol for two-way data transfer between a client and server. It is supported by all modern browsers.


Why this post?

My know-how of WebSocket connections was very limited. When I had to test an application which involved a WebSocket connection, I couldn’t find much information on how to go about testing it. It took me a bit of time to research on it and know more about it. Here is an attempt from a tester to make life simple for all my fellow testers who face a similar scenario.


What kind of applications use WebSockets

WebSockets offers connections that are both persistent and bi-directional. This means that the server can actively push information to the client and the client can push information to the server any time. So in case your application needs to get server side data that’s constantly changing WebSockets may be ideal. Applications like social networking sites, chat, gaming, stock market data sites, etc. use WebSockets


How do you begin testing Websockets?

Below are some of the important things you need to know to test WebSocket. We will take https://www.websocket.org/echo.html as an example. In this application, we can open a WebSocket connection by clicking on connect button and also send and receive frame data.

1. How do you track WebSocket using your browser
2. WebSocket Protocol
3. Testing Websocket API


Step1. How do you track WebSocket using your browser

Browsers like Chrome and Firefox helps you track WebSocket traffic.

a. Using Chrome Dev Tools:

Open the developer tools after connecting to https://www.websocket.org/echo.html and open the network tab. Now click on connect. You should be able to see a WS tab which will have the Get request with Status Code: 101 Web Socket Protocol Handshake.

WebSocket Dev Tools

b. Using Websocket Monitor Plugin for Firefox:

Websocket Monitor is a plugin you can add to your Firefox browser. Once you have added the plugin you can a see a tab called Web Sockets when you go to Developer options. This helps you track Web Sockets similarly as you have for chrome browser

WebSocket Monitor Plugin


Step2. The WebSocket Protocol

The WebSocket Protocol has two parts:
a. Handshake
b. Data transfer.

The protocol switch from HTTP to WebSocket is referred to as a WebSocket handshake. The browser sends a request to the server, indicating that it wants to switch protocols from HTTP to WebSocket. The client expresses its desire through the Upgrade header

For the chat application in websocket.org the handshake request looks like this.

        Request URL:wss://echo.websocket.org/?encoding=text
        Request Method:GET

Request Header from client to server looks like this:

        Host: echo.websocket.org
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Key: V/HjGWrwuzgpv+yoWuIhng==
        Origin: https://www.websocket.org
        Sec-WebSocket-Version: 13

Response

        Status Code:101 Web Socket Protocol Handshake

The Response Header from server looks as follows:

        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Accept: qHHKA5IeLEhMfkYgo1ItVR9Zd70=

Once the client and server have both sent their handshakes, and if the handshake was successful, then the data transfer part starts. This is a two-way communication channel where each side can independently send their data. The data message is composed of one or more frames. For our application, the request and response frames look like below
Web Socket Monitor Frame

The WebSocket handshake can also be closed. The close frame may contain a body that indicates a reason for closing, such as endpoint shutting down, an endpoint having received a frame too large, or an endpoint having received a frame that does not conform to the format expected by the endpoint.


Step3. Testing WebSocket API

Now once you know about WebSocket Protocol and how to track the WebSockets we will see how can we test these API’s. At Qxf2 Services we love Python. So naturally this tutorial uses Python.

a. WebSocket client for python:

I will use websocket-client as it is very easy to use.
If you have pip just run the below command to install it.

sudo pip install websocket-client

b. Python code to test WebSocket

This is a simple example using Python to connect to websocket.org. Once a WebSocket connection is established we will send data to server and print the response we receive back from server

import websocket 
 
ws = websocket.WebSocket()
# Connect to host url
ws.connect("wss://echo.websocket.org/")
send_string = "Testing WebSocket"
print "Sending: ",send_string
# Use ws.send() to send data to server
ws.send(send_string)
# Use ws.recv() to get the data sent from server
result = ws.recv()
print "Received: ",result
# Use ws.close() to close the WebSocket handshake
ws.close()

Well, that seems easy now. We would love to hear your feedback and questions in the comments

If you liked this article, learn more about Qxf2’s testing services for startups.


Avinash Shetty

I am a software tester with over 10 years of experience in software testing. Currently I am working at Qxf2 Services Bangalore. As a student of the context-driven approach to software testing I feel there is a lot to learn out there which keeps me very excited. My work has helped me gain good experience in different areas of testing like CRM, Web, Mobile and Database testing. I have good knowledge of building test scripts using Automation tools like Selenium and Appium using Java and Python. Beside testing I am a “Sports Fanatic” and love watching and playing sports.

9 Comments

  1. Hardik said:

    Do we have webSocket-client for java also?

    April 24, 2017
    Reply
  2. Anonymous said:

    I need to create Performance testing scenarios and put assertions. But I am unable to understand the request and response , hence unable to put assertions on.

    August 7, 2018
    Reply
    • Avinash Shetty Avinash Shetty said:

      Well, you can try WebSocket Monitor plugin which would help you to understand the request and response better. Then you can try and put some assertions around it.

      August 8, 2018
      Reply
  3. Paras said:

    I need to connect to the websocket server in secure mode, hence I need to send authentication details. Could you please suggest on how to achieve that?

    August 9, 2018
    Reply
  4. Anonymous said:

    excellent: well done

    January 28, 2019
    Reply
  5. Stephen Bates said:

    Hello,
    If I created a websocket server application how would I go about getting it on to the web so that I could test it? I apologies for the basic nature of this question but I new to the web development scene.
    Thanks
    Stephen

    February 20, 2019
    Reply

Leave a Reply

Your email address will not be published.