Login Once and Re-use access Token in JMeter - jmeter

I have a requirement where we need to login once for given test users and re-use access token generated by loginscript for remaining API call for a duration of 1 hour.
One Approach which I think is to Login & write the tokens in csv file and then read these tokens from CSV file for executing remaining API's, but with
this approach we need to write token in CSV file in an agent.
Since, we will be running test in load test rig. we do not have access to write in agents.
Is there any approach using which we can Login once, generate token in some variable and re-use token.
Appreciate if any one can help on this.

Login once for each user - put the logic for obtaining the token under Once Only Controller and it will be executed during 1st iteration of the Thread Group
Login once by 1 user and use the token by all users - put the logic for obtaining the token under setUp Thread Group, save the token into a JMeter Property using __setProperty() function and read it in the main Thread Group using __P() function

Related

OAuth token generation using Jmeter for multiple User-Credentials

I am testing an application hosted in gcloud, to execute the test using jmeter I require OAuth token for respective User credentials. I am able to generate the token for one credentials, however I have not been able to find a way to generate them for more than one users.
If anyone has faced such problem or has any glimpse please let me know.
Note:
I need a fresh token with every iteration since token expires in every 60 mins
I am able to generate token manually to run the test
I have tried auth/header manager for the process
I have tried console auth code generation code as well
Above all generate auth code for a specific credential, wherein I have to keep the application active.
So you want to apply load to app backend using authenticated users.
If so, why don't you use
CSV File to store your test credentials
Use JMeter's CSV Config to read those credentials
once only controller for authentication, extract access_token and refresh_token
Use tokens to make calls to your backend
If you need to run loadtest / soak test for longer than one hour you can use if controller to verify the token validity and renew the token if necessary.
Hope this helps.
If you need to refresh the token each 60 minutes it makes sense to create a separate Thread Group which will be executing a token refresh request each 60 minutes.
The token can be passed to the main Thread Group using __setProperty() function, you can make the token value thread-specific by combining it with __threadNum() function like:
In "token" thread group:
${__setProperty(token_${__threadNum},${token},)}
In "main" thread group you can read the value using __P() function:
${__P(token_${__threadNum},)}
Demo:
More information: Knit One Pearl Two: How to Use Variables in Different Thread Groups

Jmeter - Run multi-threaded users simultaneously without affecting the flow

I have a user flow (create base64 header auth > register request > Register > login > sending data) which I want to run simultaneously (100 users).
If I use the Thread Properties as it is, it will run in asynced way.
Examples:
User 3 will try to login with token of user 1, user 40 will register with registration header created for user 24 and so on..
Token for login extracted from base64 process,
another token is extracted from 'register-request' and used as variable in 'registration' header.
I want first user to register with token created in first register-request and login with token created in base64 process, and so on for next users.
Is there a way to do so?
Normally JMeter virtual users store variables in the thread-local storage so given your test plan design is good each thread (virtual user) should use its own header.
Not knowing the details of how your header is generated it's hard to say what's wrong, I would recommend using Debug Sampler and View Results Tree listener combination and double check the header value (and any interim variables) - hopefully you will be able to determine the cause. If not - you will need to share the test plan somewhere somehow.
Other things to consider:
If your application requires Auhtorization header in form of username:password encoded into Base64 you actually don't need to generate the header manually, you can use HTTP Authorization Manager which automatically generates and adds the relevant header to the requests. Check out How to Use HTTP Basic Authentication in JMeter article for more details if needed.
There is __base64Encode() function which you can use for encoding a custom string into Base64, the function can be installed as a part of Custom JMeter Functions bundle using JMeter Plugins Manager

How to regenerate token in JMeter at fix interval time

I am doing API Performance Testing for my application. I am creating a script using JMeter. Now, I am facing the issue that on our application auth_token is only valid for an hour. After that it becomes invalid and we need to generate new token. So how can I manage this mechanism in JMeter where I can regenrate token after fixed interval?
You and create a separate Thread Group where you will have i.e. Test Action sampler configured to "sleep" for 1 hour and the logic to "regenerate" the token.
So you will have 2 Thread Groups:
"Normal" one where your API testing happens
"Another" one where a new token will be generated each hour
You can pass the token from "Another" to "Normal" thread group using __setProperty() function

jmeter session data and runtime parameter pass for multi user

I have scenario to test different concurrent multiuser with different login and password from CSV ,
and passing session info & values and run time generated values pass as parameter to next screens for multiple pages.
How can I build Test plan & configure JMeter.
The below mentioned steps will help you achieve the above scenario:
Record the scenario
http://jmeter.apache.org/usermanual/jmeter_proxy_step_by_step.pdf
Do correlation for dynamic parameters
http://apache-jmeter.blogspot.com/2011/12/correlation-regular-expression.html
Use CSV for login credentials
http://ivetetecedor.com/how-to-use-a-csv-file-with-jmeter/
hope this will help
Go through the following link (my answer), How to send parameters from one http request to other in jmeter
make sure to add http cookie manager & http session manager in your test plan for run time generated values.

How to login to an email Address using jmeter?

I am testing some api for our product using jmeter. To test the api to verify the user's email address I need to login to the mail account. I have the email and password of a test user. How can I login using an api or any other way in jmeter?
Is it possible at all?
Yes that is possible. You'll have to create a test plan with multiple steps but these depend on the concrete implementation of the login.
First you must identify the following two locations:
The GET request that gets the initial login form (if this is required).
The POST request that posts the login credentials.
If your service has a stateless login form you can even skip the first step.
Otherwise the response of the GET request will contain stuff which needs to be extracted (e.g. the JSESSIONID if you are using JSF) and sent as a parameters in the POST request. You can use the Regular Expression Extractor of JMeter to extract these values and provide them as variables for use in the subsequent requests.
Here is an example screenshot of a HTTP Request element configured to POST login data to the url /common/j_security_check
You might also need to intercept an authentication token from the server response and then pass that into following requests.
Here is a blog entry with a couple of video tutorials on logins with JMeter:
http://community.blazemeter.com/knowledgebase/articles/80479-how-to-use-jmeter-for-login-authentication
You can use jmeter to login,
two ways:
parameterization using csv file(for more users)/user defined variables(UDV)
Login config elements(for single user)
Now that I understand you want to automatically click on a confirmation link from an email using JMeter, here is a blog entry that explains exactly how to do that:
http://blazemeter.com/blog/how-create-jmeter-script-check-email-during-registration-and-grab-confirmation-url
Ophir

Resources