Test WebSocket communication

Relevant for: API testing only

This topic explains how to add WebSocket activities to your API test.

Open a WebSocket connection

To test the communication between WebSockets, you must first open a connection to the WebSocket. This step is mandatory for testing the sending/receiving of messages from a WebSocket connection.

  1. In the Web Sockets section of the Toolbox pane, add a Open socket activity to the canvas.

  2. In the Input/Checkpoints tab in the Properties pane, in the Value cell for the URL property, enter the URL for the WebSocket (ws:\\xxx.xxx) or secure WebSocket (wss:\\xxx.xxx) connection.

    Note: You can enter URLs that contain query strings, for example, ws:\\xxx.xxx?key=value or wss:\\xxx.xxx?key=value.

Back to top

Send a message to another WebSocket

  1. In the Web Sockets section of the Toolbox pane, add a Send message activity to the canvas.

  2. In the Input/Checkpoints tab in the Properties pane, in the Value cell for the Socket ID property, click the Link to a data source button . The Select Link Source dialog box opens.

  3. In the Select Link Source dialog box, select the Available steps option.

  4. In the list of available steps, select the Open socket activity. A list of available properties is displayed in the right pane.

  5. In the right pane, select the General tab .

  6. In the General tab, select the Socket ID property and click OK to link the Send message step to the Open socket step.

  7. In the Properties pane, select the Body tab .

  8. In the Body tab, in the Request Body section, from the drop-down list, select the format for your message body. You can send a message with Text, XML, or JSON.

    You can also send a message that was stored in a file and passed to the test as a parameter, or created as an output of a previous step. In the Request Body drop-down list, select File. Then click the Link body to a data source button and select the relevant data source. For details, see Select Link Source Dialog Box (API Testing)

  9. In the Request Body section, in the text editor area, enter the body of your message to send.

    Note: You can load the XML or JSON for the sent message body from an external file by clicking the Load button in the text editor area.

Back to top

Receive a message from another WebSocket

  1. Prerequisite: Create an Open socket step in your test.

  2. In the Web Sockets section of the Toolbox pane, add a Receive message activity to the canvas.

  3. In the General tab in the Properties pane, in the Value cell for the Socket ID property, click the Link to a data source button . The Select Link Source dialog box opens.

  4. In the Select Link Source dialog box, select the Available steps option.

  5. In the list of available steps, select the Open socket activity. A list of available properties is displayed in the right pane.

  6. In the right pane, select the General tab .

  7. In the General tab, select the Socket ID property and click OK to link the Receive message step to the Open socket step.

  8. In the Properties pane, select the Receive Message tab .

  9. In the Receive Message tab, in the Received Message Body section, from the drop-down list, select the format for your message text. You can receive a message body with Text, XML, or JSON.

  10. In the Received Message Body section, in the Name or Regular Expression area, enter the body of the expected message or a regular expression representing the received message body.

    Note: You can load the XML or JSON for the received message body from an external file by clicking the Load button in the text editor area.

Add a Wait step to wait for the received message to arrive

  1. In the Toolbox pane, expand the Flow Control activities section.

  2. From the Flow Control activities, drag a Wait activity to the canvas. The Input/Checkpoints tab opens in the Properties pane.

  3. In the Input/Checkpoints tab, in the Value cell for the Completion event name property, click the Link to a data source button . The Select Link Source dialog box opens.

  4. In the Select Link Source dialog box, select the Available steps option. A list of available steps is displayed in the Select a step: (left) pane.

  5. In the Select a step: pane, select the Receive message activity. A list of available properties is displayed in the Select a property: (right) pane.

  6. In the Select a property pane, select the General tab .

  7. In the General tab, select the Completion event name property and click OK. UFT One links the Wait step to the Receive message step to the , instructing the test to wait to proceed until the message is received from the WebSocket in the Receive message step.

  8. Back to top

Close the WebSocket connection

Note: This step is optional. You should use this step if you want to send or receive messages from a different WebSocket in later test steps.

  1. Prerequisite: Create an Open socket step in your test.

  2. In the Web Sockets section of the Toolbox pane, add a Close socket activity to the canvas.

  3. In the Input/Checkpoints tab in the Properties pane, in the Value cell for the Socket ID property, click the Link to a data source button . The Select Link Source dialog box opens.

  4. In the Select Link Source dialog box, select the Available steps option.

  5. In the list of available steps, select the Open socket activity. A list of available properties is displayed in the right pane.

  6. In the right pane, select the General tab .

  7. In the General tab, select the Socket ID property and click OK to link the Close socket step to the Open socket step.

  8. (Optional)- In the Checkpoints section of the Input/Checkpoints tab, select the Validate checkbox in the Result row to set a checkpoint to check if the Close operation succeeds.

Back to top