0

I have added the Test Plan in below-following orders

1.Test Plan with user defined variables
2.Header Manager
3.Thread Group 1
4.Http Request
5.JSON extractor
6.Thread Group 2
7.Http Request
8.BeanShell Preprocessor
9.Result Tree

Screenshot

How to pass the access token(s) from the first thread group to the second thread group?

Janesh Kodikara
  • 1,670
  • 1
  • 12
  • 23
Pavithra T
  • 11
  • 1
  • 1
  • You may use Test Fragment inside a Thread Group and put HTTP Header Manager on to top level in your test fragment. – Dimio Dec 25 '22 at 19:50

3 Answers3

0

Variables cannot be passed/shared between the thread groups.

enter image description here

There could be a number of solutions.

Option 1

Use JMeter properties to share the access token between the thread groups. props.put("accessToke", accessToke) to add the token and use props.get('accessToken') to retreive the values from the second thread group.

In this solution, you can share just one token across the thread groups.

Options 2

Using Inter Thread Communication plugin.

These queues work in First-In-First-Out manner. You can put a string value into a queue from one thread, and then get that value from another thread, even in another Thread Group.

Janesh Kodikara
  • 1,670
  • 1
  • 12
  • 23
  • Could you please explain how to do the Option 1? Where to put the access token? Is there listner or postprocessor to do so? – Indi Apr 01 '23 at 11:21
0

There were at least 3 errors during your last test execution:

enter image description here

Check jmeter.log file for details, the reason should be there


  1. You should be using different thread groups for representing different groups of business users, if you're simulating authentication flow it makes sense to keep both HTTP Request samplers under one Thread Group

  2. JMeter Variables are local to the thread (virtual user) so you won't be able to access the variable value in the different thread and thread group

  3. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting

  4. There is no need to go for scripting at all, just add a HTTP Header Manager as a child of the workspace request (see JMeter Scoping Rules - The Ultimate Guide article to learn more about the scope of JMeter Test Elements) and define the token there. Suggested Test Plan Structure:

    enter image description here

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Just finished figuring this out. Don't know if it's the best solution. Looked like there were other options.

In my case the first thread group was reading a list of users and passwords from a csv file.

I did it by writing a csv file in the first thread using "JSR223 PostProcessor" after each authentication API call.

Then I read the newly created csv using the "CSV Data Set Config" in the second thread.

Groovy script follows:

import org.apache.jmeter.services.FileServer

log.info("*************************************") 
def userId = vars.get("user_id") //JMeter var from parsing auth request
def authToken= vars.get("auth_token") 
def configDir = vars.get("config_dir") 

log.info("userId:" + userId) 
log.info("authToken:" + authToken) 

def outputFilePath = configDir + "/userToken.csv"

File outputFile = new File(outputFilePath)

//check if the file exists
if (!outputFile.exists()) {
  log.info("File " + outputFilePath + "does not exist")
  log.info("Creating a new file") 
  outputFile << "userId,authToken\n"
}   
    
outputFile << userId + "," + authToken + "\n"

Test Plan on JMeter

Richie
  • 11
  • 2