在我的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();
我是否需要每次都声明上面的代码片段写入日志文件?
您需要使用append标志创建FileOutputStream:
FileOutputStream logfile = new FileOutputStream(vars.get("LogFile"), true);
append - 如果为true,则将字节写入文件的末尾而不是开头