Monday, February 13, 2017

Code To Convert Excel to Base64 -Jmeter

 Consider a scenario as follow like
                                1.Click on Upload button
                                2.Select the excel file to upload
                                3.Click upload
              We can record it and can correlate, but what if the excel file which is getting uploaded is converting into base64 and then getting uploaded. If this is the case we can write a simple bean shell code to convert that file into base64 string and save it into Jmeter Variable.

Below is the code for this conversion:
import java.io.*;
import org.apache.commons.codec.binary.Base64;

BufferedReader infile = new BufferedReader(new FileReader("--FileName with Extension--"));
String br;
String v1="";

/*Looping till the end of the file*/
while((br=infile.readLine())!=null)
{
/*Converting each byte to relevant base64 format */
byte[] encryptedUid = Base64.encodeBase64(br.getBytes());

/*Appending the converted base64 string */
v1=v1+new String(encryptedUid);
}

/*saving the string v1 into Jmeter variable genStringValue */
vars.put("genStringValue",v1);


Below are snapshots of the working code in Jmeter:

1.Excel file which has to be converted into base64 format


Sample Excel file to base 64

2. Jmeter Script having Bean shell code for converting the excel file into base64
JMeter Beanshell code
3.Below picture shows the Jmeter variables in which genStringValue consists of base64 format of the excel file
Excel in form of base64