Jmeter Beanshell post-processor 中的 else 语句

If else statement in Jmeter Beanshell post-processor

我收到包含请求 ID 的请求的响应,如果出现问题,则会显示错误代码。

我想编写一个 beanshell 脚本,它会首先查看是否有任何错误代码,如果没有,它会将请求 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 ) 

我认为这会完成工作...您只需要为您的数据获取正则表达式。

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);