Testing the response codes of all objects downloaded

When playing back a script, our agents don't consider HTTP 4xx / 5xx errors as fatal, as long as other objects being downloaded returned HTTP 200 / Success response codes. This is a "per step" requirement, meaning that every step needs to receive at least one 200 response to be successful.

If you want a test to fail when object errors are seen, there isn't an out-of-the-box way to handle this. The Dynatrace Portal will send alerts for object failures in Backbone tests, but will not mark a test as failed.

You can solve this problem in a script by using custom code to check the response codes for each object.

The first block of code goes in a Custom action at the very beginning of the script, using control as the target window. The code creates a global variable called foundBadObject, and sets the default to false (there are no bad objects). While the script runs, every response code is checked, and as in the example above, if a 399 or higher is received, then foundBadObject is set to true.

foundBadObject = false; 

//Listen for all objects being requested, capture all http responses
//If response code is greater than or equal to 399 (same as 401), fail the test and set foundBadObject to true
var getResponse = new AddEventHookCommand({
"eventType": "replyReceived",
"name": "directNavigate",
"handler":"if(response.getRequest().getUrl().indexOf('http') != -1){ if(response.status >= 399) {foundBadObject = true; alert(response.getRequest().getUrl());}}"
});
getResponse.execute();

At the very end of the script, another Custom action can be used to check the value of foundBadObject.

In this second action, we are checking to see if foundBadObject is set to true. To make this work, the final step of the script is a Validate action that would always fail, using a match value of something like Invalid Response Received. The text doesn't matter here. The idea is that if we do not find any bad objects, we will set this Validate action to do not execute so that the script can pass. If we do find bad objects, we leave the validation alone, and the script will fail.

alert("foundBadObject = " + foundBadObject); 

//foundBadObject is true, run bad validation (will cause script to fail with content match error)
//else, do nothing
if(!foundBadObject)
{
		script.steps[3].actions[6].type = 'do not execute';
}