Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract value from request in Jmeter

Tags:

jmeter

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

like image 552
mo0206 Avatar asked Jun 29 '15 23:06

mo0206


1 Answers

  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.

like image 178
Dmitri T Avatar answered Oct 04 '22 04:10

Dmitri T