Jmeter beanshell 写文件

Life is short , play more!
本文来自lihao's Blog,转载请注明。

Jmeter beanshell 写文件

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;

String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
String FILENAME = vars.get("TestLogFile");

//log.info(FILENAME);

 void  writeToFile(String fileName , String content){

	try {

		BufferedWriter bw = new BufferedWriter(new FileWriter(fileName,true)) ;

			//String content = "This is the content to write into file\n";

			bw.write(content+"\n");

			// no need to close it.
			//bw.close();

			//log.info(content+"\n");

			bw.close();

		} catch (IOException e) {

			e.printStackTrace();

		}
}


	writeToFile(FILENAME,"================== "+timeStamp+" ==="+sampler.getName()+"=====================");
    // writeToFile(FILENAME,"Request Content: "+pre.getSamplerData().replace("/\n/g", " ; "));
     if( sampler.getClass().toString().contains("JSR223Sampler") ){

	//不打印任何数据
	}
	else {
     writeToFile(FILENAME,"Request Content: "+sampleResult.getSamplerData().replace("/\r\n/g", "; ").replace("/\n/g", "; ") );
     }
     writeToFile(FILENAME,"Response Data: "+sampleResult.getResponseDataAsString().replace("/\r\n/g", "; ").replace("/\n/g", "; ")  );
     writeToFile(FILENAME,"Response Message: "+sampleResult.getResponseMessage());
     
	writeToFile(FILENAME,"*****************************************");

	if(!sampleResult.isSuccessful()){
			writeToFile(FILENAME,"***测试断言结果: !结果错误,请查证!×***");
	}
	else{
			writeToFile(FILENAME,"********测试断言结果: 结果正确。✓ ******");
	}


    writeToFile(FILENAME,"*****************************************");
    writeToFile(FILENAME,"========================================================================");

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注