fix(接口自动化): 调试结果提取变量显示丢失问题修复 #1005450

--bug=1005450 --user=赵勇 【接口自动化】响应结... https://www.tapd.cn/55049933/s/1029850
This commit is contained in:
fit2-zhao 2021-07-27 18:34:29 +08:00 committed by fit2-zhao
parent a6ed88b6d5
commit 999edfd3fb
3 changed files with 31 additions and 2 deletions

View File

@ -21,14 +21,17 @@ import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.service.MsResultService; import io.metersphere.api.service.MsResultService;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.engine.util.NoThreadClone; import org.apache.jmeter.engine.util.NoThreadClone;
import org.apache.jmeter.reporters.AbstractListenerElement; import org.apache.jmeter.reporters.AbstractListenerElement;
import org.apache.jmeter.samplers.*; import org.apache.jmeter.samplers.*;
import org.apache.jmeter.testelement.TestStateListener; import org.apache.jmeter.testelement.TestStateListener;
import org.apache.jmeter.testelement.property.BooleanProperty; import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.threads.JMeterVariables;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map;
/** /**
* 实时结果监听 * 实时结果监听
@ -124,6 +127,16 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
@Override @Override
public void sampleOccurred(SampleEvent event) { public void sampleOccurred(SampleEvent event) {
SampleResult result = event.getResult(); SampleResult result = event.getResult();
JMeterVariables variables = JMeterVars.get(result.hashCode());
if (variables != null && CollectionUtils.isNotEmpty(variables.entrySet())) {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Object> entry : variables.entrySet()) {
builder.append(entry.getKey()).append("").append(entry.getValue()).append("\n");
}
if (StringUtils.isNotEmpty(builder)) {
result.setExtVars(builder.toString());
}
}
if (isSampleWanted(result.isSuccessful()) && !StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) { if (isSampleWanted(result.isSuccessful()) && !StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) {
msResultService.setCache(this.getName(), result); msResultService.setCache(this.getName(), result);
} }

View File

@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.AssertionResult; import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterVariables;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import sun.security.util.Cache; import sun.security.util.Cache;
@ -188,9 +189,13 @@ public class MsResultService {
responseResult.setResponseSize(result.getResponseData().length); responseResult.setResponseSize(result.getResponseData().length);
responseResult.setResponseTime(result.getTime()); responseResult.setResponseTime(result.getTime());
responseResult.setResponseMessage(result.getResponseMessage()); responseResult.setResponseMessage(result.getResponseMessage());
if (JMeterVars.get(result.hashCode()) != null && CollectionUtils.isNotEmpty(JMeterVars.get(result.hashCode()).entrySet())) { JMeterVariables variables = JMeterVars.get(result.hashCode());
if (StringUtils.isNotEmpty(result.getExtVars())) {
responseResult.setVars(result.getExtVars());
JMeterVars.remove(result.hashCode());
} else if (variables != null && CollectionUtils.isNotEmpty(variables.entrySet())) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Object> entry : JMeterVars.get(result.hashCode()).entrySet()) { for (Map.Entry<String, Object> entry : variables.entrySet()) {
builder.append(entry.getKey()).append("").append(entry.getValue()).append("\n"); builder.append(entry.getKey()).append("").append(entry.getValue()).append("\n");
} }
if (StringUtils.isNotEmpty(builder)) { if (StringUtils.isNotEmpty(builder)) {
@ -198,6 +203,7 @@ public class MsResultService {
} }
JMeterVars.remove(result.hashCode()); JMeterVars.remove(result.hashCode());
} }
for (AssertionResult assertionResult : result.getAssertionResults()) { for (AssertionResult assertionResult : result.getAssertionResults()) {
ResponseAssertionResult responseAssertionResult = getResponseAssertionResult(assertionResult); ResponseAssertionResult responseAssertionResult = getResponseAssertionResult(assertionResult);
if (responseAssertionResult.isPass()) { if (responseAssertionResult.isPass()) {

View File

@ -170,6 +170,8 @@ public class SampleResult implements Serializable, Cloneable, Searchable {
private String resourceId; private String resourceId;
private String extVars;
public String getSamplerId() { public String getSamplerId() {
return this.samplerId; return this.samplerId;
} }
@ -185,6 +187,14 @@ public class SampleResult implements Serializable, Cloneable, Searchable {
return this.scenario; return this.scenario;
} }
public void setExtVars(String vars) {
this.extVars = vars;
}
public String getExtVars() {
return this.extVars;
}
/** /**
* 定制自定义添加 ================= * 定制自定义添加 =================
*/ */