Saturday, September 6, 2014

Base64 encode using Jmeter


In my application, we want to encode one of the jmeter varible to base64 variable  . We did this using using Bean shell pre processor. Below is the scenario


Scenario: I want to encode jmeter variable (session)  to Base64 and need to pass this value to Jmeter Sampler. Below is the steps to do this 

Download this project from GitHub

Step 1: Get dynamic session value using Regular expression extractor.
Add Thread Group to Test Plan and Add the sampler to Thread Group  , provide the necessary details to Http Sampler

IP:raw.githubusercontent.com
Port: 80
Path:/mcheepati/api/master/homepage
Method: Get


Step 2: Add the Regular expression extractor post processor as a child to Http Sampler. And add the below values to "Regular expression extractor" 



Step 3:Pass the encoded Base64 value(session) as a variable to http post method in this step.
Add the Http sampler as a child to Thread group 

 

Add the bean shell "pre Processor" as a child to this Http sampler and the below code to bean shell processor 



import org.apache.commons.codec.binary.Base64;

String emailIs= vars.get("email");

byte[] encryptedUid = Base64.encodeBase64(emailIs.getBytes());
      vars.put("genStringValue",new String(encryptedUid));

Note: We added Pre Processor as child, because  pre processor will executes before running the Http sampler.


Step 4:Save and run the Jmeter script and verify the post call values for Base64 encoded value.
Actual Value is              :jdwqoeendwqqm12sdw
Base64 encoded value is:amR3cW9lZW5kd3FxbTEyc2R3




Validate the jmeter Base64 response values from online Base64 http://www.base64encode.org/

4 comments:

Anonymous said...

Hi Madhu Sudhana. Thanks for the post.
In my case i have two parameters. Usernames that I read from csv and Passowrd i have kept common so reading from user defined variable.
I need to uae both values like username:password and so base 64 bit encodong.
How to achieve it by pre processor script?
And where can I find more information about pre processor shell script?
Plz email me palakshar@gmail.com.
Thanms

Anonymous said...

Use the below code into BeanShell PreProcessor. It works for me. Hope it helps.

import org.apache.commons.codec.binary.Base64;


//log.info("Base64 Encoding UserName & Password");


//Create the complete string which needs to be encoded
String auth = vars.get("UserName") + ":" + vars.get("Password");


//log.info("auth: " + auth);


//Encode the string
byte[] encoded_auth = Base64.encodeBase64(auth.getBytes());


//Save the encoded byte array into a String Variable
vars.put("encoded_auth", new String(encoded_auth));


//log.info("encoded_auth: " + vars.get("encoded_auth"));

Unknown said...

Hi

Unknown said...

I need to encode and decode the value that are generating in response data. How to get the data from response?
How to convert base 64 to text ?
How to split the value ?
Using beanshell post processor