fix(接口测试): 修复导入原生jmx文件beanshell处理和平台不一致问题

--bug=1020951 --user=赵勇 【接口测试】接口场景中导入带BeanShell自定义脚本的jmx文件,beanshell自定义脚本建议和系统中原有的自定义脚本一样 https://www.tapd.cn/55049933/s/1316722
This commit is contained in:
fit2-zhao 2022-12-19 10:13:41 +08:00 committed by fit2-zhao
parent 448cb0e081
commit 5b897859ff
14 changed files with 48 additions and 36 deletions

View File

@ -904,7 +904,7 @@ public class ElementUtil {
testElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("BeanShellSamplerGui"));
} else {
testElement.setProperty("scriptLanguage", vo.getScriptLanguage());
testElement.setProperty("script", vo.getScript());
testElement.setProperty(ElementConstants.SCRIPT, vo.getScript());
}
}

View File

@ -222,7 +222,7 @@ public class MsAssertions extends MsTestElement {
scriptLanguage = "rhino";
}
assertion.setProperty("scriptLanguage", scriptLanguage);
assertion.setProperty("script", assertionJSR223.getScript());
assertion.setProperty(ElementConstants.SCRIPT, assertionJSR223.getScript());
return assertion;
}

View File

@ -205,9 +205,9 @@ public class MsLoopController extends MsTestElement {
sampler.setName("MS_CLEAR_LOOPS_VAR_" + ms_current_timer);
sampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
sampler.setProperty("scriptLanguage", "beanshell");
ScriptFilter.verify("beanshell", this.getName(), script());
sampler.setProperty("script", "vars.put(\"" + ms_current_timer + "\", null);");
sampler.setProperty("scriptLanguage", ElementConstants.BEANSHELL);
ScriptFilter.verify(ElementConstants.BEANSHELL, this.getName(), script());
sampler.setProperty(ElementConstants.SCRIPT, "vars.put(\"" + ms_current_timer + "\", null);");
hashTree.add(sampler);
}
@ -235,11 +235,11 @@ public class MsLoopController extends MsTestElement {
jsr223PreProcessor.setName("循环超时处理");
jsr223PreProcessor.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
jsr223PreProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
jsr223PreProcessor.setProperty("scriptLanguage", "beanshell");
jsr223PreProcessor.setProperty("scriptLanguage", ElementConstants.BEANSHELL);
ScriptFilter.verify("beanshell", this.getName(), script());
ScriptFilter.verify(ElementConstants.BEANSHELL, this.getName(), script());
jsr223PreProcessor.setProperty("script", script());
jsr223PreProcessor.setProperty(ElementConstants.SCRIPT, script());
hashTree.add(jsr223PreProcessor);
return hashTree;
}

View File

@ -74,7 +74,7 @@ public class MsExtract extends MsTestElement {
shell.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName());
shell.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
shell.setProperty("cacheKey", false);
shell.setProperty("script", "io.metersphere.utils.JMeterVars.addVars(prev.getResourceId(),vars," + "\"" + extract.toString() + "\"" + ");");
shell.setProperty(ElementConstants.SCRIPT, "io.metersphere.utils.JMeterVars.addVars(prev.getResourceId(),vars," + "\"" + extract.toString() + "\"" + ");");
samplerHashTree.add(shell);
}
}

View File

@ -1,5 +1,6 @@
package io.metersphere.api.dto.shell.filter;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.plugin.core.utils.LogUtil;
import org.apache.commons.io.IOUtils;
@ -34,7 +35,7 @@ public class ScriptFilter {
if (StringUtils.isNotEmpty(script)) {
final StringBuffer buffer = new StringBuffer();
switch (language) {
case "beanshell":
case ElementConstants.BEANSHELL:
blackList(buffer, script, beanshell);
break;
case "python":

View File

@ -29,11 +29,12 @@ import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.parse.ApiImportAbstractParser;
import io.metersphere.api.parse.scenario.MsJmeterParser;
import io.metersphere.api.parse.scenario.JMeterParser;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentExample;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.enums.ApiTestDataStatus;
@ -346,7 +347,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
if (key instanceof TestPlan) {
this.planName = ((TestPlan) key).getName();
elementNode = new MsJmeterElement();
((MsJmeterElement) elementNode).setJmeterElement(MsJmeterParser.objToXml(key));
((MsJmeterElement) elementNode).setJmeterElement(JMeterParser.objToXml(key));
((MsJmeterElement) elementNode).setElementType(key.getClass().getSimpleName());
}
// 线程组
@ -382,7 +383,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
JSR223PostProcessor jsr223Sampler = (JSR223PostProcessor) key;
elementNode = new MsJSR223PostProcessor();
BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PostProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223PostProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PostProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
}
// 前置脚本
@ -390,12 +391,12 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
JSR223PreProcessor jsr223Sampler = (JSR223PreProcessor) key;
elementNode = new MsJSR223PreProcessor();
BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PreProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223PreProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PreProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
} else if (key instanceof BeanShellPostProcessor) {
elementNode = MsJmeterParser.getMsTestElement((BeanShellPostProcessor) key);
elementNode = JMeterParser.getMsTestElement((BeanShellPostProcessor) key);
} else if (key instanceof BeanShellPreProcessor) {
elementNode = MsJmeterParser.getMsTestElement((BeanShellPreProcessor) key);
elementNode = JMeterParser.getMsTestElement((BeanShellPreProcessor) key);
}
// 断言规则
else if (key instanceof ResponseAssertion || key instanceof JSONPathAssertion || key instanceof XPath2Assertion || key instanceof JSR223Assertion || key instanceof DurationAssertion) {
@ -498,7 +499,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
JSR223Assertion jsr223Assertion = (JSR223Assertion) key;
msAssertionJSR223.setName(jsr223Assertion.getName());
msAssertionJSR223.setDesc(jsr223Assertion.getName());
msAssertionJSR223.setScript(jsr223Assertion.getPropertyAsString("script"));
msAssertionJSR223.setScript(jsr223Assertion.getPropertyAsString(ElementConstants.SCRIPT));
msAssertionJSR223.setScriptLanguage(jsr223Assertion.getPropertyAsString("scriptLanguage"));
assertions.setName(jsr223Assertion.getName());

View File

@ -85,7 +85,7 @@ import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
public class JMeterParser extends ApiImportAbstractParser<ScenarioImport> {
private final String ENV_NAME = "导入数据环境";
/**
* todo 存放单个请求下的Header 为了和平台对应
@ -103,7 +103,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
MsScenario scenario = new MsScenario();
scenario.setReferenced("IMPORT");
jmterHashTree(testPlan, scenario);
formatHashTree(testPlan, scenario);
this.projectId = request.getProjectId();
ScenarioImport scenarioImport = new ScenarioImport();
scenarioImport.setData(parseObj(scenario, request));
@ -656,7 +656,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
JSR223Assertion jsr223Assertion = (JSR223Assertion) key;
msAssertionJSR223.setName(jsr223Assertion.getName());
msAssertionJSR223.setDesc(jsr223Assertion.getName());
msAssertionJSR223.setScript(jsr223Assertion.getPropertyAsString("script"));
msAssertionJSR223.setScript(jsr223Assertion.getPropertyAsString(ElementConstants.SCRIPT));
msAssertionJSR223.setScriptLanguage(jsr223Assertion.getPropertyAsString("scriptLanguage"));
assertions.setName(jsr223Assertion.getName());
@ -687,7 +687,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
return null;
}
private void jmterHashTree(HashTree tree, MsTestElement scenario) {
private void formatHashTree(HashTree tree, MsTestElement scenario) {
for (Object key : tree.keySet()) {
MsTestElement elementNode;
if (CollectionUtils.isEmpty(scenario.getHashTree())) {
@ -734,7 +734,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
JSR223Sampler jsr223Sampler = (JSR223Sampler) key;
elementNode = new MsJSR223Processor();
BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223Processor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223Processor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
}
// 后置脚本
@ -742,7 +742,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
JSR223PostProcessor jsr223Sampler = (JSR223PostProcessor) key;
elementNode = new MsJSR223PostProcessor();
BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PostProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223PostProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PostProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
} else if (key instanceof BeanShellPostProcessor) {
elementNode = getMsTestElement((BeanShellPostProcessor) key);
@ -754,7 +754,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
JSR223PreProcessor jsr223Sampler = (JSR223PreProcessor) key;
elementNode = new MsJSR223PreProcessor();
BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PreProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223PreProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PreProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
}
// 断言规则
@ -851,7 +851,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
// 递归子项
HashTree node = tree.get(key);
if (node != null) {
jmterHashTree(node, elementNode);
formatHashTree(node, elementNode);
}
}
}
@ -862,8 +862,8 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
elementNode = new MsJSR223PreProcessor();
BeanUtils.copyBean(elementNode, beanShellPreProcessor);
((MsJSR223PreProcessor) elementNode).setJsrEnable(false);
((MsJSR223PreProcessor) elementNode).setScript(beanShellPreProcessor.getPropertyAsString("script"));
((MsJSR223PreProcessor) elementNode).setScriptLanguage(beanShellPreProcessor.getPropertyAsString("scriptLanguage"));
((MsJSR223PreProcessor) elementNode).setScript(beanShellPreProcessor.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PreProcessor) elementNode).setScriptLanguage(ElementConstants.BEANSHELL);
return elementNode;
}
@ -873,8 +873,8 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
elementNode = new MsJSR223PostProcessor();
((MsJSR223PostProcessor) elementNode).setJsrEnable(false);
BeanUtils.copyBean(elementNode, beanShellPostProcessor);
((MsJSR223PostProcessor) elementNode).setScript(beanShellPostProcessor.getPropertyAsString("script"));
((MsJSR223PostProcessor) elementNode).setScriptLanguage(beanShellPostProcessor.getPropertyAsString("scriptLanguage"));
((MsJSR223PostProcessor) elementNode).setScript(beanShellPostProcessor.getPropertyAsString(ElementConstants.SCRIPT));
((MsJSR223PostProcessor) elementNode).setScriptLanguage(ElementConstants.BEANSHELL);
return elementNode;
}
}

View File

@ -11,7 +11,7 @@ public class ScenarioImportParserFactory {
} else if (StringUtils.equals(ApiImportPlatform.Postman.name(), platform)) {
return new PostmanScenarioParser();
} else if (StringUtils.equals(ApiImportPlatform.Jmeter.name(), platform)) {
return new MsJmeterParser();
return new JMeterParser();
} else if (StringUtils.equals(ApiImportPlatform.Har.name(), platform)) {
return new HarScenarioParser();
}

View File

@ -49,4 +49,8 @@ public class ElementConstants {
this.add(ElementConstants.AUTH_MANAGER);
this.add(ElementConstants.ABS_SAMPLER);
}};
public static final String SCRIPT = "script";
public static final String BEANSHELL = "beanshell";
}

View File

@ -37,7 +37,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
public static final String BODY_RAW = "body_raw";
public static final String REQUEST = "request";
public static final String BODY_XML = "body_xml";
public static final String SCRIPT = "script";
public static final String SCRIPT = ElementConstants.SCRIPT;
@Override
public String diffResponse(String newValue, String oldValue) {

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.definition.request.assertions.MsAssertionRegex;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.dto.FileInfoDTO;
import io.metersphere.dto.JmeterRunRequestDTO;
@ -36,7 +37,7 @@ public class HashTreeUtil {
public static final String VALUE = "value";
public static final String ENABLE = "enable";
public static final String NAME = "name";
public static final String SCRIPT = "script";
public static final String SCRIPT = ElementConstants.SCRIPT;
public Map<String, Map<String, String>> getEnvParamsDataByHashTree(HashTree hashTree, BaseEnvironmentService apiTestEnvironmentService) {
Map<String, Map<String, String>> returnMap = new HashMap<>();

View File

@ -5,6 +5,7 @@ import io.metersphere.api.dto.mock.MockConfigRequestParams;
import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.api.dto.shell.filter.ScriptFilter;
import io.metersphere.api.exec.generator.JSONSchemaGenerator;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.enums.MockParamConditionEnums;
import io.metersphere.commons.exception.MSException;
@ -268,14 +269,14 @@ public class MockApiUtils {
public String getResultByResponseResult(String projectId, JSONObject bodyObj, String url, Map<String, String> headerMap, RequestMockParams requestMockParams, boolean useScript) {
MockScriptEngineUtils scriptEngineUtils = new MockScriptEngineUtils();
ScriptEngine scriptEngine = null;
String scriptLanguage = "beanshell";
String scriptLanguage = ElementConstants.BEANSHELL;
String script = null;
if (useScript) {
if (bodyObj.has("scriptObject")) {
try {
JSONObject scriptObj = bodyObj.optJSONObject("scriptObject");
scriptLanguage = scriptObj.optString("scriptLanguage");
script = scriptObj.optString("script");
script = scriptObj.optString(ElementConstants.SCRIPT);
} catch (Exception e) {
LogUtil.error(e);
}

View File

@ -2,6 +2,7 @@ package io.metersphere.commons.utils.mock;
import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.JSONUtil;
@ -86,9 +87,9 @@ public class MockScriptEngineUtils {
return null;
}
String preScript = StringUtils.EMPTY;
if (StringUtils.equalsIgnoreCase(scriptLanguage, "beanshell")) {
if (StringUtils.equalsIgnoreCase(scriptLanguage, ElementConstants.BEANSHELL)) {
ScriptEngineManager scriptEngineFactory = new ScriptEngineManager();
engine = scriptEngineFactory.getEngineByName("beanshell");
engine = scriptEngineFactory.getEngineByName(ElementConstants.BEANSHELL);
preScript = this.genBeanshellPreScript(url, headerMap, requestMockParams);
} else if (StringUtils.equalsIgnoreCase(scriptLanguage, "python")) {
ScriptEngineManager scriptEngineFactory = new ScriptEngineManager();

View File

@ -174,6 +174,9 @@ export default {
if (this.jsr223Processor.jsrEnable === null || this.jsr223Processor.jsrEnable === undefined) {
this.$set(this.jsr223Processor, 'jsrEnable', true);
}
if (!this.jsr223Processor.scriptLanguage) {
this.$set(this.jsr223Processor, 'scriptLanguage', 'beanshell');
}
this.jsr223ProcessorData = this.jsr223Processor;
},
props: {