Monday, January 20, 2020

Jmeter response header parsing using BeanShell scripting

Response is :Set-Cookie: ADCSESSID=e11dd94e-ee22-4313-84bc-df5040acf101; Domain=.auction.com; httponly; Path=/

I need only ADCSESSID value. So
Regular expression is : "ADCSESSID=(.+?);"

Check this using java regualr expression tester : http://ocpsoft.org/tutorials/regular-expressions/java-visual-regex-tester/#!;t=User%20clientId%3D23421.%20Some%20more%20text%20clientId%3D33432.%20This%20clientNum%3D100&r=(clientId%3D)(%5Cd%2B)&x=***masked***

Reference doc:
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
String jsonString = prev.getResponseHeaders();
//System.out.println(jsonString);
SampleResult.setResponseData( jsonString );
text = prev.getResponseHeaders();
patternText = "ADCSESSID=(.+?);";
//System.out.println(patternText);
p = java.util.regex.Pattern.compile(patternText, java.util.regex.Pattern.DOTALL);
System.out.println("p value is "+p);
m = p.matcher(text);
System.out.println("matcher m value is " +m);
if (m.find()) {
    vars.put("beanShellExecutionId", m.group(1));
} else {
    vars.put("beanShellExecutionId", "NOT_FOUND");
}

System.out.println(vars.get("beanShellExecutionId"));

print date in MM/dd/yyyy hh:mm:ss a

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

SimpleDateFormat formatter, FORMATTER;
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
String oldDate = "2008-03-14T11:53:31.200+05:00";
Date date = formatter.parse(oldDate.substring(0, 26) + oldDate.substring(27, 29));
FORMATTER = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
System.out.println("OldDate-->"+oldDate);
log.info("NewDate-->"+FORMATTER.format(date));
vars.put("myvar", FORMATTER.format(date)); 

Share variables between thread groups

I was not able to do this with variables (since those are local to individual threads). However, I was able to solve this problem with properties!
Again, my first ThreadGroup does all of the set up, and I need some information from that work to be available to each of the threads in the second ThreadGroup. I have a BeanShell Assertion in the first ThreadGroup with the following:
${__setProperty(storeid, ${storeid})};
The ${storeid} was extracted with an XPath Extractor. The BeanShell Assertion does other stuff, like checking that storeid was returned from the previous call, etc.
Anyway, in the second ThreadGroup, I can use the value of the "storeid" property in Samplers with the following:
${__property(storeid)}
Works like a charm!