Unable to fetch the token from another thread group in Jmeter - api

In HTTP Request, I gave login credentials then login got success, access_token generated.
Then by using JSON Extractor, I extracted the access_token and name as "auth_token".
Then in Beanshell Assertion, I added script "${__setProperty(auth_token, ${auth_token})};"
Then in HTTP Request Defaults, I added Parameter "Authorization" "${__property(auth_token)}" (in Test Plan level)
I cannot fetch that token in Thread_group-2, which is generated in Thread_group-1.
suggestions, please??

You need to ensure that the Thread Group which extracts the token is executed before the Thread Group which uses the token, either tick "Run Thread Groups consecutively" on Test Plan level:
or switch to setUp Thread Group which is being executed before all "main" Thread Groups
In general you should not be passing values between Thread Groups as thread group represents a logical group of business users and authentication/authorization is an integral part of the business flow so my expectation is that you need to have authentication and other actions in the single Thread Group. If you want to perform authentication only once - put it under Once Only Controller
Using Beanshell is kind of a performance anti-pattern, consider moving to JSR223 Test Elements instead.
I also have doubts regarding correctness of your token usage, I think you should rather use HTTP Header Manager instead and configure it to pass Authorization header with the value of Bearer followed by whitespace followed by your token

Related

Jmeter - How to use the variable set in first request for all the threads in one thread group

I would like to run one request only once, and get an authorization token from its response using Json extractor and then use that token as header in another request that runs under the same thread group.
I tried to use "setup Thread Group", but the variable value was not available to the main thread group.
So, I used "If Controller" under the same thread group, with below condition:
${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
This is making the specific request to be executed only once.
However, variable value is available only for one thread for the subsequent requests, but not for all the threads. Below is the picture of results tree:
May I know how to access the variable value set in first request for all the threads instead of just one thread?
As per JMeter Documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
So if you want to use a single token for all threads (virtual users) you need to convert it into a JMeter Property first like:
Under the If Controller use __setProperty() function to convert your variable into a property
In your GET request use __P() function to read the property value
Another way of sharing data between threads (even if they're in different thread groups) is using Inter-Thread Communication Plugin
You are not allowed to use the variable created in one thread in other threads this is how JMeter scoping for variables extracted works.
You already near to solution, providing steps so any one can approach problem like this:-
Use If Controller to make sure only once request being made to get Authorization token
Extract the token using post-processor
Save token in the property using post-processor so that same token can get used in multiple threads
Use newly created property instead of a variable in subsequent requests by referring property function instead of variable
You can use one JSR223 post-processor like below to create a property from the variable:
Please note that if you are mimicking multiple users using thread group, ideally you should create different auth token for different users.
P.S.: Balzemeter have an article which uses the BeanShell to demonstrate how to solve this problem

How to run a thread after every 25min in jmeter

I am hitting a rest API using HTTP GET request that contains authentication token in header. So in a thread group i am hitting authentication server to get authentication token. In the subsequent thread groups i am going for HTTP GET request
TEST plan (property auth)
ThreadGroup(authentication)
http GET to get authentication token
extracting response and putting in auth
Thread group(GET)
http GET
Thread group(GET)
http GET
and soo on
Here the authentication token will expire for every 25 min so i need a mechanism to generate authentication token for every 25 min with out interrupting the work of on going HTTP GET calls
You can go through the below screenshots for more details. Let me know in case you still need more help on this.
Use Beanshell Assertion to store variable into Property variable
Use the property variable in this way to fetch the value
Just add Test Action sampler (since JMeter 5.0 the element is known as Flow Action Control) after your http GET to get authentication token request and configure it to Pause for 1500000 milliseconds (25 minutes * 60 seconds in minute * 1000 milliseconds in second)
You can configure it in the below manner.
ThreadGroup(authentication)
http GET to get authentication token
Extract the authentication token and save in a jmeter property variable
The benefit of saving token in a property variable is that you can access property variable in other thread groups as well. Once configured in the above manner, you can execute the ThreadGroup(authentication) after every 23 minutes so that it can update the Jmeter property variable (authentication token) before expiring.
To set a property variable: ${__setProperty(variable, ${variable})}
To access a property variable: ${__property(variable)}
You can try this at your end and let me know how it goes.

J-Meter gives false results

I'm trying to learn J-Meter.
When I'm running a sample script of logging into a site using both valid/invalid credentials,it doesn't stop thread execution when invalid login credential is used and also login is not recorded in database.
Does it actually login to the website or only creates virtual login to create a similar environment.Is there any way to achieve this using Samplers?
JMeter is/acts as a headless browser.
Whatever your browser with an UI does, JMeter can also do - except executing a javascript. So, If you had recorded your script correctly - JMeter can login to the actual application as well.
Jmeter is not like QTP/Selenium. It does not know if it is a valid credential/invalid credential. It passes/fails the request based on the HTTP codes. If the HTML response from the server comes with a 200 http code, It passed for JMeter. If the server responds with code 500, JMeter fails the request. But JMeter also provides a way to validate the response you get - Assertion. You can use Response Assertion to see if you are seeing the home page or not to confirm if the user has logged in successfully.
To stop the test on error, select the appropriate option here in thread Group properties.
JMeter is a very nice tool & have been using it for 2 years with no issues.
Good luck!
Does your script have Config Element -> HTTP Cookie Manager? It needs cookie for the login function.
If your script has many transactions with the same level with login transaction and the option you select in your Thread Group is Continue, all transactions will be executed no matter login transaction is passed or failed.
In case you want the other transactions will not be executed if login fails, let add a Regular Expression Extractor as child of the login transaction to retrieve the text Dashboard, put other transactions into a Logic Controller -> If Controller. Suppose the Regular Expression Extractor has name Dashboard and Default value is NotFound, then the Condition of If Controller will be "${Dashboard}"!="NotFound"
JMeter automatically treats 2xx and 3xx HTTP Response Codes successful so it won't be able to detect failed login unless you explicitly tell it to check presence or absence of some specific content in the response data.
So if you add a Response Assertion you will be able to conditionally fail sampler and choose what to do in case of failure via "Action to be taken after a Sampler error" on Thread Group level.
See How to Use JMeter Assertions in Three Easy Steps guide for more details on the assertions domain.
If you're unsure what JMeter Sampler is doing you can check request and response details via View Results Tree listener. If you cannot simulate login event in majority of cases it is due to missing HTTP Cookie Manager and/or failed correlation of dynamic mandatory parameter(s) like Viewstate, CSRF token, etc.

OpenID Connect Persist ID Token to Server-side Callback Function

Suppose that I invoke the following HTTP request:
https://accounts.example.com/oauth2/auth?
scope=openid+email&
nonce=53f2495d7b435ac571&
redirect_uri=https%3A%2F%2Foauth2demo.appspot.com%2Foauthcallback&
response_type=id_token+token&
client_id=753560681145-2ik2j3snsvbs80ijdi8.apps.googleusercontent.com
Which yields the following redirect response:
https://oauth2demo.appspot.com/oauthcallback#
access_token=ya29.AHES6ZSzX
token_type=Bearer&
expires_in=3600&
id_token=eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY...
What is the point of the callback parameter, given that the returned metadata, containing id_token, etc., is positioned after a hash fragment in the URI, and are therefore not persisted as parameters to the callback function? How can a server-side callback receive the various tokens?
The response type that is used here is a so-called "Implicit" response type which is primarily meant for in-browser (e.g. Javascript) clients, in which case the Javascript code that is served on the callback URL can access the parameters in the fragment. Web applications should do either one of:
stick to the code flow which is meant for web application clients
use the Form Post response mode (http://openid.net/specs/oauth-v2-form-post-response-mode-1_0.html) if supported by the OP
serve Javascript on the callback URL that parses out the parameters from the fragment and POSTs them to the server
Bottom line is that if you need the tokens server-side, you should most probably use the code response type.

Handling dynamic http requests instead of hardcoded http requests in Jmeter

I'm creating a 50 users load test on a JSF web application.
I record a scenario using JMeter proxy for one user who logs in, does some db operations and logs out. After recording the scenario, the recorded test contains http requests and data that particularly belongs to the user used while scenario recording.
At the time of running the test for 50 unique virtual users, the recorded test sends http requests and data which was in the recorded scenario. But in our application, the http requests and data vary depending upon the user. So how do I handle such situations in JMeter when it comes to methods being called depending upon the existence or non-existence of data for a user after logging in?
To be precise how would I make changes in my Test plan to manage dynamic urls and dynamic data for each virtual user?
Latest versions of JMeter allow you to write the whole parameters (raw data) from scratch, so you could use variables in this field.
To achieve dynamic URLs use a Regular Expression Extractor (Post-Processor) on a prior request that define what request will be sent and use the variable in HTTP Request's path field.
If you know what request each type of users will send you could use If Controllers and test a thread variable, created by a previous Regular Expression Extractor, and inside each controller add the specific request.
If the subsequent request for each user is defined by the server, using redirection, just check "Follow Redirection" field.
See JMeter Wiki for more examples on how to do this.