Bug 64039 - vars.put did not work after invoke jmeter beanshell preprocessor related api
Summary: vars.put did not work after invoke jmeter beanshell preprocessor related api
Status: REOPENED
Alias: None
Product: JMeter
Classification: Unclassified
Component: Main (show other bugs)
Version: 5.2.1
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: JMeter issues mailing list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-12-30 01:00 UTC by wayne.wu
Modified: 2021-05-14 10:48 UTC (History)
1 user (show)



Attachments
jmeter file and screen shot (256.15 KB, application/zip)
2019-12-30 01:00 UTC, wayne.wu
Details

Note You need to log in before you can comment on or make changes to this bug.
Description wayne.wu 2019-12-30 01:00:38 UTC
Created attachment 36938 [details]
jmeter file and screen shot

my test need to use jmeter beanshell preprocessor module, because i need to calculuate sha1 encryption for api sign variable in http request.
following is beanshell preprocessor source code
=============
import java.util.Date;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Collection;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.testelement.property.CollectionProperty;

import org.apache.commons.codec.digest.DigestUtils;


Date date = new Date();
long time = (date.getTime());

String s = String.valueOf(time);

vars.put("timestamp1",s);




Arguments args = sampler.getArguments(); 
log.info(String.valueOf(args.getArgumentCount()));
Map reqMap = args.getArgumentsAsMap();

reqMap.put("sign_key","123");
reqMap.put("client_id","456");

ArrayList arrlist=new ArrayList();

for(String key:reqMap.keySet()){
    log.info( "key: "+ key + " and value: "+ reqMap.get(key));
    if(key.equals("sign") || key.equals("access_token"))
    {
    	    continue;
    }
    arrlist.add(key);
}

Collections.sort(arrlist);
String sign="";
for(int i=0;i<arrlist.size();i++) 
{
    String key=(String)arrlist.get(i);
    sign+=key;
    sign+=(String)reqMap.get(key);
}

log.info(sign);

String final_sign=DigestUtils.sha1Hex(sign);


log.info(final_sign);
vars.put("sign1",final_sign);
=============

in jmeter http request paramter form, there was a parameter named 'sign', with value of ${sign1}, my purpose is to change this value by calculated value from var.pug("sign1" , final_sign)

but it didn't work, i found everytime, in beanshell preprocessor, invoke jemter related api will make final statement "vars.put("sign1",final_sign)" fail to put value.
Comment 1 Felix Schumacher 2019-12-30 10:20:00 UTC
The call to sampler.getArguments().getArgumentsAsMap() is causing the described behaviour.

A minimal example for this would be a preprocessor like:

sampler.getArguments().getArgumentsAsMap();
vars.put("sign1", "something");

With a http sampler that uses ${sign1} in its parameter map.
Comment 2 Philippe Mouawad 2020-02-15 11:20:56 UTC
Closing as no feedback
Comment 3 Felix Schumacher 2020-02-15 12:56:55 UTC
As my comment seemed a bit short on details:

If you use a http sampler with a variable (${sign1} for example) and you use a script to fill it in a pre processor, than calling  "sampler.getArguments().getArgumentsAsMap();" will fill the samplers arguments with the current values of the variables. In the minimal example the value for sign1 is not yet set and therefore JMeter will set "${sign1}" as a placeholder.

Setting the variable sign1 with a value after that call, will not replace the already set parameters and thus it will stay on the the string "${sign1}".

The OP wanted to see in all parameters of the http sampler, whether a special value exists and has to be filled. He called getArgumentesAsMap to do so. That triggers the behaviour.
Comment 4 didi 2021-05-14 10:48:32 UTC
You can check whether there is a PreProcessor such as RegEx User Parameters under your Sampler in addition to the Beanshell PreProcessor, and whether the location of other PreProcessors is on the Beanshell PreProcessor, which will affect the assignment of vars.put()