我收到包含请求ID的请求的响应,如果出现问题,则会出现错误的代码。
我想编写一个beanhell脚本,首先查看是否有任何错误的代码,如果不是,它会将请求id输入到csv文件中。我无法在beanshell
中找到if else语句的任何内容if (there is an err code){
write it to the csv file}
else {write request id to csv}
是否可以在beanshell中使用断言?
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 )
答案 0 :(得分:1)
我认为这将完成工作......您只需要获取数据的正则表达式。
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);