Waiting for a specific network element - IE and Firefox

Sometimes it isn't feasible to use any of the available Wait actions for a step, but you need to be sure the next step doesn't begin until a network element downloads around the end of the current step's page load. Dynatrace Synthetic doesn't have a Wait for Network Element, but you can use custom scripting to improvise a wait.

Use this example code in a Custom action at the beginning of a step, in the control target window. It creates a reply handler that looks for all or part of the URL for a network element and, when that network element is received, triggers a function that writes a <DIV> tag with a value of id="thisDoesNotExist" to the gomez_top[0] window.

You need to have a Wait for Validate for the Click action that starts the network activity (a Wait action following the Click action in a Windows Recorder script, or defined as the Click action's Wait time attribute in a Web Recorder transaction). The Wait for Validate needs a locator of type CSS and a match text value of #thisDoesNotExist.

// For IE and Firefox only
function insertOnTrigger(targetWin) {
alert('entering function');
var loc = new Locator();
var win = loc.findFrame(targetWin);
var div = win.document.createElement('div');
div.setAttribute('id', 'thisDoesNotExist');
alert('div inserted?');
// "iot" is a global variable that the reply handler can use outside this action
iot = insertOnTrigger;
// Reply handler waits for a specific object, then calls "iot"
// This code expects this step to have a Wait on Validate for #thisDoesNotExist
var getUrl = new AddEventHookCommand({
"eventType": "replyReceived",
"name": "waitIsOver",
"handler":"if(response.request.url.indexOf('adsid/google/ui') != -1){alert('adding div');iot('gomez_top[0]');}"

In the attached example script, a Navigate action loads http://www.google.com/ and waits for an element whose URL contains adsid/google/ui to load, which happens near the end of the page load.