feat(接口测试): 优化提前和断言结果展示

This commit is contained in:
AgAngle 2024-05-09 20:01:16 +08:00 committed by 刘瑞斌
parent 73635fd7b8
commit 7652bca100
17 changed files with 122 additions and 14 deletions

View File

@ -0,0 +1,15 @@
package io.metersphere.sdk.dto.api.result;
import lombok.Data;
/**
* @Author: jianxing
* @CreateTime: 2024-05-09 17:08
*/
@Data
public class ExtractResult {
private String name;
private String value;
private String type;
private String expression;
}

View File

@ -16,6 +16,7 @@ public class ResponseAssertionResult {
/**
* 断言内容
*/
@Deprecated
private String content;
/**
@ -23,6 +24,27 @@ public class ResponseAssertionResult {
*/
private String script;
/**
* 实际值
*/
private String actualValue;
/**
* 期望值
*/
private String expectedValue;
/**
* 断言类型
* {@link AssertionResultType}
*/
private String assertionType;
/**
* 断言条件
*/
private String condition;
/**
* 断言结果
*/
@ -32,4 +54,16 @@ public class ResponseAssertionResult {
* 是否通过
*/
private boolean pass;
public enum AssertionResultType {
DOCUMENT,
RESPONSE_CODE,
RESPONSE_HEADER,
RESPONSE_TIME,
SCRIPT,
VARIABLE,
JSON_PATH,
XPATH,
REGEX
}
}

View File

@ -51,10 +51,7 @@ public class ResponseResult {
*/
private String contentType;
/**
* 过程变量处理信息
*/
private String vars;
private List<ExtractResult> extractResults;
/**
* 图片内容

View File

@ -8,6 +8,7 @@ import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.ResponseAssertion;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
@ -64,4 +65,16 @@ public abstract class AssertionConverter<T extends MsAssertion> {
}
return text;
}
public static void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name, String condition, String expectedValue) {
// 保存断言信息
assertion.setProperty("name", name);
assertion.setProperty("assertionType", assertionType);
assertion.setProperty("condition", condition);
assertion.setProperty("expectedValue", expectedValue);
}
public static void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name) {
AssertionConverter.setMsAssertionInfoProperty(assertion, assertionType, name, null, null);
}
}

View File

@ -2,6 +2,7 @@ package io.metersphere.api.parser.jmeter.processor.assertion;
import io.metersphere.project.api.assertion.MsResponseCodeAssertion;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.ResponseAssertion;
import org.apache.jorphan.collections.HashTree;
@ -27,7 +28,6 @@ public class ResponseCodeAssertionConverter extends AssertionConverter<MsRespons
ResponseAssertion assertion = createResponseAssertion();
String expectedValue = msAssertion.getExpectedValue();
assertion.setEnabled(msAssertion.getEnable());
assertion.setAssumeSuccess(true);
assertion.setEnabled(msAssertion.getEnable());
@ -36,6 +36,8 @@ public class ResponseCodeAssertionConverter extends AssertionConverter<MsRespons
assertion.addTestString(generateRegexExpression(condition, expectedValue));
assertion.setToContainsType();
assertion.setTestFieldResponseCode();
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.RESPONSE_CODE.name(), assertion.getName(), condition, expectedValue);
return assertion;
}
}

View File

@ -3,6 +3,7 @@ package io.metersphere.api.parser.jmeter.processor.assertion;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.project.api.assertion.MsResponseHeaderAssertion;
import io.metersphere.sdk.constants.MsAssertionCondition;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import io.metersphere.sdk.util.EnumValidator;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@ -50,6 +51,9 @@ public class ResponseHeaderAssertionConverter extends AssertionConverter<MsRespo
String condition = msAssertion.getCondition();
MsAssertionCondition msAssertionCondition = EnumValidator.validateEnum(MsAssertionCondition.class, condition);
String header = msAssertion.getHeader();
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.RESPONSE_HEADER.name(), header, condition, expectedValue);
String regexTemplate = switch (msAssertionCondition) {
case NOT_CONTAINS, CONTAINS -> "((?:[\\r\\n]%key|^%key):.*%value)";
case EQUALS, NOT_EQUALS -> "((?:[\\r\\n]%key|^%key):\\s*(?:%value[\\r\\n]|%value$))";

View File

@ -2,6 +2,7 @@ package io.metersphere.api.parser.jmeter.processor.assertion;
import io.metersphere.project.api.assertion.MsResponseTimeAssertion;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import org.apache.jmeter.assertions.DurationAssertion;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
@ -31,6 +32,7 @@ public class ResponseTimeAssertionConverter extends AssertionConverter<MsRespons
assertion.setProperty(TestElement.TEST_CLASS, DurationAssertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(DURATION_ASSERTION_GUI));
assertion.setAllowedDuration(msAssertion.getExpectedValue());
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.RESPONSE_TIME.name(), String.valueOf(msAssertion.getExpectedValue()), null, String.valueOf(msAssertion.getExpectedValue()));
return assertion;
}

View File

@ -7,11 +7,13 @@ import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.project.api.assertion.MsScriptAssertion;
import io.metersphere.project.api.processor.ScriptProcessor;
import io.metersphere.project.constants.ScriptLanguageType;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import io.metersphere.sdk.util.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.BeanShellAssertion;
import org.apache.jmeter.assertions.JSR223Assertion;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
@ -28,7 +30,7 @@ public class ScriptAssertionConverter extends AssertionConverter<MsScriptAsserti
return;
}
TestElement assertion;
AbstractTestElement assertion;
if (isJSR233(msAssertion)) {
assertion = new JSR223Assertion();
} else {
@ -47,6 +49,8 @@ public class ScriptAssertionConverter extends AssertionConverter<MsScriptAsserti
Optional.ofNullable(ScriptProcessorConverter.getScriptArguments(scriptProcessor))
.ifPresent(hashTree::add);
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.SCRIPT.name(), assertion.getName());
hashTree.add(assertion);
}

View File

@ -6,6 +6,7 @@ import io.metersphere.project.api.processor.ScriptProcessor;
import io.metersphere.api.parser.jmeter.processor.ScriptProcessorConverter;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.sdk.constants.MsAssertionCondition;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
@ -59,6 +60,8 @@ public class VariableAssertionConverter extends AssertionConverter<MsVariableAss
scriptProcessor.setScriptLanguage(ScriptLanguageType.GROOVY.name());
JSR223Assertion jsr223Assertion = new JSR223Assertion();
ScriptProcessorConverter.parse(jsr223Assertion, scriptProcessor, config);
setMsAssertionInfoProperty(jsr223Assertion, ResponseAssertionResult.AssertionResultType.VARIABLE.name(), variableName, condition, expectedValue);
return jsr223Assertion;
}
@ -207,13 +210,13 @@ public class VariableAssertionConverter extends AssertionConverter<MsVariableAss
}
script += """
if (!result) {
if (!result){
if (flag) {
msg = "assertion [" + msg + "]: false;";
}
AssertionResult.setFailureMessage(msg);
AssertionResult.setFailure(true);
}
AssertionResult.setFailureMessage(msg + "&&&" + variableValue);
""";
return script;
}

View File

@ -1,9 +1,10 @@
package io.metersphere.api.parser.jmeter.processor.assertion.body;
import io.metersphere.project.api.assertion.body.MsJSONPathAssertion;
import io.metersphere.project.api.assertion.body.MsJSONPathAssertionItem;
import io.metersphere.assertions.JSONPathAssertion;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.project.api.assertion.body.MsJSONPathAssertion;
import io.metersphere.project.api.assertion.body.MsJSONPathAssertionItem;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
@ -34,6 +35,9 @@ public class JSONPathAssertionConverter extends ResponseBodyTypeAssertionConvert
String condition = msAssertion.getCondition();
String expression = msAssertion.getExpression();
String expectedValue = msAssertion.getExpectedValue();
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.JSON_PATH.name(), expression, condition, expectedValue);
assertion.setName(String.format("Response data JSONPath expect %s %s %s", expression, condition.toLowerCase().replace("_", ""), expectedValue));
assertion.setEnabled(msAssertion.getEnable());
if (BooleanUtils.isFalse(globalEnable)) {

View File

@ -4,6 +4,7 @@ import io.metersphere.project.api.assertion.body.MsRegexAssertion;
import io.metersphere.project.api.assertion.body.MsRegexAssertionItem;
import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverter;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.jmeter.assertions.ResponseAssertion;
import org.apache.jorphan.collections.HashTree;
@ -31,6 +32,9 @@ public class RegexAssertionConverter extends ResponseBodyTypeAssertionConverter<
private ResponseAssertion parse2RegexResponseAssertion(MsRegexAssertionItem msAssertion, Boolean globalEnable) {
ResponseAssertion assertion = AssertionConverter.createResponseAssertion();
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.REGEX.name(), msAssertion.getExpression());
assertion.setEnabled(msAssertion.getEnable());
assertion.setName("Response data expect regex " + msAssertion.getExpression());
assertion.addTestString(msAssertion.getExpression());

View File

@ -1,8 +1,10 @@
package io.metersphere.api.parser.jmeter.processor.assertion.body;
import io.metersphere.api.parser.jmeter.processor.assertion.AssertionConverter;
import io.metersphere.project.api.assertion.body.MsBodyAssertionItem;
import io.metersphere.plugin.api.dto.ParameterConfig;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jorphan.collections.HashTree;
/**
@ -26,4 +28,12 @@ public abstract class ResponseBodyTypeAssertionConverter <T> {
}
return false;
}
public void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name, String condition, String expectedValue) {
AssertionConverter.setMsAssertionInfoProperty(assertion, assertionType, name, condition, expectedValue);
}
public void setMsAssertionInfoProperty(AbstractTestElement assertion, String assertionType, String name) {
AssertionConverter.setMsAssertionInfoProperty(assertion, assertionType, name);
}
}

View File

@ -4,6 +4,7 @@ import io.metersphere.project.api.assertion.body.MsXPathAssertion;
import io.metersphere.project.api.assertion.body.MsXPathAssertionItem;
import io.metersphere.project.api.processor.extract.XPathExtract;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.sdk.dto.api.result.ResponseAssertionResult;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.XPath2Assertion;
@ -43,13 +44,15 @@ public class XPathAssertionConverter extends ResponseBodyTypeAssertionConverter<
private XPathAssertion parse2XPathAssertion(MsXPathAssertionItem msAssertion, Boolean globalEnable) {
XPathAssertion assertion = new XPathAssertion();
String expression = msAssertion.getExpression();
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.XPATH.name(), expression);
assertion.setEnabled(msAssertion.getEnable());
assertion.setTolerant(true);
assertion.setValidating(false);
assertion.setName("Response data expect xpath " + msAssertion.getExpression());
assertion.setName("Response data expect xpath " + expression);
assertion.setProperty(TestElement.TEST_CLASS, XPathAssertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(XPATH_ASSERTION_GUI));
assertion.setXPathString(msAssertion.getExpression());
assertion.setXPathString(expression);
assertion.setNegated(false);
assertion.setEnabled(msAssertion.getEnable());
if (BooleanUtils.isFalse(globalEnable)) {
@ -61,11 +64,13 @@ public class XPathAssertionConverter extends ResponseBodyTypeAssertionConverter<
private XPath2Assertion parse2XPath2Assertion(MsXPathAssertionItem msAssertion, Boolean globalEnable) {
XPath2Assertion assertion = new XPath2Assertion();
String expression = msAssertion.getExpression();
setMsAssertionInfoProperty(assertion, ResponseAssertionResult.AssertionResultType.XPATH.name(), expression);
assertion.setEnabled(msAssertion.getEnable());
assertion.setName("Response date expect xpath " + msAssertion.getExpression());
assertion.setName("Response date expect xpath " +expression);
assertion.setProperty(TestElement.TEST_CLASS, XPath2Assertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(X_PATH_2_ASSERTION_GUI));
assertion.setXPathString(msAssertion.getExpression());
assertion.setXPathString(expression);
assertion.setNegated(false);
assertion.setEnabled(msAssertion.getEnable());
if (BooleanUtils.isFalse(globalEnable)) {

View File

@ -4,6 +4,7 @@ import io.metersphere.project.api.processor.extract.MsExtract;
import io.metersphere.project.api.processor.extract.ResultMatchingExtract;
import io.metersphere.plugin.api.dto.ParameterConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jorphan.collections.HashTree;
/**
@ -36,4 +37,10 @@ public abstract class ExtractConverter<T extends MsExtract> {
}
return 1;
}
public static void setMsExtractInfoProperty(AbstractTestElement extract, String type, String expression) {
// 保存断言信息
extract.setProperty("type", type);
extract.setProperty("expression", expression);
}
}

View File

@ -30,6 +30,7 @@ public class JSONPathExtractConverter extends ExtractConverter<JSONPathExtract>
extractor.setComputeConcatenation(true);
}
extractor.setEnabled(msExtract.getEnable());
setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression());
hashTree.add(extractor);
}
}

View File

@ -30,6 +30,7 @@ public class RegexExtractConverter extends ExtractConverter<RegexExtract> {
extractor.setTemplate(getTemplate(msExtract.getExpressionMatchingRule()));
// 处理匹配多条等匹配规则
extractor.setMatchNumber(parseResultMatchingRule(msExtract));
setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression());
hashTree.add(extractor);
}

View File

@ -38,6 +38,7 @@ public class XPathExtractConverter extends ExtractConverter<XPathExtract> {
extractor.setEnabled(msExtract.getEnable());
// 处理匹配多条等匹配规则
extractor.setMatchNumber(parseResultMatchingRule(msExtract));
setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression());
return extractor;
}
@ -51,6 +52,7 @@ public class XPathExtractConverter extends ExtractConverter<XPathExtract> {
extractor.setEnabled(msExtract.getEnable());
// 处理匹配多条等匹配规则
extractor.setMatchNumber(parseResultMatchingRule(msExtract));
setMsExtractInfoProperty(extractor, msExtract.getVariableType(), msExtract.getExpression());
return extractor;
}
}