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

时间:2019-03-14 12:52:23

标签: jmeter beanshell

在我的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();

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

1 个答案:

答案 0 :(得分:1)

您需要创建带有附加标志的FileOutputStream

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

追加-如果为true,则字节将被写入文件的末尾而不是开头