我写了一个新的JSR223 Listener来将测试结果写入extentreports html报告。这很好用,但可以改进。我只是不确定改善它的最佳方法。我看到的一个具体问题是仪表板的Time Taken值。它显示了上次采样器运行的开始/结束时间。它应该显示第一个采样器的时间作为开始时间和结束采样器的时间作为结束时间,并且应该从这两个日期时间获得Time Taken值。你能看一下我的听众脚本并分享你可能有的建议吗?
测试计划设置:在线程组内,我有登录的HTTP请求采样器,执行一个操作并注销。在线程组的根目录下,我使用以下代码在BeanShell Assertion中包含此代码:
//request data
String requestData = new String(prev.SamplerData);
//String requestData = new String(requestData);
props.put("propRequestData", requestData);
//response data
String respData = new String(prev.ResponseData);
//String respData = new String(prev.getResponseDataAsString());
props.put("propResponse", respData);
//response code
String respCode = new String(prev.ResponseCode);
props.put("propRespCode",respCode);
//response message
String respMessage = new String(prev.ResponseMessage);
props.put("propRespMessage",respMessage);
在我的测试计划的根本上,我有这个JSR223监听器代码:
import com.aventstack.extentreports.*;
import com.aventstack.extentreports.reporter.*;
import com.aventstack.extentreports.markuputils.*;
ExtentHtmlReporter htmlReporter;
ExtentReports extent;
ExtentTest test;
// create the HtmlReporter
htmlReporter = new ExtentHtmlReporter("C:/AUTO_Results/Results_${testApp}_${reportDate}_${currentTime}_${testenv}.html");
//configure report
htmlReporter.config().setCreateOfflineReport(true);
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setDocumentTitle("${testApp} Results");
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName("${testApp} Results ${reportDate}_${currentTime}_${testenv}");
htmlReporter.setAppendExisting(true);
// create ExtentReports
extent = new ExtentReports();
// attach reporter to ExtentReports
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
// Show Env section and set data on dashboard
extent.setSystemInfo("Tool","JMeter");
extent.setSystemInfo("Test Env","${testenv}");
extent.setSystemInfo("Test Date","${reportDate}");
extent.setSystemInfo("Test Time","${currentTime}");
//stringify test info
String threadName = sampler.getThreadName();
String samplerName = sampler.getName();
String requestData = props.get("propRequestData");
String respCode = props.get("propRespCode");
String respMessage = props.get("propRespMessage");
String responseData = props.get("propResponse");
// create test
test = extent.createTest(threadName+" - "+samplerName);
//test.assignCategory("API Testing");
// analyze sampler result
if (vars.get("JMeterThread.last_sample_ok") == "false") {
log.error("FAILED: "+samplerName);
print("FAILED: "+samplerName);
test.fail(MarkupHelper.createLabel("FAILED: "+sampler.getName(),ExtentColor.RED));
} else if (vars.get("JMeterThread.last_sample_ok") == "true") {
if(responseData.contains("@error")) {
log.info("FAILED: "+sampler.getName());
print("FAILED: "+sampler.getName());
test.fail(MarkupHelper.createLabel("FAILED: "+sampler.getName(),ExtentColor.RED));
} else if (responseData.contains("{")) {
log.info("Passed: "+sampler.getName());
print("Passed: "+sampler.getName());
test.pass(MarkupHelper.createLabel("Passed: "+sampler.getName(),ExtentColor.GREEN));
}
} else {
log.error("Something is really wonky");
print("Something is really wonky");
test.fatal("Something is really wonky");
}
//info messages
test.info("RequestData: "+requestData);
test.info("Response Code and Message: "+respCode+" "+respMessage);
test.info("ResponseData: "+responseData);
//playing around
//markupify json into code blocks
//Markup m = MarkupHelper.createCodeBlock(requestData);
//test.info(MarkupHelper.createModal("Modal text"));
//Markup mCard = MarkupHelper.createCard(requestData, ExtentColor.CYAN);
// test.info("Request "+m);
// test.info(mCard);
// test.info("Response Data: "+MarkupHelper.createCodeBlock(props.get("propResponse")));
// test.info("ASSERTION MESSAGE: "+props.get("propAssertion"));
// end the reporting and save the file
extent.flush();
您在JSR223侦听器中列出的$ {variables}在用户定义的变量元素中定义。我在用: JMeter的-3.2 我的lib文件夹中的extentreports-pro-3.0.5.jar
答案 0 :(得分:0)
您的结束时间很好(最终采样器),您需要的是第一个采样器的开始
我认为您可以使用START predefined property:
START属性也会复制到具有相同名称的变量。
- START.MS - JMeter开始时间(以毫秒为单位)
- START.YMD - JMeter开始时间为yyyyMMdd
- START.HMS - JMeter开始时间为HHmmss
- TESTSTART.MS - 以毫秒为单位的测试开始时间
将JSR223 PostProcessor添加到第一个采样器并将变量添加到变量的另一个选项:
log.info("start time is " + prev.getStartTime() );
vars.put("startTimeFirstSample", "" + prev.getStartTime() );
答案 1 :(得分:0)
我最终在测试期间采用了以下解决方法。 1.在设置线程组中,我添加了一个带有此代码的JSR223采样器:
log.info("--------------Initialize");
import java.time.Duration;
import java.time.Instant;
Instant myStart = Instant.now();
props.put("varmyStart",myStart);
log.info("Test Start time: ---- "+props.get("varmyStart"));
//response
props.put("propResponse","Test Start time { "+props.get("varmyStart")+" }");
SampleResult.setResponseData(props.get("propResponse"));
在拆解线程组中,我添加了一个带有此代码的JSR223采样器:
log.info("---------------End Test JSR223Sampler");
//props.put("varEndTest","Yes");
import java.time.Duration;
import java.time.Instant;
Instant myEnd = Instant.now();
props.put("varmyEnd",myEnd);
log.info("varmyEnd---- "+props.get("varmyEnd"));
Duration timeElapsed = Duration.between(props.get("varmyStart"),
props.get("varmyEnd"));
//millis
props.put("varTimeTakenMS",timeElapsed.toMillis().toString());
log.info("prop varTimeTaken(MS): --"+props.get("varTimeTakenMS"));
//seconds
props.put("varTimeTakenSEC",timeElapsed.getSeconds());
log.info("prop varTimeTaken(SEC): --"+props.get("varTimeTakenSEC"));
//minutes
props.put("varTimeTakenMINS",timeElapsed.toMinutes().toString());
log.info("prop varTimeTaken(MINS): --"+props.get("varTimeTakenMINS"));
//response
props.put("propResponse","Test End time { "+props.get("varmyEnd")+" }.
Test Duration: "+props.get("varTimeTakenMINS")+" Minutes");
SampleResult.setResponseData(props.get("propResponse"));
在监听器代码中,在// Show Env部分下,并在仪表板上设置数据,我添加了这些行:
//extent.setSystemInfo("Actual Test Duration(ms)",props.get("varTimeTakenMS")+" milliseconds");
extent.setSystemInfo("Actual Test Duration(seconds)",props.get("varTimeTakenSEC")+" seconds");
extent.setSystemInfo("Actual Test Duration(mins)",props.get("varTimeTakenMINS")+" minutes");
它并不漂亮,但却能满足我的需求。