Scripting for dynamic data in a web services test

This procedure shows how to create a script to test dynamic web services.

The replyHandler available on the Firefox agent enables users to script dynamic web services tests in which values generated by one request can be captured, stored, and then later used in a future request.

The most common use case is when a web service requires authentication. The first request authenticates and returns a session ID, which then needs to be passed in as a parameter in the next request.

Create a basic web services script.

For more information, see Creating a Basic Web Services Test.

Add a step boundary after the last action to create a new step.
Hold the mouse pointer over the last action separator timeline action separator in the timeline, click the add icon Add icon, and select Step from the list.
Add a step
The step is added to the timeline.
New step

In the new step, add a new HTTP action HTTP action for the second web service call.
Specify the URL and method on the HTTP Requests tab, but do not click More; that is, do not specify the Body content for a POST method.
HTTP action

For more information, see HTTP Action.

After the new HTTP action, add a Wait Wait action action and configure it as Wait for Network.

For more information, see Wait for Network.

Within the first HTTP action, click More for the object request, then select the Reply Handler tab.

Reply handler tab

In the JavaScript field, enter the JavaScript necessary to grab the content to capture and store it in a global variable.

For example, the code below intercepts the reply, verifies that it is successful (checks for a 200 status), and grabs an orderId value from the reply body through a regular expression match.

 if (response.status == '200')
{
     body = response.content;
     sessionId = body.match(/session\=([0-9]+)/)[1];
}

Add a Custom action Custom action before the step boundary to the right of the step containing the first HTTP action.

Set the targetWindow to control.

In the Custom action's Code field, enter the JavaScript that will build the POST string required for the second HTTP action, inserting the value captured and stored earlier where needed. An example is shown in the following figure.
Code for Custom action

To properly insert the POST string within the second HTTP action, the string needs to be base64 encoded before it can be placed within action's body property. This can be done by adding the following code to the Custom action.

 postStringB64 = window.btoa(postString);

Insert the newly created POST string into the body of the first object request within the second HTTP action.

This can be done by referencing the action property through the JSON model.

 script.steps[1].actions[0].requestList[0].body = postStringB64;

Add a Validate action Validate action at the end of the script to verify that the second request was successful.
Validate action

For more information, see Validate Actions.