0

Is there a way to store a value extracted from the response of a test. I can extract the value using the regular expression extractor, but I need to store that value in a variable and use it in the next run.

I can do that in POSTMAN using the following script:

var jsonData = JSON.parse(responseBody); postman.setEnvironmentVariable("accessToken", jsonData.accessToken);

Can someone help me out with this?

I tried using the regular expression extractor.

  • Can you show the example for `extract the value using the regular expression extractor`? The token example is in [here](https://stackoverflow.com/questions/73606410/which-api-should-use-to-get-user-permissions-list-by-userid-with-keycloak-admin/73607156#73607156) – Bench Vue Aug 22 '23 at 11:36

1 Answers1

0

JSON is not a regular language hence using it for parsing the response is not a best idea, there are following alternatives in JMeter:

If you extract the value it will be stored into JMeter Variable and it will be accessible for Samplers which will be executed after the PostProcessor.

There is one limitation: JMeter Variables are local to the thread so other virtual users cannot "see" this variable value and when the virtual user is being shut down the variable gets lost.

If by "next run" you mean next test execution, i.e. you start JMeter from zero once again the only way to have this variable stored is writing it into a file. It can be done using the aforementioned JSR223 PostProcessor or a plugin like Flexible File Writer

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks for the answer. Will try this out. – Krutharth Patel Aug 24 '23 at 06:25
  • I used the Beanshell PostProcessor for writing the response in the file and later used that file to read the value and it worked. – Krutharth Patel Aug 24 '23 at 07:43
  • It might not be the best idea, [since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting](https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting) because [using Beanshell is some form of a performance anti-pattern](https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting). Also if you run your test with > 1 user you might run into the situation when 2 or more threads are writing into the same file at the same time resulting in data corruption or loss due to race conditions – Dmitri T Aug 24 '23 at 08:11