I'm trying to do some testing using JMeter but I'm facing an issue trying to do some complex stuff.
I have a login HTTP request test that comes back with a response which includes an auth_token. I need to add ":" at the end and encode it to base64 to use that value on the request of another test.
I've been reading that it can be done using BeanShell but I could not achieve it yet. I will appreciate if someone could give me some steps to perform this task.
I assume you know how to get this auth_token into a JMeter Variable via i.e. Regular Expression Extractor
If you're have JMeter Plugins installed - you can use __base64Encode() function like:
${__base64Encode(${auth_token},auth_token_encoded)}
If you don't have the plugins/cannot have/don't want to have - here is how to do it with Beanshell.
Add Beanshell PostProcessor somewhere after Regular Expression Extractor (or other PostProcessor you're using to fetch the auth_token value
Put the following code into the Beanshell PostProcessor "Script" area:
import org.apache.jmeter.protocol.http.util.Base64Encoder;
String auth_token = vars.get("auth_token");
String auth_token_encoded = Base64Encoder.encode(auth_token);
vars.put("auth_token_encoded", auth_token_encoded);
See How to Use BeanShell: JMeter's Favorite Built-in Component to get started with Beanshell scripting.
Both cases assume:
you have "auth_token" value stored in ${auth_token} JMeter Variable
you will be able to access the encoded value as ${auth_token_encoded}
I had a similar test case where I need to put a file as Base64 encoded String into the body of a HTTP Request.
Instead of a BeanShell I used the groovy script functionality¹:
{
"example": "${__groovy(new File('${SCRIPT_PATH}/test.file').bytes.encodeBase64())}"
}
If you already have a String this snippet would work similar:
{
"example": "${__groovy('string to encode'.bytes.encodeBase64())}"
}
Or this is the usage with a user defined variable:
{
"example": "${__groovy('${STRING_VARIABLE}'.bytes.encodeBase64())}"
}
¹ ${SCRIPT_PATH} is a user defined variable pointing – in my case – to the folder of the loaded jmx-file: ${__BeanShell(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
Related
I Have a JSON response in string so I used JSR223 processor and extracted the required token, is there any way to make that variable as global variable for all the threads to use
COuldn't get any slutions or haven't got the right link to check
As per request I have added screenshot for more clarity in my question
enter image description here
I even tried using
$(_setProperty(text3,${token},)};
still no use
I have extracted a string from response and assigned to text3 and I want to make it as global variable in thread group so that It can be used all other next API's I tried using props.put() however it didn't worked
Set-up
Request #1
POST https://url/
RegEx Extractor
Response #1
{
"Token":"WkQTxNnZRR0nofyJzb-kioALlXgwc7cN9rokXrKzWmtB2BDedUXeQnd94S5KWvaz0",
"ExpirationUTC":"2121-09-17T14:39:57.504Z",
"TokenId":"string"
}
Request #2
GET https://some-other-url
Header Manager
As shown:
Result
Instead of
Authorization: Bearer WkQTxNnZRR0nofyJzb-kioALlXgwc7cN9rokXrKzWmtB2BDedUXeQnd94S5KWvaz0
we have
Authorization: Bearer ${token}
Debug Component Results - Starting to Look Like a Variable Scope Issue
... but:
Result of RegExp Tester
Here, I used the same reg-ex as I used in the Reg Ex Extractor, and it finds the desired string.
JSON Extractor Attempt
Still says Bearer ${token}
As per JMeter Documentation Variables are local to a thread hence you cannot refer the variables which are set in one Thread Group in another Thread Group.
You either need to convert the variable into a JMeter Property using __setProperty() function in 1st thread group and load it using __P() function in 2nd thread group or go for Inter-Thread Communication Plugin
Also be informed that JSON is not a regular language hence using regular expressions is not the best choice for extracting the token from the response
I have been able to write a python script to get Base 64 auth for my username and password (Admin:password) equal to --> Basic QWRtaW46cGFzc3dvcmQ=
When I add that to my header manager as:
Authorization Basic QWRtaW46cGFzc3dvcmQ=
all my HTTP Requests succeed.
in Jmeter I have googled and I find to add below in Bean PreProcessor:
import org.apache.commons.codec.binary.Base64;
String username = vars.get("Username");
String password = vars.get("Password");
String combineduserpass = username + ":" + password;
byte[] encodedUsernamePassword =
Base64.encodeBase64(combineduserpass.getBytes());
vars.put("base64HeaderValue",new String(encodedUsernamePassword));
System.out.println(encodedUsernamePassword);
but that system output gives me --> [B#558e816b which is incorrect
when I add that to my Header manager like this
Authorization Basic ${base64HeaderValue}
my HTTP Req obviously fails. The Base64 for "Admin:password should really be Basic QWRtaW46cGFzc3dvcmQ= and not [B#558e816b
You are trying to print byte array. You can print the new variable as:
System.out.println(vars.get("base64HeaderValue"));
Also your Header Manager should be under your HTTP Request so it be execute aftet script and before your request
Instead of scrpting you can use JMeter plugin of custom functions and use inside Header manager the __base64Encode function similar to:
${__base64Encode(test string, base64HeaderValue)}
To do Basic Auth, just add HTTP Authorization Manager to your plan as per this answer:
JMeter Basic Authentication
It would be configured like this if your server URL is http://localhost:8080/test:
There is no need for scripting here.
I would recommend switching to JSR223 PreProcessor and Groovy language as:
Groovy supports all modern Java language features including (but not limited to)
encoding byte arrays into Base64
decoding Base64 strings
Groovy performance is way better comparing to Beanshell
Groovy equivalent of your code would be:
vars.put('base64HeaderValue',(vars.get('Username') + ':' + vars.get('Password')).bytes.encodeBase64().toString())
I am hitting an http url and need url contents into property in jmeter.
I have done the fetching part from url,but unable to store the value in properties using the jmeter.
For e.g.
Request is like
http://url/user=admin,password=admin
I need property in jmeters
property1(user)=admin
property(password)=admin
Given you have already extracted what you need it might be easier to use __setProperty() function like:
${__setProperty(foo,bar,)}
creates "foo" property with the value of "bar"
If you still want to go the "Beanshell" way, you can use props shorthand which provides read-write access to JMeter Properties (in fact it's instance of java.util.Properties) for properties manipulation.
The Beanshell script:
props.put("foo", "bar");
will create a property "foo" having value of "bar".
Returning to your use case, if your URL looks like http://example.com/?user=admin&password=admin use the following Beanshell code:
Map parameters = ctx.getCurrentSampler().getArguments().getArgumentsAsMap();
String user = parameters.get("user");
String password = parameters.get("password");
props.put("user", user);
props.put("password", password);
should do what you need. See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter.
I am testing RESTapi with (json format) using (HTTP Request sampler) in jmeter. I am facing problems with the PUT calls for update operation.
The PUT call with parameters don't work at all using (HTTP Request sampler), so now i am using the post body to pass the Json.
How can i pass the extracted values from the previous response to next PUT request in thread group? Passing the 'Regex veritable' to PUT call in Post body don't work, it doesn't take ${value} in Post body.
How do i carry out UPDATE operations using (HTTP Request sampler) in Jmeter?
Check that your regexp extractor really worked using a debug sampler to show extracted value.
Check your regexp extractor is scoped correctly.
See this configuration:
A Variable:
Its use with a PUT request:
The sampler result: