Skip to main content

How to use JavaScript in your HP LoadRunner scripts?

  
 
JavaScript is rapidly becoming the driving force behind the Internet. Any website, small or large, has some parts written with JavaScript to enhance the user experience. Browser vendors constantly improve the speed in which JavaScript is parsed and executed in their browsers. This increase in speed is now to the point that it’s become one of the key benchmarks for every browser version. When we performance test a website, a web application, or even a REST service we can be sure that JavaScript plays some role in the business process we are about to test.



Why would we want to use JavaScript in our script?
There are four main reasons:
  • JavaScript often offers a more intuitive experience which is easier to implement than in C.
  • The JavaScript regular expression library simplifies the challenge of working with regular expressions.
  • Numerous JavaScript libraries that assist with any coding task (e.g. string manipulation) are available.
  • Client-side logic is often implemented in JavaScript. Inserting snippets of the original JavaScript code means you don’t have to translate the JavaScript client logic into C code.

This article explores the following scenario: We are testing a web server application which is used for authentication. Our goal is to receive an authentication token from the server and use the token in all future communication with the server. The authentication process is as follows:
  1. Obtain the String url to be encoded from the HTTP GET request.
  2. Perform some calculation on that string and generate a unique hash code using a proprietary SHA encryption algorithm.
  3. Send a GET request  passing the hashcode generated in step 2 as a query parameter.

For this scenario we have to generate a hash code to be appended along the URL using basic SHA Algorithm (test.js).

Our first goal is to obtain the web URL from the server for which the hash code has to be generated. We have the following step into our script to make the required request call:

web_url("demo.do",
        "URL=https://demotest.com/userTokenId={userTokenId}",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=https://demotest.com/demo.do?",
        "Snapshot=t13.inf",
        "Mode=HTML",
        LAST);

Calculating the hashcode                                                                                              

Now that we have the challenge string we can use some JavaScript manipulations to generate the hashcode.

First we need to enable JavaScript for our script. To do this, open the Run-Time Settings (F4) and go to Internet Protocol > Preferences > JavaScript. Check the  Enable running JavaScript code to checked (as in the screenshot below) and then click Save.




Next we need to create a new JavaScript file that contains our code. Right-click the Extra Files node in the Solution Explorer and select Create new file… Name the new file test.js and press OK.

 We add a new JavaScript function that encodes the input string value from the url into hashcode using SHA algorithm(A part of the code will look like the screenshot shown below):

function SHA (msg) {  
  
    var valA=msg;
     msg = generateHashcode(msg);
    
        return msg;
 
 };

The required value is stored in the “msg” variable, and now we have to apply the encryption function.

Now we just add the web_js_run step to run our JavaScript code:

    web_js_run(
        "Code=SHA('String to be encrypted');",
        "Resultparam=hashcode",
        SOURCES,
        "file=test.js", 
         ENDITEM,LAST);            

The result of the function is stored in the parameter defined by the ResultParam argument (“hashcode” in our case). Finally we have the source files that contain our JavaScript code added as “File=…” arguments separated by the ENDITEM constant.      

Now we add the step that makes the final call with the calculated Hashcode appended to the appropriate request:

web_url("demo.do",
        "URL=https://demotest.com/userTokenId={userTokenId}&hashcode={hashcode}",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=https://demotest.com/demo.do?",
        "Snapshot=t13.inf",
        "Mode=HTML",
        LAST);  

Conclusion

Enhancing your HTTP/HTML script with JavaScript code is easy and useful. There are numerous scenarios where this capability can be employed to shorten scripting time and create a much more robust script. The HP LoadRunner User Guide is key to understanding the full potential of this feature as it includes a built in function library that links the JavaScript code to your test script’s code.



Comments

  1. This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation.Your final conclusion was good.
    Loadrunner course in Chennai
    Qtp training in Chennai

    ReplyDelete
  2. Excellent idea!!! I really enjoyed reading your post. Thank you for your efforts. Share more like this.
    Software Testing Courses Online Certification

    ReplyDelete
  3. This post is so helpfull and informative.keep updating more information...
    Software System Testing
    System Tests

    ReplyDelete

Post a Comment

Popular posts from this blog

Loadrunner ~ Performance Testing with Citrix Protocol : Recording Tips n Tricks!

1 . INTRODUCTION 1.1 Citrix Protocol in load runner Recording Tips When recording a script, be sure to follow these guidelines in order to create an effective script. Single vs. Multi-Protocol Scripts When creating a new script, you may create a single protocol or multi-protocol script. If you plan to record a simple Citrix ICA session, use a single protocol script. When recording an NFUSE Web Access session, however, you must create a multi-protocol script for Citrix ICA and Web (HTML/HTTP), to enable the recording of both protocols. Record into Appropriate Sections Record the connection process into the vuser_init section, and the closing process into the vuser_end section. This will prevent you from performing iterations on the connecting and disconnecting. Run a Clean Session When recording a session, make sure to perform the complete business process, starting with the connection and ending with the cleanup. End your session at a point from where you could sta

Peformance Testing with Citrix Protocol : Citrix ICA Vuser Functions (CTRX)

Mouse Functions ctrx_mouse_click Emulates a mouse click on a Citrix client ctrx_mouse_double_click Emulates a double mouse click on a Citrix client ctrx_mouse_down Emulates a mouse down click on a Citrix client ctrx_mouse_move Emulates a mouse move on a Citrix client sent to a Citrix server. ctrx_mouse_up Emulates a mouse up click on a Citrix client ctrx_mouse_click Emulates a mouse click on a Citrix client sent to a Citrix server. int ctrx_mouse_click( long x_pos, long y_pos, long mouse_button, long key_modifier, char *window_name, [CONTINUE_ON_ERROR,] CTRX_LAST ); int ctrx_obj_mouse_click( const char *obj_desc, long x_pos, long y_pos, long mouse_button, long key_modifier, char *window_name, [CONTINUE_ON_ERROR,] CTRX_LAST ); obj_desc A description of the object recorded if the agent is installed on the server. x_pos The horizontal distance of t

ATM API Loadtesting with Sybase ASE using LOADRUNNER 12.60

Introduction            SAP ASE   (Adaptive Server Enterprise), originally known as   Sybase   SQL Server, and also commonly known s   Sybase   DB or   Sybase ASE , is a relational model database server product for businesses developed by   Sybase   Corporation which became part of   SAP  AG. Metrics Considered for load testing of ATM APIs ·          Transaction per Minute (TPM) e.g : 600 TPM ·          Transaction per second (TPS) e.g: 10 TPS Setting up Database connection ODBC Data Source Administrator For 32 bit: C:\Windows\system32\ odbcad32 For 34 bit: C:\Windows\SysWOW64\ odbcad32 Add a new Data Source for Sybase ASE                                  LR Scripting: Web-HTTP/HTML Protocol Action () {          lr_start_transaction ( " P_API_XXXXXX_XXXXXX _ExecuteQuery" );        //Establishing ODBC driver Connection // Connection String for Sybase ASE : //ConnectionString=Dsn={ODBC Reference Name};Uid={use