how to extract value from request in Jmeter
Asked Answered
D

6

6

Hi I am passing an email which is a time function like below

email = ${__time(MMddyy)}_${__time(HMS)}@yopmail.com

The value of this function changes eveytime I call the variable email. I would like to store this value that is generated from this function into a variable and use that in other requests.

So currently I am getting two different emails in two different http requests since there is some time lag between my two http requests.

what I would like to do is .. store the email that is being sent in first http request by extracting the value from the request and pass it in the second http request.

POST data:
email=062915_160738%40yopmail.com

I know the way to extract from html response.. but is there any way to extract from request in jmeter?

If so can someone pls tell me how to achieve this?

thank you

Daveta answered 29/6, 2015 at 23:16 Comment(0)
R
6
  1. Add a Beanshell PostProcessor as a child of the request which sends that POST request
  2. Put the following code into the PostProcessor's "Script" area

    import org.apache.jmeter.config.Argument;
    import org.apache.jmeter.config.Arguments;
    
    Arguments argz = ctx.getCurrentSampler().getArguments();
    for (int i = 0; i < argz.getArgumentCount(); i++) {
        Argument arg = argz.getArgument(i);
        if (arg.getName().equals("email")) {
            vars.put("EMAIL", arg.getValue());
            break;
        }
    }
    
  3. Refer generated value as ${EMAIL} where required.

Clarification:

  • above code will extract the value of email request parameter (if any) and store it to EMAIL JMeter Variable
  • ctx - shorthand to JMeterContext class instance
  • vars = shorthand to JMeterVariables class instance
  • Arguments and Argument - you can figure that out from JMeterContext JavaDoc

See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in JMeter.

Rocher answered 30/6, 2015 at 15:9 Comment(1)
This is the only thing that worked for me to extract a dynamic value which auto generates by application as part of the request. did try may other ways. could be bit heavy on the working, but have to stick with this till a better solution is found. Thanks a lot.Topsyturvy
F
1

Instead of the entire email, you can store the timestamp value in a variable and then use this timestamp variable to create email anywhere you want. This way you will can have same email every where.

Furthermore answered 30/6, 2015 at 7:30 Comment(0)
K
1

Add a Beanshell PostProcessor & Add following script:

import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;

Arguments argz = ctx.getCurrentSampler().getArguments();
for (int i = 0; i < argz.getArgumentCount(); i++) {
   Argument arg = argz.getArgument(i);
    String req_body = arg.getValue();
    vars.put("req_Json",req_body);
 }

here we get the output in json format:

 ${req_Json}=
"email":"062915_160738%40yopmail.com",
"name":"abc xyz"

Now using jp@gc Json Path Extractor extract the value of email

Json expression = $['email']

and store the value in email_value_extacted

now use the variable ${email_value_extacted} anywhere you want to use. finally,

${email_value_extacted} = 062915_160738%40yopmail.com
Kyanize answered 18/4, 2021 at 14:17 Comment(0)
C
0

Is it HTTP Sampler? If so, just put into beanshell postprocessor:

String prevQuery = prev.getQueryString(); //your request text
System.out.println(prevQuery );

Also works for any samplers:

String prevQuery  = prev.getSamplerData();
Culler answered 30/6, 2015 at 13:4 Comment(0)
N
0

You can use Regular Expression extractor to extract the e-mail address from the request URL.

Add Regular Expression Extractor as a child of sampler which sends the post request. In the Regular Expression Extractor select URL in Response Filed to check instead of Body.

You should be able to extract e-mail id from the request in this way.

Novelist answered 30/6, 2015 at 19:45 Comment(0)
F
0

Add a Beanshell PostProcessor & Add the following script:

import org.apache.jmeter.config.Arguments;

Arguments argz = ctx.getCurrentSampler().getArguments();
for (int i = 0; i < argz.getArgumentCount(); i++) {
   Argument arg = argz.getArgument(i);
    String req_body = arg.getValue();
    vars.put("req_Json",requestBody);
 }

You'll get json like this:

{
"email":"062915_160738%40yopmail.com",
"name":"abc xyz"
}

Now add jp@gc - Add Dummy Subresult and add ${req_Json} in the Response Data field.

Now add JSON Extractor & select Sub-samples only radio button & add $.emailAddress in JSON Path expression & provide name of variable & using newly created variable you can able to access email address.

Fireback answered 4/7, 2023 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.