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

问题描述 投票:1回答:1

在我的JMeter脚本中有两个步骤。登录和第二次获取订单,在登录线程组内我使用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();

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

jmeter beanshell
1个回答
1
投票

您需要使用append标志创建FileOutputStream

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

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

© www.soinside.com 2019 - 2024. All rights reserved.