Summary
Dynatrace Synthetic Browser Monitors support JavaScript events for advanced scripting scenarios. This article provides reusable JavaScript snippets to handle dynamic elements, locations, iFrames, mouse interactions, and conditional logic in browser monitor scripts.
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.
Find the Location and Cloud Platform where an execution is running
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.
-
This example gets the location name and cloud platform that the browser monitor is running on, and uses it to set the value of a variable “searchValue” based on the location. The value can then be accessed later in a keystroke event using {searchvalue} as the Text Value.
Note: this will only work when executed when scheduled or on demand, as the location details must be present. You could add default values to allow for local playback.
// 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);
Double-click mouse event
- From time-to-time an application requires a double-click mouse interaction. This can only be accomplished through a custom JavaScript event. (Please replace
unique-idin the example below)
var Clickevent = new MouseEvent('dblclick', {'view': window});
document.getElementById('unique-id').dispatchEvent(Clickevent);
How to create a locator for a resource in a iFrame
- This snippet can be used for a CSS in an iFrame, and, also, be used on the parent page. (Please replace both
id-of-iframe&CSS-selector-for-element)
document.getElementById('id-of-iframe').contentWindow.document.querySelector('CSS-selector-for-element')
How to change the setting for --disable-web-security
- By default as of
1.198,--disable-web-securityis set totrue. It can be set tofalseby adding the following line to the Synthetic Script.
"configuration": {
"device": {
"orientation": "landscape",
"deviceName": "Desktop"
},
"disableWebSecurity": false
},
Handling an randomly appearing question
- Here is a JavaScript synthetic event example for handling a randomly appearing security question. If the security question is asked, the answer is set as the last word in the question. If the security question does not appear, skip the next click that would submit the answer.
// 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;
}
Validate text in a JavaScript event
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");
}
What’s Next
You can find more helpful tips for Synthetic in the Synthetic Troubleshooting Map
Related reading
How to perform a right-click in a Browser Clickpath Synthetic Monitor
How to perform a keypress in a Browser Clickpath Synthetic Monitor
How to perform mouseover event on browser click path synthetic monitor.
How to perform DoubleClick event on browser click path synthetic monitor.