Reputation: 5
I get response from my request containing request id and if something goes wrong there is a err code.
I want to write a beanshell script that would look firstly if there is any err code, if not it would wirte request id into the csv file. I am not able to find anything about if else statements in beanshell
if (there is an err code){
write it to the csv file}
else {write request id to csv}
is it possible in beanshell or better to use assertions ?
Problem with java
2016/08/09 13:38:38 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.jmeter.threads.JMeterContextService; import java.io.PrintWrite . . . '' : Command not found: regexMethod( java.lang.String, java.lang.String )
2016/08/09 13:38:38 WARN - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.jmeter.threads.JMeterContextService; import java.io.PrintWrite . . . '' : Command not found: regexMethod( java.lang.String, java.lang.String )
Upvotes: 0
Views: 7009
Reputation: 1200
I think this will do the job... You just need to get regex expressions for your data.
import org.apache.jmeter.threads.JMeterContextService;
import java.io.PrintWriter;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String prevResponse = JMeterContextService.getContext().getPreviousResult().
getResponseDataAsString();
public void writeToFile(String toWrite) {
String path = "/home/username/Desktop/TSO_test_failure.csv";
File file = new File(path);
try {
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.print(toWrite);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String regexMethod (String regex, String text) {
String regResult;
try {
Pattern pat = Pattern.compile(regex);
Matcher mac = pat.matcher(text);
mac.find();
regResult = mac.group(1);
} catch (Exception e) {
e.printStackTrace();
}
return regResult;
}
String result = null;
if (prevResponse.contains("errorCode")) {
String errRegex = "findErrorID"; // change this to meet your needs!
result = regexMethod(errRegex, prevResponse);
} else {
String reqRegex = "findRequestID"; // change this to meet your needs!
result = regexMethod(reqRegex, prevResponse);
}
writeToFile(result);
Upvotes: 1