on
24 Nov 2022
11:06 PM
- edited on
20 Oct 2025
02:30 PM
by
HannahM
Most scenarios can be recorded automatically using the Dynatrace Synthetic Recorder. There are some cases where a little scripting can make a script dynamic or do things not available out of the box. This article provides several helpful examples.
For editing a browser monitor in script mode rather than as a clickpath, please see the documentation on correct script formatting: Script mode for Browser Monitors. Note: Ctrl + Spacebar will provide a list of autocomplete suggestions when working in script mode.
Rather than having multiple browser monitors running in different locations, so they can each use different values for certain fields, this script allows the values to be changed dynamically.
// assign default value. validate on this value after page load (fail if found)
var mysearchvalue = "ValueDidNotGetAssigned";
try {
var loc = api.getContext().location.name;
api.info("Location Name is: " + loc);
var platform = api.getContext().location.cloudPlatform;
api.info("Cloud Platform is: " + platform);
// set parameters
if ((loc.indexOf("Mumbai") >= 0) && (platform.indexOf("Alibaba") >= 0)) {
mysearchvalue = 'Mumbai';
} else if ((loc.indexOf("Sydney") >= 0) && (platform.indexOf("AWS") >= 0)) {
mysearchvalue = 'Sydney';
} else if ((loc.indexOf("N. California") >= 0) && (platform.indexOf("AWS") >= 0)) {
mysearchvalue = 'N. California';
} else if ((loc.indexOf("Chennai") >= 0) && (platform.indexOf("Azure") >= 0)) {
mysearchvalue = 'Chennai';
}
// display location, platform and parameter values on error screenshot
document.body.innerText = "Location Name is: " + loc + ' and Cloud Platform is:' + platform + ' and Search Value is: ' + mysearchvalue;
} catch (err) {
api.info("Error message: " + err.description);
}
api.setValue("searchvalue", mysearchvalue);
unique-id in the example below)var Clickevent = new MouseEvent('dblclick', {'view': window});
document.getElementById('unique-id').dispatchEvent(Clickevent);
id-of-iframe & CSS-selector-for-element)document.getElementById('id-of-iframe').contentWindow.document.querySelector('CSS-selector-for-element')
1.198, --disable-web-security is set to true. It can be set to false by adding the following line to the Synthetic Script."configuration": {
"device": {
"orientation": "landscape",
"deviceName": "Desktop"
},
"disableWebSecurity": false
},
// Capture the security question element
var question = document.getElementById("CSS ID of the Security Question Text");
// Check to see if the security question element exists
if (question === null) {
// No security question was asked so skip the next click
api.skipNextSyntheticEvent();
} else {
//Security question was asked so get the words of the question
var words = question.textContent.split(" ");
//Select the last word of the question and remove the question mark at the end
var answer = words[words.length - 1].slice(0, -1);
// set the answer to the security question
document.getElementById("CSS ID for the Answer Box").value = answer;
}
text = 'Text to validate';
text1 = document.documentElement.innerText;
api.info(text1);
api.info(text1.indexOf(text));
if (text1.indexOf(text) == -1) {
api.fail("execution failed");
}
You can find more helpful tips for Synthetic in the Synthetic Troubleshooting Map
Can the JS also be used to simulate a TAP action for a mobile device script? We have attempted to record a new mobile device monitor that must select an item from a drop down, but the playback will not execute the selection. Would a JS command work for that tap action and/or would it possible to simply set the value with a JS? IF you have an example to share, that would be great. Thanks