UPDATE: HP has changed the syntax for input and output variables in ST 11.10. Check out UPDATE: Service Test 11.10: How to Create Custom Code using Service Test 11.10 for more info.
HP’s documentation on Service Test custom code functionality is pretty slight. Reading the user guide I was unable to find an exact example of how to pass data to and from my custom code. The key to cracking the custom code feature is to become familiar with the args object, as well as two of its properties: Input and Output.
These two properties are what you need to use in order to pass value to and from ST’s custom code. So — to assign a value to a input variable that you created, you need to use the following statement:
And to assign a value to a variable for later use in your script, you would use:
The easiest way for me to learn something is to do it. The following is a small example I came up with to help me understand how to use Service Test’s custom code Feature:
1. Create a text file on your local drive called myCounter.txt
2. Open the text file and add the value of 100. Save and Close.
3. Open Service Test 11
4. Under ST’s ToolBar, navigate to the File System folder. Drag the ‘Read from File” objects onto your test’s main canvas area.
5. Go to the Custom Code’s property sheet and click on the “test input parameters” button (The Step Icon).
6. Point your File path to the location of the myCounter.txt file. Set “Encoding” to Automatic
8. Go to the Custom Code’s property sheet and click on the “test input parameters” button (The Step Icon).
9. Click on the “Add Property” button (The + icon) and select the “Add Input Property” option from the menu
10. In the Add Input Property, enter the following:
11. Click OK (You should now have a value called inCounter located under the Input\Properties section).
12. For the inCounter value, point the Read From File’s ‘Content’ value (this will contain the value that is read from the myCounter.txt file)
13. Click on the Add Property button (The + icon) and select “Add
Output Property” option from the menu.
14. In the Add Output Property fill out the following:
15. Click OK (your Custom Code’s property sheet should now have the following:
16. In the Custom Code’s property sheet, click on the “Events”
17. Click on “ExecuteEvent” and select Create a
18. A SharedUserCode window should appears in ST. Click on it, then
navigate to the CodeActivity_OnExecuteEvent section and add the following code:
19. Go to the “String Manipulation” folder under ST’s Toolbox section
and drag the ‘Concatenate Strings’ object onto your test’s canvas area
under the Custom Code.
20. Go to the “Concatenate Strings” property sheet and click on the
“test input parameters” button (the Step Icon), and fill out the following info:
21. Run the script.
22. The test should pass with the following Output:
I know this example was simplistic, but by using these same steps, you should be well on your way to creating your own Custom Code script masterpiece.