Share this page

# web_js_run

Runs the specified JavaScript.

## C Language

    int web_js_run("Code=<JS code>" | "File=<file_name>",["Domain=domain",] ["ResultParam=parameter name",] ["ActionStep=No|Yes",] [SOURCES,["File=file_name" | "Code=code", ENDITEM,]+ ] LAST)

Miscellaneous Functions

Code | The main code argument contains literal JavaScript code.
File | The main code File argument contains a JavaScript file to be loaded.
Domain | Where the JavaScript code contains a relative URL, the domain is the base URL.
ResultParam | Name of the parameter in which to store the result of the JavaScript call.
ActionStep | Whether or not the web_js_run step is an Action Functions step. Possible values are Yes (default) or No.
SOURCES | A delimiter that starts this section containing arguments containing source code and the location of JavaScript files.
File | A JavaScript file to be loaded either because it contains a function referenced by a Code argument or it provides required objects or data.
Code | Literal JavaScript code.
ENDITEM | A delimiter that separates one set of values from the next, where a set of values defines one compound argument. Sometimes a set contains a single value. Do not pass ENDITEM except to separate definitions.
LAST | Required marker for the end of the argument list.

web_js_run runs the specified JavaScript.

One of either the main Code argument or the main File argument is required. Do not pass both.

Files referenced with File arguments must be present in the VUser directory. If the files are in a subdirectory, pass the relative path. For example, "`js_scripts/code.js`", where js_scripts is a subdirectory of the VUser directory.

In the SOURCES section, place any items that have to be evaluated before the main Code or File is evaluated. Arguments in the SOURCES section are evaluated in the order in which they appear. The entire SOURCES section is evaluated before the main File or Code argument.

If any JavaScript code contains a relative URL, use the Domain argument to specify the base. A relative URL is any URL that does not contain "`://`". The base URL in the Domain argument must include the protocol, for example, `http://` or`ftp://`.

Example of use of the Domain argument:

    web_js_run(“Code=”
        “var request = new XMLHTTPRequest();”
        “request.open(‘GET’, ‘page.html’, false);” // Relative URL
        // more code…
        ,
        “Domain=http://www.mysite.com/abc”, // Base URL
        LAST);

This request resolves to `http://www.mysite.com/abc/page.html`.

web_js_run cannot run script directly from a URL, for example: `"URL=http://www.domain.com/file.js"`. If you need to run code from a web server, download the page with web_url, save the response to a parameter, and use the parameter in the web_js_run call.

To run web_js_run and web_js_reset, Javascript must be enabled in the run time settings under`Internet Protocol > Preferences > Options… > Web Javascript > Enable running Javascript code`.

When the code called in a web_js_run step returns, the context is not discarded. Therefore, functions, objects, and variables remain in context and can be used in subsequent steps of web_js_run.

For example, if a function is defined in one web_js_run step, it can be used in a subsequent step without redefining it. In addition, if the function in a web_js_run step defines, calculates, or fetches some data, that data can be used again in a subsequent web_js_run step.

The convenience of preservation of context is offset by an increase in memory consumption. Since the JavaScript context is saved per Vuser, the memory consumption is not trivial when there are many web_js_run calls. You can call web_js_reset to clear this context for the Vuser, thereby recovering the memory. You can also call web_js_reset if you need to clear the values for logical reasons.

If the runtime setting `Simulate a new user on each iteration` is set, web_js_reset is called automatically at the start of each iteration. If `Simulate a new user on each iteration` is not set, avoid excessive memory consumption by inserting web_js_reset calls in your script at points where you no longer need the saved context.

Several TruClient functions can be used in the code of a web_js_run call. These functions have equivalent utility functions, as shown in the following table:

TruClient Function | Equivalent VuGen Utility Function
--- | ---
TC.advanceParam | lr_advance_param
TC.setParam | lr_save_string
TC.getParam | lr_eval_string
TC.log | lr_debug_message
TC.userDataPoint | lr_user_data_point

## Return Values

This function returns LR_PASS (0) on success, and LR_FAIL (1) on failure.

## Parameterization

You cannot use standard parameterization for any arguments in this function. You can refer to script parameters by using “LR.getParam()” and “LR.setParam()” from within your JavaScript code.

## Example

Hello world

Copy code

    web_js_run("Code= alert('Hello world');", LAST);

Copy code

    // Save the value 3 to parameter "Sum"
    web_js_run("Code= myFunc(1, 2);",
        "ResultParam=Sum",
        "ActionStep=NO",
        SOURCES,
        "Code= var myFunc = function(x,y) { return x+y; }", ENDITEM,
        LAST);

    // Simple string manipulation
    web_js_run("Code= myFunc('abc', 'def');",
        // myFunc was defined in the previous step.
        // No need to redefine, unless web_js_reset() was called.
        "ResultParam=ConcatStr",
        LAST);

Copy code

    web_set_max_html_param_len("4000");

    web_reg_save_param("js_url_body",
        "LB=",
        "RB=",
        "Search=body",
        "RelFrameId=1",
        LAST);

    web_url("js_url",
        "URL=http://127.0.0.1:1080/WebTours/myCode.js",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t1.inf",
        "Mode=HTML",
        LAST);

    web_js_run("Code=eval(LR.getParam('js_url_body'));",
               LAST);

Copy code

    // Running autorun.js
    web_js_run(
        "File=autorun.js",
        LAST);

    // 1) Loading code.js
    // 2) Calling function F(a,b), which is defined in code.js
    // 3) Writing the return value of the call to F(a,b) to param1
    web_js_run(
        "Code=F(a , b)",
        "ResultParam=param1",
        SOURCES,
        "File=code.js", ENDITEM,
        LAST);

This code is in the “send_request.js” file in the script folder:

Copy code

        var request = new XMLHttpRequest();
        request .open('POST','validate_request.asp',true);
        var session = LR.getParam('sessionParam');
        var custom = LR.getParam('customParam');
        request.send('my_session=' + session + '&my_custom=' + custom);
        request.responseText;

Copy code

    web_reg_save_param_ex(
        "ParamName=customParam",
        "LB=:  \"",
        "RB=\",",
        SEARCH_FILTERS,
        "Scope=Body",
        "RequestUrl=*/generate_json_2_parameters.asp*",
        LAST);

    web_reg_save_param_ex(
        "ParamName=sessionParam",
        "LB=:  \"",
        "RB=\",",
        "Ordinal=2",
        SEARCH_FILTERS,
        "Scope=Body",
        "RequestUrl=*/generate_json_2_parameters.asp*",
        LAST);

    web_url("generate_json_2_parameters.asp",
        "URL=http://myServer/cgi-bin/json/generate_json_2_parameters.asp",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://myServer/cgi-bin/json/json_correlation_2parameters.asp",
        "Snapshot=t2.inf",
        "Mode=HTML",
        LAST);

Copy code

    web_js_run( "File=send_request.js",
        "Domain=http://www.mydomain.com/cgi-bin/json",
        "ResultParam=response",
        LAST );

Copy code

    web_js_reset();

Tips to improve your search results... VuGen Function Reference Version 26.1-26.3 Function Reference 26.1-26.3 All View Local Help Center View Online Help Center Version: