How to end a test on a non-timing-related event

Prev Next

Overview

Currently, there is no command for ending a test once certain data has been rendered on a page. Tests only end after a period of 2 seconds after Document Complete (in case of a web test) or 0.5 seconds (in case of a transaction test). To work around this, you can create a JavaScript Interval Function which listens for the creation of a given element and ends the test.

Example

Let's say we have a page that calls an API to load content and we want to measure the amount of time it takes for that content to appear, but nothing else after that. We can use the RunScript command to both listen for the delivery of the content and end the test.

\\Step 1 - Open the page
open(http://example.com) 

\\Step 2 -  Write the JavaScript to listen for the delivery of the content. 
($('SELECTOR').length) 

SELECTOR should be the id of the tag containing the content, however, if it is the only element in its class, the class selector will suffice.

The above code only evaluates to true when the object exists, as all non-existent objects have a length of 0.

**NOTE: This example assumes the page is already loading the jQuery Library. However, this code can be used without jQuery by using document.getElementById or document.getElementByClass.

To complete the JavaScript we are going to add a third step with the Interval and Test Termination Code.

\\Step 3
var sensor = setInterval(function(){if($('SELECTOR').length){window.location = 'SMALLIMAGEURL'; clearInterval(sensor);}},25);

This code creates a function, sensor, that gets called every 25 milliseconds. When sensor is called, and the listening code we wrote previously evaluates to true, the browser gets redirected to SMALLIMAGEURL. This should be the URL of an image (smaller than 50x50 pixels), pulled from earlier in the waterfall. If no such image exists, you may use this URL instead:

http://64.79.149.76/wp-content/uploads/2014/02/global-node.png

**Using the Catchpoint image may add up to 100ms, so please use as a last resort.

Every request on the wire at the time this redirect is called is canceled, as are all future requests for the page. Once the image is rendered the test will end. To complete the script, add the following two steps:

\\Step 4 - Give the page time to load the asynchronous content
WaitForNoRequest(10)

\\Step 5 - Check for the presence of the image
WaitForElementPresent(/html/body/img)

The final script should look like this:

open(http://exampleurl.com)
RunScript(var sensor = setInterval(function(){if(\$('SELECTOR').length){window.location = 'SMALLIMAGEURL';clearInterval(sensor);}},25);)
WaitForNoRequest(10)
WaitForElementPresent(/html/body/img)