feat(接口测试): 场景设置的步骤等待时间

This commit is contained in:
AgAngle 2024-03-25 17:46:55 +08:00 committed by Craftsman
parent 88756c8685
commit 6f4d4e62e1
7 changed files with 68 additions and 42 deletions

View File

@ -14,7 +14,5 @@ public class MsConstantTimerController extends AbstractMsTestElement {
/** /**
* 延迟时间 * 延迟时间
*/ */
private String delay; private Long delay;
} }

View File

@ -1,51 +1,26 @@
package io.metersphere.api.parser.jmeter; package io.metersphere.api.parser.jmeter;
import io.metersphere.api.dto.request.controller.MsConstantTimerController; import io.metersphere.api.dto.request.controller.MsConstantTimerController;
import io.metersphere.plugin.api.constants.ElementProperty; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter;
import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter;
import io.metersphere.project.api.processor.TimeWaitingProcessor;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.jmeter.sampler.DebugSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.timers.ConstantTimer;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
public class MsConstantTimerControllerConverter extends AbstractJmeterElementConverter<MsConstantTimerController> { public class MsConstantTimerControllerConverter extends AbstractJmeterElementConverter<MsConstantTimerController> {
@Override @Override
public void toHashTree(HashTree tree, MsConstantTimerController element, ParameterConfig config) { public void toHashTree(HashTree tree, MsConstantTimerController element, ParameterConfig config) {
if (BooleanUtils.isFalse(element.getEnable())) { if (BooleanUtils.isFalse(element.getEnable())) {
LogUtils.info("MsConstantTimerController is disabled"); LogUtils.info("MsConstantTimerController is disabled");
return; return;
} }
HashTree groupTree = tree.add(constantTimer(element)); MsProcessorConverter timeWaitingConverter = MsProcessorConverterFactory.getPostConverter(TimeWaitingProcessor.class);
parseChild(groupTree, element, config); TimeWaitingProcessor timeWaitingProcessor = BeanUtils.copyBean(new TimeWaitingProcessor(), element);
timeWaitingConverter.parse(tree, timeWaitingProcessor, config);
} }
private DebugSampler constantTimer(MsConstantTimerController element) {
// 添加 debugSampler 步骤间生效
DebugSampler debugSampler = new DebugSampler();
debugSampler.setEnabled(true);
debugSampler.setName(ElementProperty.SCENARIO_CONSTANT_TIMER.name());
debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName());
debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
// 上面三行直接Set属性会导致DebugSampler构建时取不到值可能是JMeter的Bug,需要SetProperty
debugSampler.setProperty("displayJMeterProperties", false);
debugSampler.setProperty("displayJMeterVariables", false);
debugSampler.setProperty("displaySystemProperties", false);
ConstantTimer constantTimer = new ConstantTimer();
constantTimer.setEnabled(element.getEnable());
constantTimer.setName(element.getName() + " ms");
constantTimer.setProperty(TestElement.TEST_CLASS, ConstantTimer.class.getName());
constantTimer.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ConstantTimerGui"));
constantTimer.setDelay(element.getDelay());
debugSampler.addTestElement(constantTimer);
return debugSampler;
}
} }

View File

@ -6,15 +6,18 @@ import io.metersphere.api.dto.ApiScenarioParamConfig;
import io.metersphere.api.dto.request.MsScenario; import io.metersphere.api.dto.request.MsScenario;
import io.metersphere.api.dto.request.processors.MsProcessorConfig; import io.metersphere.api.dto.request.processors.MsProcessorConfig;
import io.metersphere.api.dto.scenario.ScenarioConfig; import io.metersphere.api.dto.scenario.ScenarioConfig;
import io.metersphere.api.dto.scenario.ScenarioOtherConfig;
import io.metersphere.api.dto.scenario.ScenarioStepConfig; import io.metersphere.api.dto.scenario.ScenarioStepConfig;
import io.metersphere.api.dto.scenario.ScenarioVariable; import io.metersphere.api.dto.scenario.ScenarioVariable;
import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverter;
import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory; import io.metersphere.api.parser.jmeter.processor.MsProcessorConverterFactory;
import io.metersphere.api.parser.jmeter.processor.TimeWaitingProcessorConverter;
import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFactory; import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverterFactory;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter;
import io.metersphere.project.api.assertion.MsAssertion; import io.metersphere.project.api.assertion.MsAssertion;
import io.metersphere.project.api.processor.MsProcessor; import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.api.processor.TimeWaitingProcessor;
import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import io.metersphere.project.dto.environment.processors.EnvProcessorConfig; import io.metersphere.project.dto.environment.processors.EnvProcessorConfig;
@ -27,6 +30,7 @@ import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.timers.ConstantTimer;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.util.List; import java.util.List;
@ -58,6 +62,9 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
// 添加场景和环境变量 // 添加场景和环境变量
addArguments(tree, msScenario, envInfo); addArguments(tree, msScenario, envInfo);
// 添加场景每个步骤的全局等待时间
addScenarioStepTimeWaiting(tree, msScenario);
// 添加环境的前置 // 添加环境的前置
addEnvScenarioProcessor(tree, msScenario, config, envInfo, true); addEnvScenarioProcessor(tree, msScenario, config, envInfo, true);
// 添加场景前置 // 添加场景前置
@ -76,6 +83,26 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
addScenarioAssertions(tree, msScenario, config); addScenarioAssertions(tree, msScenario, config);
} }
/**
* 添加步骤等待
* @param tree
* @param msScenario
*/
private void addScenarioStepTimeWaiting(HashTree tree, MsScenario msScenario) {
if (isRootScenario(msScenario.getRefType())) {
// 获取场景前后置
ScenarioConfig scenarioConfig = msScenario.getScenarioConfig();
ScenarioOtherConfig otherConfig = scenarioConfig.getOtherConfig();
if (BooleanUtils.isTrue(otherConfig.getEnableStepWait())) {
TimeWaitingProcessor timeWaitingProcessor = new TimeWaitingProcessor();
timeWaitingProcessor.setDelay(otherConfig.getStepWaitTime());
timeWaitingProcessor.setName(msScenario.getName());
ConstantTimer constantTimer = TimeWaitingProcessorConverter.getConstantTimer(timeWaitingProcessor);
tree.add(constantTimer);
}
}
}
/** /**
* 添加场景和环境变量 * 添加场景和环境变量
* @param tree * @param tree
@ -245,7 +272,6 @@ public class MsScenarioConverter extends AbstractJmeterElementConverter<MsScenar
ScenarioConfig scenarioConfig = msScenario.getScenarioConfig(); ScenarioConfig scenarioConfig = msScenario.getScenarioConfig();
MsProcessorConfig processorConfig = isPre ? scenarioConfig.getPreProcessorConfig() : scenarioConfig.getPostProcessorConfig(); MsProcessorConfig processorConfig = isPre ? scenarioConfig.getPreProcessorConfig() : scenarioConfig.getPostProcessorConfig();
if (processorConfig == null || CollectionUtils.isEmpty(processorConfig.getProcessors())) { if (processorConfig == null || CollectionUtils.isEmpty(processorConfig.getProcessors())) {
return; return;
} }

View File

@ -1,7 +1,11 @@
package io.metersphere.api.parser.jmeter.processor; package io.metersphere.api.parser.jmeter.processor;
import io.metersphere.project.api.processor.TimeWaitingProcessor; import io.metersphere.api.parser.jmeter.constants.JmeterAlias;
import io.metersphere.plugin.api.constants.ElementProperty;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.project.api.processor.TimeWaitingProcessor;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.sampler.DebugSampler;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.timers.ConstantTimer; import org.apache.jmeter.timers.ConstantTimer;
@ -17,12 +21,35 @@ import static io.metersphere.api.parser.jmeter.constants.JmeterAlias.CONSTANT_TI
public class TimeWaitingProcessorConverter extends MsProcessorConverter<TimeWaitingProcessor> { public class TimeWaitingProcessorConverter extends MsProcessorConverter<TimeWaitingProcessor> {
@Override @Override
public void parse(HashTree hashTree, TimeWaitingProcessor processor, ParameterConfig config) { public void parse(HashTree hashTree, TimeWaitingProcessor processor, ParameterConfig config) {
hashTree.add(getConstantTimerElement(processor));
}
private DebugSampler getConstantTimerElement(TimeWaitingProcessor processor) {
// 添加 debugSampler 步骤间生效
DebugSampler debugSampler = new DebugSampler();
debugSampler.setEnabled(true);
debugSampler.setName(ElementProperty.SCENARIO_CONSTANT_TIMER.name());
debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName());
debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI));
// 下面三行直接Set属性会导致DebugSampler构建时取不到值可能是JMeter的Bug,需要SetProperty
debugSampler.setProperty("displayJMeterProperties", false);
debugSampler.setProperty("displayJMeterVariables", false);
debugSampler.setProperty("displaySystemProperties", false);
ConstantTimer constantTimer = getConstantTimer(processor);
debugSampler.addTestElement(constantTimer);
return debugSampler;
}
public static ConstantTimer getConstantTimer(TimeWaitingProcessor processor) {
ConstantTimer constantTimer = new ConstantTimer(); ConstantTimer constantTimer = new ConstantTimer();
constantTimer.setEnabled(processor.getEnable()); constantTimer.setEnabled(processor.getEnable());
constantTimer.setName(processor.getDelay() + " ms"); constantTimer.setName(StringUtils.isBlank(processor.getName()) ? (processor.getDelay() + " ms") : processor.getName());
constantTimer.setProperty(TestElement.TEST_CLASS, ConstantTimer.class.getName()); constantTimer.setProperty(TestElement.TEST_CLASS, ConstantTimer.class.getName());
constantTimer.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(CONSTANT_TIMER_GUI)); constantTimer.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(CONSTANT_TIMER_GUI));
constantTimer.setDelay(processor.getDelay().toString()); constantTimer.setDelay(processor.getDelay().toString());
hashTree.add(constantTimer); return constantTimer;
} }
} }

View File

@ -958,7 +958,7 @@ public class ApiScenarioControllerTests extends BaseTest {
MsConstantTimerController constantTimerController = new MsConstantTimerController(); MsConstantTimerController constantTimerController = new MsConstantTimerController();
constantTimerController.setName(StringUtils.isNotBlank(name) ? name : "等待控制器"); constantTimerController.setName(StringUtils.isNotBlank(name) ? name : "等待控制器");
constantTimerController.setEnable(enable); constantTimerController.setEnable(enable);
constantTimerController.setDelay("1000"); constantTimerController.setDelay(1000L);
return constantTimerController; return constantTimerController;
} }

View File

@ -158,7 +158,7 @@ public class MsHTTPElementTest {
processors.add(sqlProcessor1); processors.add(sqlProcessor1);
TimeWaitingProcessor timeWaitingProcessor = new TimeWaitingProcessor(); TimeWaitingProcessor timeWaitingProcessor = new TimeWaitingProcessor();
timeWaitingProcessor.setDelay(1000); timeWaitingProcessor.setDelay(1000L);
timeWaitingProcessor.setEnable(true); timeWaitingProcessor.setEnable(true);
processors.add(timeWaitingProcessor); processors.add(timeWaitingProcessor);

View File

@ -15,5 +15,5 @@ public class TimeWaitingProcessor extends MsProcessor {
* 等待时间 * 等待时间
* 单位毫秒 * 单位毫秒
*/ */
private Integer delay; private Long delay;
} }