fix(接口测试): 修复全局单个步骤内的前后置脚本运行两次的问题

--bug=1009994 --user=宋天阳 【接口测试】全局前后置脚本-单个脚本前/后执行的脚本,执行了两次
https://www.tapd.cn/55049933/s/1099137
This commit is contained in:
song-tianyang 2022-01-26 15:41:11 +08:00 committed by xiaomeinvG
parent 085c008571
commit f9f5180ff6
4 changed files with 32 additions and 56 deletions

View File

@ -57,15 +57,20 @@ public class JMeterScriptUtil {
* @param environmentId 环境ID
* @param config 参数配置
*/
public static void setScript(EnvironmentConfig envConfig, HashTree samplerHashTree, String protocal, String environmentId, ParameterConfig config, boolean isAfterPrivateScript) {
public static void setScriptByEnvironmentConfig(EnvironmentConfig envConfig, HashTree samplerHashTree, String protocal, String environmentId, ParameterConfig config, boolean isAfterPrivateScript) {
GlobalScriptConfig globalScriptConfig = envConfig != null ? envConfig.getGlobalScriptConfig() : null;
MsJSR223PreProcessor preProcessor = JMeterScriptUtil.getPreScript(envConfig);
MsJSR223PostProcessor postProcessor = JMeterScriptUtil.getPostScript(envConfig);
setScript(globalScriptConfig, protocal, isAfterPrivateScript, environmentId, config, samplerHashTree, preProcessor, postProcessor);
}
public static void setScript(GlobalScriptConfig globalScriptConfig, String protocal, boolean isAfterPrivateScript, String environmentId, ParameterConfig config,
HashTree samplerHashTree, MsJSR223PreProcessor preProcessor, MsJSR223PostProcessor postProcessor) {
boolean isPreScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPreScriptExecAfterPrivateScript();
boolean isPostScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPostScriptExecAfterPrivateScript();
List<String> preFilterProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPreScript();
List<String> postFilterProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPostScript();
MsJSR223PreProcessor preProcessor = JMeterScriptUtil.getPreScript(envConfig);
MsJSR223PostProcessor postProcessor = JMeterScriptUtil.getPostScript(envConfig);
boolean globalPreScriptIsFilter = JMeterScriptUtil.isScriptFilter(preFilterProtocal, protocal);
boolean globalPostScriptIsFilter = JMeterScriptUtil.isScriptFilter(postFilterProtocal, protocal);
if (isAfterPrivateScript) {
@ -92,41 +97,12 @@ public class JMeterScriptUtil {
}
}
public static void setHttpScript(HttpConfig httpConfig, HashTree httpSamplerTree, ParameterConfig config, String useEnvironment, String environmentId) {
public static void setScriptByHttpConfig(HttpConfig httpConfig, HashTree httpSamplerTree, ParameterConfig config, String useEnvironment, String environmentId, boolean isStepAfterElement) {
MsJSR223PreProcessor preProcessor = httpConfig.getPreProcessor();
MsJSR223PostProcessor postProcessor = httpConfig.getPostProcessor();
GlobalScriptConfig globalScriptConfig = httpConfig.getGlobalScriptConfig();
List<String> filterPreProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPreScript();
List<String> filterPostProtocal = globalScriptConfig == null ? null : globalScriptConfig.getFilterRequestPostScript();
boolean filterPre = JMeterScriptUtil.isScriptFilter(filterPreProtocal, GlobalScriptFilterRequest.HTTP.name());
boolean filterPost = JMeterScriptUtil.isScriptFilter(filterPostProtocal, GlobalScriptFilterRequest.HTTP.name());
boolean isPreScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPreScriptExecAfterPrivateScript();
boolean isPostScriptExecAfterPrivateScript = globalScriptConfig == null ? false : globalScriptConfig.isPostScriptExecAfterPrivateScript();
if (!filterPre && preProcessor != null && StringUtils.isNotEmpty(preProcessor.getScript())) {
if ((isPreScriptExecAfterPrivateScript) || (!isPreScriptExecAfterPrivateScript)) {
if (preProcessor.getEnvironmentId() == null) {
if (environmentId == null) {
preProcessor.setEnvironmentId(useEnvironment);
} else {
preProcessor.setEnvironmentId(useEnvironment);
}
}
preProcessor.toHashTree(httpSamplerTree, preProcessor.getHashTree(), config);
}
}
if (!filterPost && postProcessor != null && StringUtils.isNotEmpty(postProcessor.getScript())) {
if ((isPostScriptExecAfterPrivateScript) || (!isPostScriptExecAfterPrivateScript)) {
if (postProcessor.getEnvironmentId() == null) {
if (environmentId == null) {
postProcessor.setEnvironmentId(useEnvironment);
} else {
postProcessor.setEnvironmentId(environmentId);
}
}
postProcessor.toHashTree(httpSamplerTree, postProcessor.getHashTree(), config);
}
}
setScript(globalScriptConfig, GlobalScriptFilterRequest.HTTP.name(), isStepAfterElement, environmentId == null ? useEnvironment : environmentId, config, httpSamplerTree, preProcessor, postProcessor);
}
}

View File

@ -232,7 +232,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (httpConfig != null) {
//根据配置增加全局前后至脚本
JMeterScriptUtil.setHttpScript(httpConfig, httpSamplerTree, config, useEnvironment, this.getEnvironmentId());
JMeterScriptUtil.setScriptByHttpConfig(httpConfig, httpSamplerTree, config, useEnvironment, this.getEnvironmentId(), false);
//增加误报断言
if (CollectionUtils.isNotEmpty(httpConfig.getErrorReportAssertions())) {
for (MsAssertions assertion : httpConfig.getErrorReportAssertions()) {
@ -260,7 +260,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
//根据配置增加全局前后至脚本
if (httpConfig != null) {
JMeterScriptUtil.setHttpScript(httpConfig, httpSamplerTree, config, useEnvironment, this.getEnvironmentId());
JMeterScriptUtil.setScriptByHttpConfig(httpConfig, httpSamplerTree, config, useEnvironment, this.getEnvironmentId(), true);
}
}
@ -658,22 +658,22 @@ public class MsHTTPSamplerProxy extends MsTestElement {
list.stream().
filter(KeyValue::isValid).
filter(KeyValue::isEnable).forEach(keyValue -> {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
arguments.addArgument(httpArgument);
} catch (Exception e) {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
arguments.addArgument(httpArgument);
} catch (Exception e) {
}
}
);
}
}
);
return arguments;
}

View File

@ -169,7 +169,7 @@ public class MsJDBCSampler extends MsTestElement {
environmentId = this.useEnvironment;
}
//根据配置将脚本放置在私有脚本之前
JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), environmentId, config, false);
JMeterScriptUtil.setScriptByEnvironmentConfig(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), environmentId, config, false);
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {
@ -177,7 +177,7 @@ public class MsJDBCSampler extends MsTestElement {
});
}
//根据配置将脚本放置在私有脚本之后
JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), environmentId, config, true);
JMeterScriptUtil.setScriptByEnvironmentConfig(envConfig, samplerHashTree, GlobalScriptFilterRequest.JDBC.name(), environmentId, config, true);
}

View File

@ -164,7 +164,7 @@ public class MsTCPSampler extends MsTestElement {
environmentId = this.useEnvironment;
}
//根据配置将脚本放置在私有脚本之前
JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), environmentId, config, false);
JMeterScriptUtil.setScriptByEnvironmentConfig(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), environmentId, config, false);
HashTreeUtil hashTreeUtil = new HashTreeUtil();
@ -179,7 +179,7 @@ public class MsTCPSampler extends MsTestElement {
});
}
//根据配置将脚本放置在私有脚本之后
JMeterScriptUtil.setScript(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), environmentId, config, true);
JMeterScriptUtil.setScriptByEnvironmentConfig(envConfig, samplerHashTree, GlobalScriptFilterRequest.TCP.name(), environmentId, config, true);
}
private boolean setRefElement() {