fix(接口测试): 修复jmx中包beanshell脚本导入的相关问题

--bug=1020847 --user=王孝刚 【接口测试】接口场景中导入带BeanShell脚本的jmx文件,beanshell脚本没显示在接口内 https://www.tapd.cn/55049933/s/1315717
--bug=1020849 --user=王孝刚 【接口测试】导入带BeanShell处理器的jmx接口文件,提示格式错误 https://www.tapd.cn/55049933/s/1315720
This commit is contained in:
wxg0103 2022-12-14 16:46:33 +08:00 committed by fit2-zhao
parent 281b7e3c70
commit 50942557c9
2 changed files with 35 additions and 9 deletions

View File

@ -46,10 +46,12 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.*;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.extractor.BeanShellPostProcessor;
import org.apache.jmeter.extractor.JSR223PostProcessor;
import org.apache.jmeter.extractor.RegexExtractor;
import org.apache.jmeter.extractor.XPath2Extractor;
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.modifiers.BeanShellPreProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
@ -110,10 +112,6 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
for (MsTestElement element : results) {
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = buildApiDefinition(element);
if (apiDefinitionWithBLOBs != null) {
if (element.getHashTree() != null) {
element.getHashTree().clear();
}
element.setEnable(true);
apiDefinitionWithBLOBs.setRequest(JSON.toJSONString(element));
HttpResponse defaultHttpResponse = getDefaultHttpResponse();
@ -394,6 +392,10 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PreProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223PreProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
} else if (key instanceof BeanShellPostProcessor) {
elementNode = MsJmeterParser.getMsTestElement((BeanShellPostProcessor) key);
} else if (key instanceof BeanShellPreProcessor) {
elementNode = MsJmeterParser.getMsTestElement((BeanShellPreProcessor) key);
}
// 断言规则
else if (key instanceof ResponseAssertion || key instanceof JSONPathAssertion || key instanceof XPath2Assertion || key instanceof JSR223Assertion || key instanceof DurationAssertion) {

View File

@ -42,14 +42,10 @@ import io.metersphere.commons.constants.LoopConstants;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.*;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.request.BodyFile;
import io.metersphere.commons.utils.JSONUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
@ -59,10 +55,12 @@ import org.apache.jmeter.control.ForeachController;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.TransactionController;
import org.apache.jmeter.control.WhileController;
import org.apache.jmeter.extractor.BeanShellPostProcessor;
import org.apache.jmeter.extractor.JSR223PostProcessor;
import org.apache.jmeter.extractor.RegexExtractor;
import org.apache.jmeter.extractor.XPath2Extractor;
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.modifiers.BeanShellPreProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
@ -746,6 +744,10 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
BeanUtils.copyBean(elementNode, jsr223Sampler);
((MsJSR223PostProcessor) elementNode).setScript(jsr223Sampler.getPropertyAsString("script"));
((MsJSR223PostProcessor) elementNode).setScriptLanguage(jsr223Sampler.getPropertyAsString("scriptLanguage"));
} else if (key instanceof BeanShellPostProcessor) {
elementNode = getMsTestElement((BeanShellPostProcessor) key);
} else if (key instanceof BeanShellPreProcessor) {
elementNode = getMsTestElement((BeanShellPreProcessor) key);
}
// 前置脚本
else if (key instanceof JSR223PreProcessor) {
@ -853,4 +855,26 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
}
}
}
public static MsTestElement getMsTestElement(BeanShellPreProcessor key) {
MsTestElement elementNode;
BeanShellPreProcessor beanShellPreProcessor = key;
elementNode = new MsJSR223PreProcessor();
BeanUtils.copyBean(elementNode, beanShellPreProcessor);
((MsJSR223PreProcessor) elementNode).setJsrEnable(false);
((MsJSR223PreProcessor) elementNode).setScript(beanShellPreProcessor.getPropertyAsString("script"));
((MsJSR223PreProcessor) elementNode).setScriptLanguage(beanShellPreProcessor.getPropertyAsString("scriptLanguage"));
return elementNode;
}
public static MsTestElement getMsTestElement(BeanShellPostProcessor key) {
MsTestElement elementNode;
BeanShellPostProcessor beanShellPostProcessor = key;
elementNode = new MsJSR223PostProcessor();
((MsJSR223PostProcessor) elementNode).setJsrEnable(false);
BeanUtils.copyBean(elementNode, beanShellPostProcessor);
((MsJSR223PostProcessor) elementNode).setScript(beanShellPostProcessor.getPropertyAsString("script"));
((MsJSR223PostProcessor) elementNode).setScriptLanguage(beanShellPostProcessor.getPropertyAsString("scriptLanguage"));
return elementNode;
}
}