BrowserExecuteJavaScript Function

Action

Executes JavaScript code based on the currently loaded HTML content.

Include file

BrowserAPI.bdh

Syntax

BrowserExecuteJavaScript( sScript     : in string
                          sResult     : out string optional 
                          uTestObject : in union optional ): boolean;
Parameter Description
sScript The JavaScript code.
sResult Optional: Variable receiving the result of the JavaScript execution, converted to a string.
uTestObject The XPath locator or the handle to the DOM element. This element can be accessed as currentElement from within the script.
Note: Return values are not supported when uTestObject is specified. In this case, NULL must be supplied as value for sResult.

Return value

  • true if successful

  • false otherwise

Example

benchmark SilkPerformerRecorder

use "Kernel.bdh"
use "BrowserAPI.bdh"

dcluser
  user
    VUser
  transactions
    TJavascript  : 1;

dcltrans
  transaction TJavascript
  var
    sScript  : string;
    sVersion : string;
    sObjectAsJSON : string;
    sResult : string;
  begin
    BrowserStart(BROWSER_MODE_DEFAULT, 800, 600);
    // define JavaScript to execute
    sScript := "window.open('http://demo.borland.com/gmoajax')";
    // execute the script (which opens a new window and navigates to demo.borland.com)
    BrowserExecuteJavaScript(sScript);
    
    // Check, if page is using "Modernizr"    
    BrowserExecuteJavaScript("if (window.Modernizr) { window.Modernizr._version; }", sVersion);
    if (Strlen(sVersion) > 0) then
      Print("Page is using Modernizr, version " + sVersion);
    else
      Print("Page is not using Modernizr");
    end;

    // Acces object, stringifying it first
    BrowserExecuteJavaScript("JSON.stringify(window.Modernizr)", sObjectAsJSON);
    Print(sObjectAsJSON); // yields, e.g.: {"touch":false,"cssanimations":true,"csstransitions":true,"_version" ... }

    // run javascript for specific locator
    // return value can be retrieved via helper variable
    BrowserNavigate("http://demo.borland.com/gmoajax/");
    BrowserExecuteJavascript("document.body.sResult=currentElement.currentStyle.borderBottomColor", NULL, "//img[@id='logoIMG']");
    BrowserGetProperty("//body", "sResult", sResult);    
    Print(sResult);      // yields, e.g.: hsl(120, 33%, 43%)
  end TJavascript;