简体   繁体   English

在JMeter Java API中,如何在PreProcessor采样器中设置POST表单值?

[英]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. 我正在编写一个预处理器,可以通过采样器成功更新GET查询字符串。

However if I use the same approach with POST, while I can list the FORM fields via getArguments(), the value does not get set. 但是,如果我对POST使用相同的方法,尽管可以通过getArguments()列出FORM字段,但不会设置该值。

  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. 此输出显示Arguments值不变。

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. 在进一步深入jmeter代码之后,有一个Attribute对象的“ runningVersion”属性(通过isRunningVersion())设置为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 强制runningVersion为false-然后设置值,但发送GET消息
  • create a new Arguments object and add new Argument entries to it with values - this does not change the values 创建一个新的Arguments对象,并向其添加带有值的新Argument条目-这不会更改值

Can anyone point out the official way to set POST FORM field values before they get sent? 谁能指出在发送前设置POST FORM字段值的官方方法?

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: 我坚信KISS原则,因此建议您像下面这样简化脚本,而不是添加更多内容:

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 . 另外,我希望您使用的是JSR223 PreProcessor和Groovy语言

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. (在深入了解我的设置时,问题又回来了),方法是添加一个新的Argments对象的创建并插入(以相同的顺序)具有我所需要的值的新Argument对象。 Then setting the sampler to use that new Arguments object. 然后将采样器设置为使用该新Arguments对象。

     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. 我的看法是,这要归结到我跳过的“ Argument.setValue()”所使用的setProperty()中的“ if(isRunningVersion())”测试。

While this appears to work (for my test cases so far) I appreciate that overriding this may not be the correct formal approach. 尽管这似乎行得通(到目前为止,对于我的测试用例),但我很欣赏覆盖它可能不是正确的正式方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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