This article presents an example implementation of CloudTest's Custom Module feature with a full-duplex protocol—in this case the protocol used is the WebSocket protocol—in a test composition. This implementation was done in lieu of full support for WebSocket Recording (which was added in SOASTA 53,). The example provided below is a WebSocket example, however, the Custom Modules feature can be used with other, non-WebSocket, asynchronous protocols, such as UDP.
Additionally, it is also possible that having recorded a WebSocket test clip, a user may also want to use the simulated transaction or some other detail of the Custom Modules feature in tandem with a test clip based on a WebSocket Recording, and so the following content has not been fully deprecated.
This material is no longer the starting point for WebSocket testers. WebSocket testers should apprise themselves of both the WebSocket Recording feature and the WebSocket Support reference article before continuing with the following material.
By deploying Java JAR files whose code calls the Scripting Custom Module Java API into CloudTest, and then calling that module(s) from within a Composition, CloudTest users can accomplish tasks that cannot currently be done in a standard Composition.
- The developer will utilize the Scripting Custom Module Java API (for all tasks that must be accomplished from Java).
- The example in this article uses the open source Java-WebSocket open-source project, which can be downloaded here. The project contains a JAR (
java_websocket.jar) file that is necessary for this example to work. Skip creating the sample test composition described in this article if you already have a JAR file to use. In which case, use the steps below as a basic workflow for creating your own test composition using a full-duplex protocol implementation.
Using the Scripting Custom Module Java API
The Custom Module feature permits users to create and add their own Java custom modules (such as a Jar file to a CloudTest environment).
- Using the WebSocketExample source code here as a model, create your own Java source code.
- Use the execute method to call from the CloudTest Script in the composition; also passes in the SOASTA Custom Java API and the address of the WebSocket server.
- Uss the API to get the current Composition object.
- Create a WebSockets communication handler. In this example, the handler is defined in the WebSocketsExampleCommHanlder.java(shown above).
- Use SimulatedTransactionInfo to setup virtual messages.
- Use the Result object's postMessage method for each step in the communication. Simulated messages can be represented in the Result as if they were standard Messages in the Clip, and as such are included in the composition's statistics as if they were a standard Message. Java operations can be mixed in with regular Messages (or any other standard item) in a Clip.
Checking Composition Status using Flag Variables
The final step prior to uploading custom Java mode code for use with CloudTest should be to ensure that the Custom Module Java API's Composition flag variables—
isEnding—are used everywhere that is necessary.
For custom Java code, the Java programmer is responsible for checking these API flags, and the developer should ensure that their Java code will stop when the composition stops. There is no technique by which CloudTest can stop Java code from the outside.
An example use of the Composition flag variable,
isEnding, is shown below. This example appears in the WebSocketsExample.java file discussed above.
Uploading a Custom Module
Once your code is written, compile it into a JAR file for use in CloudTest. Then, use the Custom Module feature to add it to a CloudTest environment. To do so, open your CloudTest instance and use the followign steps to add it to your repository IIn this example, we are using two JAR files:
java_websocket.jar (from the Java-WebSocket, dist folder) and our own compiled JAR,
- Login to your CloudTest instance and navigate to Central > Custom Modules (under the Library node).
- Click the New icon.
Use the New Custom Module box for the remaining steps.
- In the New Custom Module box, click Choose File to select your Java code file. For example,
- Assign a repository name. This will appear in the Central > Custom Modules list.
- Optionally, select a repository folder.
- Click OK to complete creation of the Custom Module.
- Repeat this step for any additional JAR files necessary to create your own scenario. In this example, we also uploaded
The Custom Modules list will show both of the new custom modules.
Calling a Custom Module from a CloudTest Script
- Next, create a CloudTest script (from Central > Scripts) in your test that will call the new Custom Module.
In this example script, we will need the following lines of code:
- An import statement. In this case, we are using the websocketexample.
- A variable for the ID that we are generating.
- The execute method (mentioned above) to call the WebSocket server from the CloudTest script.
- Save the script before proceeding. Leave the script open in the Script Editor.
Associate the Custom Module with the Script
Next, we will create a binding of this script and the Custom Module we created above.
- With the script open in the Script Editor, click the Custom Modules button in the Script Editor.
When you do so, the Custom Modules panel opens on the right.
- Click Add Custom Modules.
- Use the Choose custom module box to navigate to the custom module created above. For example,
- Click OK and repeat, if necessary. In this example, there is a second custom module to associate with this script.
- Save the CloudTest script and give it a name before proceeding. For example, WebSocketsSample Script.
Use the Script in a Test Clip and Playing the Composition
The remaining steps are basic CloudTest test assembly steps.
- Create a test clip. For example, WebSocketsSample Clip. Refer to Creating a New Clip for steps.
- Create a group in the test clip, Group1.
- In Group1, add a second group, Group2.
- With the Clip Editor, lower panel Scripts tab shown, drag the script created above into Group2.
- Save the test clip and give it a name before proceeding. For example, WebSocketsSample Clip.
- From the Clip Editor toolbar, select Open in Test Composition.
- Click Save on the Composition Editor toolbar and give the new test composition a name. For example, WebSocketsSample Composition.
- Click the Play button on the Composition Editor toolbar. The composition begins to play and the Results tab appears showing the Result Details dashboard.
- Expand the Navigation Tree in the Result Details dashboard.
- Click the Foreground Transaction, and then click the Events List tab. This is a simulated transaction and it was defined in the
WebSocketsExample.jar(shown in bold below):
- Examine the Events List results for each of the 3 messages in the test clip. Note how they relate to the simulated messages that were created in the
- Stop the test composition when ready.