web_js_reset

Resets the JavaScript engine context for the Vuser.

C Language

int web_js_reset();
Miscellaneous Functions

web_js_reset discards saved variables and objects and recovers the memory.

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.

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.

Example

Hello world

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

Defining and invoking a function

// 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);

Running JavaScript from a URL by downloading it and saving it to a parameter

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);

Running code from files in the script folder

// 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);

Sending XMLHttpRequest request using script parameters

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

	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;

Values are saved to parameters 'sessionParam' and 'customParam'.

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);

The request is sent to 'http://www.mydomain.com/cgi-bin/json/validate_request.asp'. The response is saved to parameter "response".


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

Resetting the JavaScript engine context

web_js_reset();