How to GET and SET Variables in JMeter

Jmeter is very useful for testing web pages, as it helps us scrape a web page and grab stuffs you want in the web page to be used somewhere else in the script.

Classic example is when you want to grab a token, or a URL which you would like to call after you call the main page for instance.

My favourate way to do this is by  using the Regular Expression Post processor, which allows me to provide a regular expression with the following options

1. Reference Name : This will be the name of the variable where the value extracted form the web page based on the regular expression will be stored and can be referenced elsewhere in the script

2. Regular Expression : this is the regular expression for the line to be matched

3. Template : The format to be specified is $<group_number>$ , which specifies which group will be picked up and stored in the reference name/variable. The grouping is similar to the one in Java Regular Expression, – example : $2$ would result in the second of the groups to be picked up form a regular exmpression – (.*)details=(.*)src=(.*) , which is the value of what ever follows “details=” in the regular expression

4. Match No ( 0 for Random) : 1 would specify the first occurance of a line that matches the regular expression

5. Default Value :

Now once you have a Reference name of a variable , you would use it in the following way

1. You can refer to it form other “SAMPLERS” by using the following format ${REF_NAME}

2. Assign it to a String variable in “BEAN SHELL” – String x = vars.get(“REF_NAME”);

Now if you would like to re-assign value for the REFFRENCE NAME after modification in Bean Shell, you would do it as follow

vars.put(“REF_NAME”,”some_value”);

About these ads

5 Comments

Filed under JMeter

5 responses to “How to GET and SET Variables in JMeter

  1. Ray

    I have tried the following in beanshell without success:
    String STR = vars.get(${VALIDATEXML});
    vars.put(${RESULT},${STR});

    String STR = vars.get(VALIDATEXML);
    vars.put(RESULT,STR);

    String STR = vars.get(“VALIDATEXML”);
    vars.put(“RESULT”,”STR”);

    String STR = vars.get(“VALIDATEXML”);
    vars.put(“STR”,”RESULT”);
    and

    String STR = vars.get(“${VALIDATEXML}”);
    vars.put(“${RESULT}”,”${STR}”);

    Should one of the work. Or, am I missing something still?

  2. ssujesh

    String STR = vars.get(“VALIDATEXML”); where VALIDATEXML is a variable that hold the value u need to get
    vars.put(“RESULT”,STR); now u can reference ${RESULT} anywhere in your JMeter script it will hold the value that was stored in STR

  3. Jmeter Guy

    How to set Value in User-defined variable using BeanShell Preprocessor… I want to set datetime value using beanshell to my user-defined variable …

    Thanks!

  4. Bunny

    Hi!
    I need to concatenate values of a loop,
    for Instance loop generates 10 values:A1,A2…..,A10
    I need to generate a single string after concatenating all these values something like A1A2A3…A10..How to do this concatenation ? Can it be done with vars.get and vars.put ?

  5. Heya are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get
    started and create my own. Do you need any coding expertise to make your own blog?
    Any help would be greatly appreciated!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s