Check byte limit in a script via JavaScript - Firefox

This code only works in Firefox. Internet Explorer doesn't support event hooks such as this, and the functions are slightly different for Chrome.

The functions used here for Firefox could possibly be replaced with the Chrome equivalents to work in Chrome browsers.

In some cases, it may be necessary to check the size of the content received during script playback, and cause the script to fail programmatically. Below is a sample event hook that would capture the bytes received for all objects downloaded.

bytesDownloaded = 0;

var send = new AddEventHookCommand({
"eventType": "replyReceived",
"name": "getHeaders",
"handler":"if(response.getRequest().getUrl().indexOf('http') != -1){ header = response.getHeader('Content-Length'); bytesDownloaded = bytesDownloaded + Number(header); }"

Following a Navigation action, a Wait for Page Complete, etc., the following code block can be used to view the total number of bytes received and then make a decision based on the number.

alert ("Total bytes downloaded: " + bytesDownloaded);

if (bytesDownloaded > 7500000) {
	// byte limit exceeded our specified value
	// do nothing here so the validation action at the end of the script will fail the test
} else {
	// byte limit has not exceeded our specified value
	// set the validation not to execute so that the script can pass
	script.steps[0].actions[4].type = 'do not execute';

While the above sample gets the content length for all objects downloaded, a good use case for the code would be to get the content size of objects downloaded from a third-party site, where the expectation is that content should fall within a certain value.

A demo script can be downloaded: FailDueToByteLimit.gsl