Jul 11, 2007

JMeter: BeanShell Example

For example, I need to handle number's get from web page which is return by HTTP Request Sampler. It's hard to get the number string directly from ResponseData sometime, just use a "Regular Expression Extractor" and store it to a temp variable, say _tmpAmount, then,

Script:

String amountString = vars.get("_tmpAmount");
int amount = Integer.valueOf(amountString).intValue();

int minAmount = 10;

if (amount > minAmount) {
vars.put("_finalAmount", Integer.toString(minAmount));
} else {
Failure = true;
ErrorMessage = "Must has at least " + minAmount + " in original account!";
SampleResult.setDataType(SampleResult.TEXT);
SampleResult.setResponseMessage(ErrorMessage);
SampleResult.setResponseData(ErrorMessage.getBytes());
SampleResult.setStopThread(true);
}

No comments: