cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
xu_guo
Dynatrace Organizer
Dynatrace Organizer


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 an execution is running on

  • This script gets the location name and platform of the synthetic test 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.
// 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

  • Very basic double-click mouse event. From time-to-time our customers custom application require a double-click mouse interaction which needs to be accomplished through a custom JavaScript step. (Please replace unique-id in 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

  • Snippet to get CSS in an iFrame. Can 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-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
  },

 

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. Reference: (https://www.dynatrace.com/news/blog/synthetic-clickpaths-level-up/)
// 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");
}
Version history
Last update:
‎12 Nov 2024 10:22 AM
Updated by:
Comments
ChadTurner
DynaMight Legend
DynaMight Legend

Thanks for sharing this @xu_guo 

michael_2_kumme
Organizer

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

HannahM
Dynatrace Leader
Dynatrace Leader

I just tested it with a mobile site and, if the out of the box tap doesn't work, you can indeed just use a click. This was the code I used. 

document.querySelector('yourCSSSelector').click();