简体   繁体   中英

In JMeter java api how to set POST Form values in a PreProcessor sampler?

In a PreProcessor I am writing I can successfully update GET query string via sampler.

However if I use the same approach with POST, while I can list the FORM fields via getArguments(), the value does not get set.

  Arguments arguments = sampler.getArguments();
  for (int i=0; i<arguments.getArgumentCount(); i++) {
      Argument argument = arguments.getArgument(i);
      if (argument.getName().equals("page_submission_id")) {
          String newVal = "8743718386872";                         
          argument.setValue(newVal);
          System.out.println("Setting arg["+argument.getName()+"] to["+newVal+"]");
      }
  }
  sampler.setArguments(arguments);  
  printArgs(arguments);  

The output from this shows Arguments values are unchanged.

Setting arg[page_submission_id] to[8743718386872]
Arguments After
   ...
   Found arg[page_submission_id] is[XXXXXXXXXXXXX]
   ...

Having dug into the jmeter code a bit further, there is a "runningVersion" attribute of an Attribute object which (via isRunningVersion()) is set true.

I have tried a few ways to get round this:

  • force runningVersion to false - then values are set but a GET message is sent
  • create a new Arguments object and add new Argument entries to it with values - this does not change the values

Can anyone point out the official way to set POST FORM field values before they get sent?

Thanks

Well, you assigning a new value to an argument, but I fail to see where you updating sampler's arguments with the argument having the new value.

I'm a strong believer of KISS principle so instead of adding some more lines I would recommend simplifying your script as follows:

import org.apache.jmeter.config.Argument;

sampler.getArguments().removeArgument("page_submission_id");
sampler.addArgument("page_submission_id","8743718386872"); 

Also I hope you're using JSR223 PreProcessor and Groovy language .

I managed to resolve this:

  1. (initially) by cleaning up the Thread Pool, as my initial attempts had included a number of things like "Regular Expression Extractors" and "User defined variables". Once those were removed the approach I was using successfully changed the argument values, and
  2. (when deeper in to my setup the problem came back) by adding the creation of a new Argments object and inserting (in the same order) new Argument objects with the value set as I require. Then setting the sampler to use that new Arguments object.

     Arguments newArgs = new Arguments(); Arguments arguments = sampler.getArguments(); for (int i=0; i<arguments.getArgumentCount(); i++) { Argument argument = arguments.getArgument(i); HTTPArgument newArg = new HTTPArgument(); newArg.setName(arguments.getName()); if (arguments.getName().equals("field_to_replace")) { newArg.setValue("new value"); } else { newArg.setValue(arguments.getValue()); } newArgs.addArgument(newArg); } sampler.setArguments(newArgs); 

My take is that this down to the "if (isRunningVersion())" test within setProperty() used by "Argument.setValue()" which I'm tripping over.

While this appears to work (for my test cases so far) I appreciate that overriding this may not be the correct formal approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM