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:
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:
- Obtain the String url to be encoded from the HTTP GET request.
- Perform some calculation on that string and generate a unique hash code using a proprietary SHA encryption algorithm.
- 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:
"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.
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.
ReplyDeleteLoadrunner course in Chennai
Qtp training in Chennai
Excellent idea!!! I really enjoyed reading your post. Thank you for your efforts. Share more like this.
ReplyDeleteSoftware Testing Courses Online Certification
This post is so helpfull and informative.keep updating more information...
ReplyDeleteSoftware System Testing
System Tests