| |
One of the first things I ran into when building my Web Service test cases in SOATest was the need for creating random user IDs. I needed random user IDs because typically a set of test cases required a fresh enrollment of a user. If I used a static user ID, when I re-run the test case it would fail since the user was already enrolled. Ergo, I needed a random, new user every time I ran the test case. I achieved this in SOATest by utilizing Set-up tests, JavaScript, and an XML Data Bank:
First, I needed a way to generate a random value that conformed to the application's specifications for user IDs. I achieved this by writing the following JavaScript and then feeding the string into soapgest.api.:SOAPUtil method getXMLFromString.
|
function rUserID() { strChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ'; minLength= 6; var randomLength = Math.floor(Math.random()*50); if (randomLength <= minLength) {randomLength=minLength} var randomstring = ''; for (var i=0; i<randomLength; i++) { var rnum = Math.floor(Math.random() * &strChars.length); randomstring += strChars.substring(rnum,rnum+1);
} <!--Feed the JavaScript string into the SOAPTest API--> return Packages.soaptest.api.SOAPUtil.getXMLFromString([randomstring]); }
|
I then saved the script off to my project\Scripts directory as UserID.js. (by creating the file externally, I have the power to reuse the script from many locations from within my test). Now that I had something to generate the IDs all I had to do was set the rest up in SOATest. In order for the user ID to be generated every time I executed my Test Suite, I needed to create a Set-up test. This was done by:
- In the left frame, right mouse clicking the associated Test Suite directory
- Selecting Add Test > Setup Test > Test Method
- Selecting the new created Setup Method; the Tool page will appear in the right frame:

- In the Tool page, choose the Language parameter as "JavaScript"
- Browse to the previously saved UserID.js file
- Select the one available JavaScript function in the Method textbox (rUserID in my case)
- In the left Tests frame, right mouse click the Setup Method
- Select Add Return Value Output > New Output > XML Databank

With the above mentioned, executing the Set-up Test will now call the function from the .js file, return it to the SOAPUtil, and then will populate the XML Databank.
Now, from the test case in which I intend to use my random user ID I can select that Username attribute/node (in the right frame), select the value to be "Parameterized" from the dropdown, and then select the name of the Set-up Test I previously created (Set-up 1: z0 in my case).

Thus, a SOATest test case using a random user ID! |
|