[英]In JMeter java api how to set POST Form values in a PreProcessor sampler?
[英]JMeter - Variable set in preprocessor is not available to sampler
我有一個相對簡單的JMeter測試計划設置,如下所示: https : //imgur.com/c8BIzBB
其中的相關部分是BeanShell預處理器(顯示為Setup元素數據 )及其與HTTP請求采樣器的關系(顯示為POST / elements )。 兩者都在循環控制器內部(顯示為Do幾次 )。
預處理程序獲取存儲在bsh.shared
對象上的數據數組,並隨機選擇一項。 然后,它設置一個名為elementTypeId
的變量。
當我運行此測試時, elementTypeId
會正確記錄(因此我假設已設置)。 但是,第一次,變量設置不正確,仍然顯示為${elementTypeId}
。 似乎設置了其他樣本,但使用的是第n-1個值。
第一個失敗的示例如下所示: https : //imgur.com/Gj2YAje
最終示例(和記錄的值)顯示在此處: https : //imgur.com/OW5HSsS
設置元素數據-BeanShell預處理程序代碼:
import java.util.Random;
import com.eclipsesource.json.*;
Random rand = new Random();
int idx = rand.nextInt(bsh.shared.elementTypes.size());
JsonValue elementType = bsh.shared.elementTypes.get(idx);
String elementTypeId = String.valueOf(elementType.get("id").asInt());
log.info(elementTypeId);
vars.put("elementTypeId", elementTypeId);
在我看來,在預處理程序設置變量之前,采樣器正在觸發-這似乎與應該發生的情況相反。
當我禁用SetQueryParams PreProcessor時,似乎變量設置正確(盡管請求失敗,因為它需要添加查詢參數)。
SetQueryParams預處理器:
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
if (sampler instanceof HTTPSamplerBase &&
sampler.getMethod().equalsIgnoreCase("post")) {
// add query parameter to url
var customPath = sampler.getPath() + '?' + sampler.getQueryString();
sampler.setPath(customPath);
// remove query parameter from body
arguments = sampler.getArguments();
while (arguments.getArgumentCount() > 1) {
arguments.removeArgument(arguments.getArgumentCount() - 1);
}
sampler.setArguments(arguments);
}
是否可以將兩個預處理器都應用於POST請求?
在控制台中檢查是否引發了異常。 嘗試解決
如果沒有任何效果,請復制您的腳本,刪除Beanshell預處理程序,然后再次添加BeanShell並粘貼您的腳本。 這可能很愚蠢,但對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.