Running JavaScript in the loadrunner12_ script

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"System and Software Configuration"

LR Version: 12.53

JDK Version: 1.8

Function:web_js_run, which provides support only in the LR12 version, LR11 does not support JavaScript operation.

LR configuration: Enable JavaScript to run: replay→runtime settings→preferences→javascript→ Tick "Enabe running JavaScript code"

The "Test 1"-----Executes the JavaScript self-brought object.

Web_js_run (
"Code=alert (' Hello World ');",
last);

Note: The function "code=" can not have spaces before and after the equal sign, otherwise the function will error: argument (number 1) is unrecognized or misplaced [msgid:merr-26396]

Operation Result:

Test 2-----Execute a method of JavaScript's own object and get the return value.

Web_js_run (
"Code=new Date (). GetTime ();",
"Resultparam=timestamp",
last);
Lr_output_message (lr_eval_string ("{timestamp}"));

Operation Result:

The "Test 3"-----executes the parameterless function in the external JS file and gets the return value.

Web_js_run (
"Code=getstring ()",
"Resultparam=string1",
SOURCES,
"File=000.js",
Enditem,
last);
Lr_output_message (lr_eval_string ("{string1}"));
The 000.js content is as follows:

function getString () {
var date = "This is LoadRunner12";
return date;
}

Note:The JS file in the ①file parameter can be used relative path in the working directory , ② computer-Organization-folder and search options- suppress "hide extensions of known files", or you may see a file not found error, The file attributes used in the test are as follows;

Operation Result:

"Test 4" executes an argument function in an external JS file and the parameters are fixed, and the return value is obtained.

Web_js_run (
"Code=getsum",
"Resultparam=sum",
SOURCES,
"File=000.js",
Enditem,
last);
Lr_output_message (lr_eval_string ("{sum}"));
The contents of the 000.js file are as follows:

function Getsum (ARG1,ARG2) {
var date = arg1*1 + arg2*1;
return date;
}
If the arg1+arg2 is used directly, the javscript performs the addition of the strings;

Operation Result:

Test 5 executes an argument function in an external JS file and the parameter needs to be dynamically correlated and gets the return value.

Web_js_run (
"Code=getsum (Lr.getparam (' Param '), Lr.getparam (' Param '))",
"Resultparam=sum2",
SOURCES,
"File=000.js",
Enditem,
last);
Lr_output_message (Lr_eval_string ("{sum2}"));

Note: ① uses the Lr.getparam () function to extract the value of the LoadRunner parameter , which is passed to the local JS file as the function parameter of the Web_js_run function runtime code. Similar to LoadRunner's lr_eval_string ();

Operation Result:

Parameter format attempts and results:

"Code=getsum ({Param},{param})", Error from JS Engine: ": 0:syntaxerror:invalid object initializer
"[msgid:merr-26000]
"Code=getsum (\" {param}\ ", \" {param}\ ")", ACTION.C (+): Web_js_run started [msgid:mmsg-26355]
ACTION.C (+): Web_js_run was successful [msgid:mmsg-26392]
ACTION.C (+): NaN

"Test 6" executes multiple JS functions, and the parameters of the function need to be dynamically correlated, and the return value needs to be obtained.

Web_js_run (
"Code="
"Let Rsakey = new Rsakey ();"
"Rsakey.setpublic (B64tohex (Lr.getparam (' exponent ')"), B64tohex (Lr.getparam (' modulus '))); "
"Hex2b64 (Rsakey.encrypt (Lr.getparam (' password '));",//
"Resultparam=newpassword",
SOURCES,
"File=sec.min.js",
Enditem,
last);
Lr_output_message (Lr_eval_string ("{newpassword}"));

Note: 3 JavaScript statements are executed consecutively in the code parameter;

At present the above statement still left a problem, although the script did not error, but the NewPassword value is empty;

The "Test 7" function defines multiple uses at a time

Web_js_run (
"Code=myfunc (1, 2);",
"Resultparam=sum4",
SOURCES,
"Code=var myfunc=function (x, y) {return x*1+y*1;}",
Enditem,
last);
Lr_output_message (Lr_eval_string ("{sum4}"));

Web_js_run ("Code=myfunc (3, 4);",
"RESULTPARAM=SUM5",
last);
Lr_output_message (Lr_eval_string ("{SUM5}"));

Operation Result:

In this example, MyFunc () is defined and used multiple times, so we ask what is the life cycle of the function? will multiple iterations generate more of this function? If so, what about the waste of resources that duplicate definitions cause? In fact: LoadRunner in the design of these have been considered, for this there is a function web_js_reset, as follows.

"Extension: Web_js_reset"

Role: ① cleanup web_js_run After the execution of the generated "variables, functions," and other content, releasing its memory, otherwise the previously defined functions and saved variables will persist, will consume the memory of local resources;

Note: If "simulate a new user on each iteration" is checked in "Replay→runtime settings→browser→browser Emulation" , at the beginning of the new iteration, LoadRunner will automatically execute web_js_reset.

Running JavaScript in the loadrunner12_ script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.