Response validation with JavaScript code

One of the benefits of the Pro version of WAPT in comparison with the regular one is that you can insert JavaScript code inside profiles to handle various calculations. This can be used to parametrize complex user sessions when session-specific values are not contained in the response page code explicitly. Another example is when you cannot extract values from the response with help of standard functions, like “Search Parameter”, because bounding text may be variable. So, you need to implement more complex search algorithms to find the right value occurrence.

All the above cases are relatively rare and they are specific for each web application. In this topic I will show how to implement with JavaScript another task, which is very general. As you know, WAPT Pro can validate server responses by a keyword. This feature is available on the “Response Processing” tab for any request.

However what if you need to validate with help of several keywords? Unfortunately this is not possible with the standard validation functionality, but you can easily implement this in JavaScript.

To do this choose “Add | JavaScript” on the toolbar.

This will insert a JavaScript operator to the profile next to the currently selected request in the left view. The code will be executed in each user session. It can access the content of the recently received page. That is why it is possible to work with values contained there and perform various validations.

For example, if you want to invalidate page in case it contains any one of the tree keywords (suppose that those keywords appear in case of different errors), you may use the following code.

var pos1 = context.responseBody.search("string 1");

var pos2 = context.responseBody.search("string 2");

var pos2 = context.responseBody.search("string 3");

if ((pos1 >= 0) || (pos2 >= 0) || (pos3 >= 0)) log.error("Validation error");

If you want to invalidate the page in case it does not contain one or more of the three strings of text, you can use the following code.

var pos1 = context.responseBody.search("string 1");

var pos2 = context.responseBody.search("string 2");

var pos2 = context.responseBody.search("string 3");

if ((pos1

You can easily modify those examples to work with more complex validations with different number of keywords. All you need is a quick reference on JavaScript.

Note that errors reported this way will appear in the report in the “Other errors” table. As those are not related to specific pages (in general), error rate is not calculated for them, so you will see the total number of error occurrences.