无法通过 Jmeter 中的 beanshell 断言采样器附加到文件中的下一行

Unable to append to next line in a file through beanshell assertion sampler in Jmeter

我的 JMeter 脚本中有 2 个步骤。登录和第二次获取订单,在登录线程组内,我使用 bean shell 断言来验证一些条件,一旦遇到错误,我就会将错误写入日志文件。所以对于第一个线程,它是正确写入的,但是当它到达下一个断言时,它会覆盖同一个文件而不附加它。

第一个断言:

import org.apache.jmeter.assertions.AssertionResult;
String failureMessage = "";
String successMessage = "";
String ResCode =SampleResult.getResponseDataAsString();
if (!ResCode.contains("Admin") )
    {
    failureMessage = "Got Response Code" + ResCode;
    log.warn("Creation of a new record failed: Response code " + ResCode);
    AssertionResult result = new AssertionResult("Expected Response 200");
    result.setFailure(true);
    result.setFailureMessage(failureMessage);
    prev.addAssertionResult(result);
    prev.setSuccessful(false);
    SampleResult.setStartNextThreadLoop(true);
    }
else {
    successMessage = "Got Response Code" + ResCode;
    log.info("----->"+successMessage);
    FileOutputStream logfile = new FileOutputStream(vars.get("LogFile"));
    PrintStream printtoFile = new PrintStream(logfile); 
    printtoFile.println( successMessage );
    printtoFile.close();
    logfile.close();
    }

第二个断言:

import org.apache.jmeter.assertions.AssertionResult;
String failureOrderMessage = "";
String successOrderMessage = "";
OrderId = vars.get("workOrderId");
log.info("----->"+OrderId);

if (OrderId == null){
    failureOrderMessage = "The Order Id :"+OrderId;
    log.info("----->"+failureOrderMessage);
    FileOutputStream logfile = new FileOutputStream(vars.get("LogFile"));
    PrintStream printtoFile = new PrintStream(logfile); 
    printtoFile.println( failureOrderMessage );
    printtoFile.close();
    logfile.close();
}
else {
    successOrderMessage = "Getting few order id";
    log.info("----->"+successOrderMessage);
 }

上面的断言工作正常,但每次它覆盖日志文件但我想追加它。 第二

FileOutputStream logfile = new FileOutputStream(vars.get("LogFile"));
PrintStream printtoFile = new PrintStream(logfile); 
printtoFile.println( successMessage );
printtoFile.close();
logfile.close();

是否需要每次都声明上面的代码片段来写入日志文件?

您需要使用附加标志创建 FileOutputStream

 FileOutputStream logfile = new FileOutputStream(vars.get("LogFile"), true);

append - if true, then bytes will be written to the end of the file rather than the beginning