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.
-
In the Web Sockets section of the Toolbox pane, add a Open socket activity to the canvas.
-
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
orwss:\\xxx.xxx?key=value
.
Send a message to another WebSocket
-
In the Web Sockets section of the Toolbox pane, add a Send message activity to the canvas.
-
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.
-
In the Select Link Source dialog box, select the Available steps option.
-
In the list of available steps, select the Open socket activity. A list of available properties is displayed in the right pane.
-
In the right pane, select the General tab .
-
In the General tab, select the Socket ID property and click OK to link the Send message step to the Open socket step.
-
In the Properties pane, select the Body tab .
-
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)
-
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.
Receive a message from another WebSocket
-
Prerequisite: Create an Open socket step in your test.
-
In the Web Sockets section of the Toolbox pane, add a Receive message activity to the canvas.
-
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.
-
In the Select Link Source dialog box, select the Available steps option.
-
In the list of available steps, select the Open socket activity. A list of available properties is displayed in the right pane.
-
In the right pane, select the General tab .
-
In the General tab, select the Socket ID property and click OK to link the Receive message step to the Open socket step.
-
In the Properties pane, select the Receive Message tab .
-
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.
-
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
-
In the Toolbox pane, expand the Flow Control activities section.
-
From the Flow Control activities, drag a Wait activity to the canvas. The Input/Checkpoints tab opens in the Properties pane.
-
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.
-
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.
-
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.
-
In the Select a property pane, select the General tab .
-
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.
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.
-
Prerequisite: Create an Open socket step in your test.
-
In the Web Sockets section of the Toolbox pane, add a Close socket activity to the canvas.
-
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.
-
In the Select Link Source dialog box, select the Available steps option.
-
In the list of available steps, select the Open socket activity. A list of available properties is displayed in the right pane.
-
In the right pane, select the General tab .
-
In the General tab, select the Socket ID property and click OK to link the Close socket step to the Open socket step.
-
(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.