Upload External JavaScript Files in JMeter


How to use external .js(java script files) in JMeter ?

Sometimes there are scenarios when one required using external js files in JMeter in order to use functions inside in it. Now the question arises-Is it really possible? The answer is yes, thanks to JMeter functionality through which you can import or configure the external .js files and can use the functions inside in it. In this tutorial, we can see how we can achieve this.

We have one scenario, in this; we want to run one http request. This request will run with some token id generated by some external .js file which developer has created. So you need to generate token before running this HTTP request and for this, you would have needed to import this external .js files which will generate token for you. In order to achieve this, we will follow the below steps:

Step 1: Figure out the .js files from developer so that you can import/configure in your JMeter. We have:

('md5.js');
('frameworkResourceVariableJS.js');

After that we will call one function in which we will pass one url and this function will return the same url with token-id appended:

getLinkUrlSecurityToken(${url});

Step 2: Add two JSR223 Sampler for two .js functions: Test Plan->Thread Group->Sampler->JSR223Sampler
Then, select javascript as a Script language dropdown. Provide the path of the .js file in Script file (overrides script).
Step 3: Add load() method to load the .js file(s) in your JMeter
load('md5.js');
load('frameworkResourceVariableJS.js');
getLinkUrlSecurityToken(${url});


Step 4: Add Listener:  Thread Group->Listener->View Result Tree
As you can see both the js files has been loaded successfully.
You can import external JavaScript into JSR223 Sampler using load directive.
1.     Download latest release of crypto-js from https://github.com/brix/crypto-js/releases (I used 3.1.9) and unpack it to JMeter's "bin" folder
2.     Add the next line to the beginning of your JSR223 script:
load('crypto-js-3.1.9/crypto-js.js');
3.     You should be able to access your JavaScript functions directly in the JSR223 Sampler


Be aware that JavaScript being interpreted via Rhino/Nashorn has serious performance drawbacks therefore if you are planning to call this funciton by many threads it makes more sense to rewrite your functions in Groovy.


No comments:

Post a Comment

|Software Testing Glossary Series|CTFL|N|O|P|

Non-Functional Requirement: Non-Functional Requirement is a requirement that describes how the component or system will do what it is s...