<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>article Javascript Snippets for Browser Monitor Scripts in Troubleshooting</title>
    <link>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/ta-p/199070</link>
    <description>&lt;P&gt;&lt;LI-TOC indent="15" liststyle="disc" maxheadinglevel="2"&gt;&lt;/LI-TOC&gt;&lt;/P&gt;
&lt;DIV class="lia-message-template-content-zone"&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Summary&lt;/H2&gt;
&lt;P&gt;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.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For editing a browser monitor in script mode rather than as a clickpath, please see the documentation on correct script formatting:&lt;SPAN&gt; &lt;A href="https://docs.dynatrace.com/docs/observe/digital-experience/synthetic-monitoring/browser-monitors/script-mode-for-browser-monitor-configuration" target="_self"&gt;Script mode for Browser Monitors&lt;/A&gt;. &lt;/SPAN&gt;Note: &lt;STRONG&gt;Ctrl + Spacebar&lt;/STRONG&gt; will provide a list of autocomplete suggestions when working in script mode.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Find the Location and Cloud Platform where an execution is running&lt;/H2&gt;
&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;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.&lt;BR /&gt;&lt;STRONG&gt;&lt;BR /&gt;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.&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;// 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") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("Alibaba") &amp;gt;= 0)) {
        mysearchvalue = 'Mumbai';
    } else if ((loc.indexOf("Sydney") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("AWS") &amp;gt;= 0)) {
        mysearchvalue = 'Sydney';
    } else if ((loc.indexOf("N. California") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("AWS") &amp;gt;= 0)) {
        mysearchvalue = 'N. California';
    } else if ((loc.indexOf("Chennai") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("Azure") &amp;gt;= 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);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Double-click mouse event&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;From time-to-time an application requires a double-click mouse interaction. This can only be accomplished through a custom JavaScript event. &lt;EM style="font-family: inherit;"&gt;(Please replace &lt;CODE&gt;unique-id&lt;/CODE&gt; in the example below)&lt;/EM&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;var Clickevent = new MouseEvent('dblclick', {'view': window});
document.getElementById('unique-id').dispatchEvent(Clickevent);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;How to create a locator for a resource in a iFrame&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;This snippet can be used for a CSS in an iFrame, and, also, be used on the parent page. &lt;/SPAN&gt;&lt;EM&gt;(Please replace both &lt;CODE&gt;id-of-iframe&lt;/CODE&gt; &amp;amp; &lt;CODE&gt;CSS-selector-for-element&lt;/CODE&gt;)&lt;/EM&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;document.getElementById('id-of-iframe').contentWindow.document.querySelector('CSS-selector-for-element')&lt;/LI-CODE&gt;
&lt;H2&gt;&lt;BR /&gt;How to change the setting for --disable-web-security&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;By default as of &lt;/SPAN&gt;&lt;CODE&gt;1.198&lt;/CODE&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;CODE&gt;--disable-web-security&lt;/CODE&gt;&lt;SPAN&gt; is set to &lt;/SPAN&gt;&lt;CODE&gt;true&lt;/CODE&gt;&lt;SPAN&gt;. It can be set to &lt;/SPAN&gt;&lt;CODE&gt;false&lt;/CODE&gt;&lt;SPAN&gt; by adding the following line to the Synthetic Script.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;"configuration": {
      "device": {
          "orientation": "landscape",
          "deviceName": "Desktop"
      },
      "disableWebSecurity": false
  },&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Handling an randomly appearing question&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;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.&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;// 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;
}&lt;/LI-CODE&gt;
&lt;H2&gt;&lt;BR /&gt;Validate text in a JavaScript event&lt;/H2&gt;
&lt;LI-CODE lang="markup"&gt;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");
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;What's Next&lt;/H2&gt;
&lt;P&gt;You can find more helpful tips &lt;SPAN&gt;for Synthetic in the&amp;nbsp;&lt;/SPAN&gt;&lt;A class="" href="https://community.dynatrace.com/t5/Troubleshooting/Synthetic-Troubleshooting-Map/ta-p/250426" target="_blank" rel="noopener"&gt;Synthetic Troubleshooting Map&lt;/A&gt;&lt;/P&gt;
&lt;/DIV&gt;</description>
    <pubDate>Mon, 20 Oct 2025 13:30:28 GMT</pubDate>
    <dc:creator>xu_guo</dc:creator>
    <dc:date>2025-10-20T13:30:28Z</dc:date>
    <item>
      <title>Javascript Snippets for Browser Monitor Scripts</title>
      <link>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/ta-p/199070</link>
      <description>&lt;P&gt;&lt;LI-TOC indent="15" liststyle="disc" maxheadinglevel="2"&gt;&lt;/LI-TOC&gt;&lt;/P&gt;
&lt;DIV class="lia-message-template-content-zone"&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Summary&lt;/H2&gt;
&lt;P&gt;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.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For editing a browser monitor in script mode rather than as a clickpath, please see the documentation on correct script formatting:&lt;SPAN&gt; &lt;A href="https://docs.dynatrace.com/docs/observe/digital-experience/synthetic-monitoring/browser-monitors/script-mode-for-browser-monitor-configuration" target="_self"&gt;Script mode for Browser Monitors&lt;/A&gt;. &lt;/SPAN&gt;Note: &lt;STRONG&gt;Ctrl + Spacebar&lt;/STRONG&gt; will provide a list of autocomplete suggestions when working in script mode.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Find the Location and Cloud Platform where an execution is running&lt;/H2&gt;
&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;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.&lt;BR /&gt;&lt;STRONG&gt;&lt;BR /&gt;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.&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;// 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") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("Alibaba") &amp;gt;= 0)) {
        mysearchvalue = 'Mumbai';
    } else if ((loc.indexOf("Sydney") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("AWS") &amp;gt;= 0)) {
        mysearchvalue = 'Sydney';
    } else if ((loc.indexOf("N. California") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("AWS") &amp;gt;= 0)) {
        mysearchvalue = 'N. California';
    } else if ((loc.indexOf("Chennai") &amp;gt;= 0) &amp;amp;&amp;amp; (platform.indexOf("Azure") &amp;gt;= 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);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Double-click mouse event&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;From time-to-time an application requires a double-click mouse interaction. This can only be accomplished through a custom JavaScript event. &lt;EM style="font-family: inherit;"&gt;(Please replace &lt;CODE&gt;unique-id&lt;/CODE&gt; in the example below)&lt;/EM&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;var Clickevent = new MouseEvent('dblclick', {'view': window});
document.getElementById('unique-id').dispatchEvent(Clickevent);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;How to create a locator for a resource in a iFrame&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;This snippet can be used for a CSS in an iFrame, and, also, be used on the parent page. &lt;/SPAN&gt;&lt;EM&gt;(Please replace both &lt;CODE&gt;id-of-iframe&lt;/CODE&gt; &amp;amp; &lt;CODE&gt;CSS-selector-for-element&lt;/CODE&gt;)&lt;/EM&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;document.getElementById('id-of-iframe').contentWindow.document.querySelector('CSS-selector-for-element')&lt;/LI-CODE&gt;
&lt;H2&gt;&lt;BR /&gt;How to change the setting for --disable-web-security&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;By default as of &lt;/SPAN&gt;&lt;CODE&gt;1.198&lt;/CODE&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;CODE&gt;--disable-web-security&lt;/CODE&gt;&lt;SPAN&gt; is set to &lt;/SPAN&gt;&lt;CODE&gt;true&lt;/CODE&gt;&lt;SPAN&gt;. It can be set to &lt;/SPAN&gt;&lt;CODE&gt;false&lt;/CODE&gt;&lt;SPAN&gt; by adding the following line to the Synthetic Script.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;"configuration": {
      "device": {
          "orientation": "landscape",
          "deviceName": "Desktop"
      },
      "disableWebSecurity": false
  },&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Handling an randomly appearing question&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;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.&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="javascript"&gt;// 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;
}&lt;/LI-CODE&gt;
&lt;H2&gt;&lt;BR /&gt;Validate text in a JavaScript event&lt;/H2&gt;
&lt;LI-CODE lang="markup"&gt;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");
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;What's Next&lt;/H2&gt;
&lt;P&gt;You can find more helpful tips &lt;SPAN&gt;for Synthetic in the&amp;nbsp;&lt;/SPAN&gt;&lt;A class="" href="https://community.dynatrace.com/t5/Troubleshooting/Synthetic-Troubleshooting-Map/ta-p/250426" target="_blank" rel="noopener"&gt;Synthetic Troubleshooting Map&lt;/A&gt;&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 20 Oct 2025 13:30:28 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/ta-p/199070</guid>
      <dc:creator>xu_guo</dc:creator>
      <dc:date>2025-10-20T13:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript Snippets for Browser Monitor Scripts</title>
      <link>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/tac-p/202116#M125</link>
      <description>&lt;P&gt;Thanks for sharing this&amp;nbsp;&lt;a href="https://community.dynatrace.com/t5/user/viewprofilepage/user-id/21845"&gt;@xu_guo&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2023 20:06:48 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/tac-p/202116#M125</guid>
      <dc:creator>ChadTurner</dc:creator>
      <dc:date>2023-01-10T20:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript Snippets for Browser Monitor Scripts</title>
      <link>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/tac-p/205606#M132</link>
      <description>&lt;P&gt;Can the JS also be used to simulate a TAP action for a mobile device script?&amp;nbsp; 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.&amp;nbsp; Would a JS command work for that tap action and/or would it possible to simply set the value with a JS?&amp;nbsp; IF you have an example to share, that would be great.&amp;nbsp; Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 15:29:37 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/tac-p/205606#M132</guid>
      <dc:creator>michael_2_kumme</dc:creator>
      <dc:date>2023-02-24T15:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript Snippets for Browser Monitor Scripts</title>
      <link>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/tac-p/205615#M133</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;document.querySelector('yourCSSSelector').click();&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 16:04:59 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Troubleshooting/Javascript-Snippets-for-Browser-Monitor-Scripts/tac-p/205615#M133</guid>
      <dc:creator>HannahM</dc:creator>
      <dc:date>2023-02-24T16:04:59Z</dc:date>
    </item>
  </channel>
</rss>

