我正试图在BeanShell后处理器中使用jmeter的时间函数获取今天的日期,格式为“$ {__ time(yyyy-MM-dd)}”。但在执行Beanshell后处理器后,结果显示为“1996”。基本上,“时间”功能通过减去像“2018-03-19”= 1996这样的值来显示结果。 请帮我解决这个问题,以便获得当前的日期和时间。 Beanshell代码如下
import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;
String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
String testPlanFileDir = FilenameUtils.getFullPathNoEndSeparator(testPlanFile);
vars.put("testPlanFileDir", testPlanFileDir);
//create the file in test plan file dir and print current time and Date
FileOutputStream f = new FileOutputStream(testPlanFileDir+"/CurrentDate.txt", true);
PrintStream p = new PrintStream(f);
this.interpreter.setOut(p);
//current date and time;
print("Current date is:"+${__time(yyyy-MM-dd)});
f.close();
答案 0 :(得分:2)
答案 1 :(得分:0)
您应该根据JMeter Best Practices移动到JSR223后处理器:
从JMeter 3.1开始,我们建议从BeanShell切换到JSR223测试元素
在此之前,您可以通过引用函数调用
来修复它 print("Current date is:"+"${__time(yyyy-MM-dd)})";
此修复程序会将函数的返回值视为字符串。 当前它将其视为数字减法:2018-3-19 = 1996然后将其转换为字符串
答案 2 :(得分:0)
所以:
将以下代码放入"脚本"面积:
String testPlanDir = org.apache.jmeter.services.FileServer.getFileServer().getBaseDir()
File currentDate = new File(testPlanDir + File.separator + "CurrentDate.txt")
currentDate << new Date().format("yyyy-MM-dd")
答案 3 :(得分:0)
尝试使用 JSR223 PostProcessor 和语言 Groovy 并将其放入脚本区域:
def a = new Date().format('yyyy-MM-dd')
new File('Example.txt').withWriter('utf-8') {
writer -> writer.writeLine 'Current date is : ' + a.toString() }
(适用于 JMeter 4.0 )