Problem with usage __base64Encode function to encode Random Variable.
I use Random Variable to generate unique email for user. (in each iteration of test scenario)
I want to use :
${__base64Encode(${randomvar})} which generate encoded ${randomvar}
Real example user defined variable 'babretr' defined below:
babretr=${__base64Encode(${randomMail}${timeStamp}#jmeter.soi:Kazek2017#)}
after base64 encode looks:
babretr=JHtyYW5kb21NYWlsfTIwMTcwNTE5MDg0MTI0NDcyWkBqbWV0ZXIuc29pOkthemVrMjAxN0A=
after base64 decode looks:
${randomMail}20170519084124472Z#jmeter.soi:Kazek2017#
Jmeter Random Variable ${randomMail} is not encoded.
With different variables problem does not exist.
You could add Beanshell PreProcessor on your sampler and put something like:
import java.util.Base64;
import org.apache.commons.lang3.RandomStringUtils;
String sufix = "#jmeter.soi:Kazek2017#";
String address = Base64.getEncoder().encodeToString(new String(
RandomStringUtils.random(10) + vars.get("timeStamp") + sufix).getBytes());
vars.put("encodedAddress", address);
After that, just use ${encodedAddress} in your sampler.
Related
I want to extract a hex data from a response in JMeter.
I have extract the text value and I want to convert it to hex but I don't know how to do it.
I want to put the hex data in a variable
If you have another method to extract hex data in JMeter, I'll take it.
Edit
I tried to put on a Beanshell Postprocessor sample :
import java.math.BigInteger;
vars.put("CHALLENGE",new BigInteger(1, vars.get("CHALLENGE").getBytes(/*YOUR_CHARSET?*/)));
But I have this error :
Static method format( java.lang.String, java.math.BigInteger ) not found in class'java.lang.String'
You should use Integer.toHexString
Returns a string representation of the integer argument as an unsigned integer in base 16.
In your case to override (why not use different variable?) variable:
vars.put("CHALLENGE", Integer.toHexString(vars.get("CHALLENGE").getBytes()));
You can perform conversion using:
JSR223 Test Elements
Groovy language
DatatypeConverter class
Demo:
I want create a property from a variable. The variables was created by calling a variable from a xpath extraction, then using a substring to then get the last 4 characters. The substring string value is saved to a variable, then set to a property.
When I run the script, the log.info(vars.get("lastcard")); returns the value of the variable. However it then fails to save to a property, because when that property is called(${__property(lastNum)} it will display - ${lastcard}
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3;
String tesTe = vars.get("card");
String last4 = tesTe.substring(tesTe.length()-4,tesTe.length());
vars.put("lastcard", String.valueOf(last4));
log.info(vars.get("lastcard"));
${__setProperty(lastNum,${lastcard})};
Any ideas as to what is going on
You should read user manual about scripting:
ensure the script does not use any variable using ${varName}
You should use JSR223 variables vars and props to handle variables and properties. In your case change last line to:
props.put("lastNum", vars.get("lastcard"));
Also you can set variable in shorter way:
vars.put("lastcard", vars.get("card").substring(tesTe.length()-4));
There was 2 changes that need to be made to resolve the issues.
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3;
String tesTe = vars.get("card");
String last4 = tesTe.substring(tesTe.length()-4,tesTe.length());
vars.put("lastcard", last4); //Already string therefore no need to use String.valueOf()
log.info(vars.get("lastcard"));
props.put("lastNum",vars.get("lastcard")); //Setup to use props.put instead of set property
I'm calling a api serice that requires a http header with the hash value of the request body.
I'm trying to use a beanshell post processor in JMeter to automatically create the sha-256 hash of the request body. The hash value is right up until I introduce a line break in the request body (which is a pain as the JSON message spans several lines!)
I assume it's something to do with hidden characters however I can't work out what is going wrong! :(
When I compare the hash generated by JMeter to seperate Hash generator tools it is an exact match until there are line breaks, then JMeter is wrong.
Why is JMeter generating an incorrect hash when there are line breaks?
My code is:
[import org.apache.commons.httpclient.auth.DigestScheme; // necessary imports
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.services.FileServer;
import javax.xml.bind.DatatypeConverter;
import java.security.*;
String body = sampler.getArguments().getArgument(0).getValue();
String hash = DigestUtils.sha256Hex(body);
log.info(hash);
What is your input data and what output do you expect?
Since JMeter 3.1 it is recommended to switch to JSR223 Test Elements and Groovy language so:
Given the following request body:
{
"foo": "bar"
}
And the next Groovy code to generate a SHA-256 hex string:
def sha256Hex = { input ->
java.security.MessageDigest.getInstance("SHA-256")
.digest(input.getBytes("UTF-8")).encodeHex().toString()
}
log.info(sha256Hex(sampler.getArguments().getArgument(0).getValue()))
I'm getting dbc67f71c921b5b7649481a5123d94dfa919748d2962889681d96438033c548f value which is basically the same I can see using https://hash.online-convert.com/sha256-generator generator.
I need to extract the dynamic value "BSS1,DS1,HYS1,MS1,PTS1,QS1,USG1,YS1,RT10086,RT10081,RT10084,RT10082,OT10076,RT10083,UT10081,RT10085,"
from the string response "ACCOUNT_DETAIL_ACCOUNT_PRODUCT_SERVICES_EDIT_UPDATE_NameSpace.grid.setSelectedKeys(["BSS1","DS1","HYS1","MS1","PTS1","QS1","USG1","YS1","RT10086","RT10081","RT10084","RT10082","OT10076","RT10083","UT10081","RT10085"]);"
I have tried using the regular expression extractor :
Regular Expression :Keys\(\[\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\"]\)
template : $1$$2$$3$$4$$5$$6$$7$$8$$9$$10$$11$$12$$13$$14$$15$$16$
But the above regular expression works only if there are 16 values in the response. If the response contains less number of values, for example, "ACCOUNT_DETAIL_ACCOUNT_PRODUCT_SERVICES_EDIT_UPDATE_NameSpace.grid.setSelectedKeys(["BSS1","DS1"]);"
then the above regular expression doesn't work.
How can I extract the values in the response if the total count is unknown?
Also the double quotes in the response need to be omitted.
Is there any post processor using which dynamic values can be extracted?
Any help is greatly appreciated.
I believe it will be easier with some scripting.
Add Beanshell PostProcessor as a child of the request which returns aforementioned response
Put the following code into the PostProcessor's "Script" area:
String response = new String(data);
String rawKeys = response.substring(response.indexOf("[") + 1, response.indexOf("]")); // get the data inside square brackets
String keysWithoutQuotes = rawKeys.replaceAll("\"", ""); // remove quotes
String[] keyData = keysWithoutQuotes.split("\\,"); // get array of keys
for (int i = 0; i < keyData.length; i++) { // store array of keys into JMeter variables like
vars.put("Keys_" + (i +1), keyData[i]); // Keys_1=BSS1, Keys_2=DS1, etc.
}
vars.put("Keys_matchNr", String.valueOf(keyData.length)); // set Keys_matchNr variable
Where:
data is byte array containing parent sampler's response data
vars is a shorthand to JMeterVariables class which provides read/write access to JMeter Variables.
As a result you'll have variables like:
Keys_1=BSS1
Keys_2=DS1
..
Keys_matchNr=X
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for additional information on Beanshell scripting in JMeter and some more examples
Hi I am passing an email which is a time function like below
email = ${__time(MMddyy)}_${__time(HMS)}#yopmail.com
The value of this function changes eveytime I call the variable email.
I would like to store this value that is generated from this function into a variable and use that in other requests.
So currently I am getting two different emails in two different http requests since there is some time lag between my two http requests.
what I would like to do is .. store the email that is being sent in first http request by extracting the value from the request and pass it in the second http request.
POST data:
email=062915_160738%40yopmail.com
I know the way to extract from html response.. but is there any way to extract from request in jmeter?
If so can someone pls tell me how to achieve this?
thank you
Add a Beanshell PostProcessor as a child of the request which sends that POST request
Put the following code into the PostProcessor's "Script" area
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
Arguments argz = ctx.getCurrentSampler().getArguments();
for (int i = 0; i < argz.getArgumentCount(); i++) {
Argument arg = argz.getArgument(i);
if (arg.getName().equals("email")) {
vars.put("EMAIL", arg.getValue());
break;
}
}
Refer generated value as ${EMAIL} where required.
Clarification:
above code will extract the value of email request parameter (if any) and store it to EMAIL JMeter Variable
ctx - shorthand to JMeterContext class instance
vars = shorthand to JMeterVariables class instance
Arguments and Argument - you can figure that out from JMeterContext JavaDoc
See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in JMeter.
Instead of the entire email, you can store the timestamp value in a variable and then use this timestamp variable to create email anywhere you want.
This way you will can have same email every where.
Add a Beanshell PostProcessor & Add following script:
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
Arguments argz = ctx.getCurrentSampler().getArguments();
for (int i = 0; i < argz.getArgumentCount(); i++) {
Argument arg = argz.getArgument(i);
String req_body = arg.getValue();
vars.put("req_Json",req_body);
}
here we get the output in json format:
${req_Json}=
"email":"062915_160738%40yopmail.com",
"name":"abc xyz"
Now using jp#gc Json Path Extractor extract the value of email
Json expression = $['email']
and store the value in email_value_extacted
now use the variable ${email_value_extacted} anywhere you want to use.
finally,
${email_value_extacted} = 062915_160738%40yopmail.com
Is it HTTP Sampler? If so, just put into beanshell postprocessor:
String prevQuery = prev.getQueryString(); //your request text
System.out.println(prevQuery );
Also works for any samplers:
String prevQuery = prev.getSamplerData();
You can use Regular Expression extractor to extract the e-mail address from the request URL.
Add Regular Expression Extractor as a child of sampler which sends the post request.
In the Regular Expression Extractor select URL in Response Filed to check instead of Body.
You should be able to extract e-mail id from the request in this way.