feat: 断言支持JSONPath

This commit is contained in:
chenjianxing 2020-07-30 17:01:04 +08:00
parent 62bec3d557
commit 2d2ac4f632
12 changed files with 183 additions and 6 deletions

View File

@ -0,0 +1,16 @@
package io.metersphere.api.dto.scenario.assertions;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class AssertionJsonPath extends AssertionType {
private String expect;
private String expression;
private String description;
public AssertionJsonPath() {
setType(AssertionType.JSON_PATH);
}
}

View File

@ -6,6 +6,7 @@ import lombok.Data;
public class AssertionType { public class AssertionType {
public final static String REGEX = "Regex"; public final static String REGEX = "Regex";
public final static String DURATION = "Duration"; public final static String DURATION = "Duration";
public final static String JSON_PATH = "JSONPath";
public final static String TEXT = "Text"; public final static String TEXT = "Text";
private String type; private String type;

View File

@ -7,5 +7,6 @@ import java.util.List;
@Data @Data
public class Assertions { public class Assertions {
private List<AssertionRegex> regex; private List<AssertionRegex> regex;
private List<AssertionJsonPath> jsonPath;
private AssertionDuration duration; private AssertionDuration duration;
} }

View File

@ -1,6 +1,7 @@
package io.metersphere.api.jmeter; package io.metersphere.api.jmeter;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.config.JmeterProperties; import io.metersphere.config.JmeterProperties;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.Arguments;
@ -33,6 +34,7 @@ public class JMeterService {
LocalRunner runner = new LocalRunner(testPlan); LocalRunner runner = new LocalRunner(testPlan);
runner.run(); runner.run();
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage(), e);
MSException.throwException(Translator.get("api_load_script_error")); MSException.throwException(Translator.get("api_load_script_error"));
} }
} }

View File

@ -0,0 +1,93 @@
<template>
<div>
<el-row :gutter="10" type="flex" justify="space-between" align="middle">
<el-col>
<el-input :disabled="isReadOnly" v-model="jsonPath.expression" maxlength="255" size="small" show-word-limit
:placeholder="$t('api_test.request.extract.json_path_expression')"/>
</el-col>
<el-col>
<el-input :disabled="isReadOnly" v-model="jsonPath.expect" maxlength="255" size="small" show-word-limit
:placeholder="$t('api_test.request.assertions.expect')"/>
</el-col>
<el-col class="assertion-btn">
<el-button :disabled="isReadOnly" type="danger" size="mini" icon="el-icon-delete" circle @click="remove" v-if="edit"/>
<el-button :disabled="isReadOnly" type="primary" size="small" @click="add" v-else>Add</el-button>
</el-col>
</el-row>
</div>
</template>
<script>
import {JSONPath} from "../../model/ScenarioModel";
export default {
name: "MsApiAssertionJsonPath",
props: {
jsonPath: {
type: JSONPath,
default: () => {
return new JSONPath();
}
},
edit: {
type: Boolean,
default: false
},
index: Number,
list: Array,
callback: Function,
isReadOnly: {
type: Boolean,
default: false
}
},
data() {
return {
}
},
watch: {
'jsonPath.expect'() {
this.setJSONPathDescription();
},
'jsonPath.expression'() {
this.setJSONPathDescription();
}
},
methods: {
add: function () {
this.list.push(this.getJSONPath());
this.callback();
},
remove: function () {
this.list.splice(this.index, 1);
},
getJSONPath() {
let jsonPath = new JSONPath(this.jsonPath);
jsonPath.description = jsonPath.expression + " expect: " + (jsonPath.expect ? jsonPath.expect : '');
return jsonPath;
},
setJSONPathDescription() {
this.jsonPath.description = this.jsonPath.expression + " expect: " + (this.jsonPath.expect ? this.jsonPath.expect : '');
}
}
}
</script>
<style scoped>
.assertion-select {
width: 250px;
}
.assertion-item {
width: 100%;
}
.assertion-btn {
text-align: center;
width: 60px;
}
</style>

View File

@ -7,12 +7,14 @@
size="small"> size="small">
<el-option :label="$t('api_test.request.assertions.text')" :value="options.TEXT"/> <el-option :label="$t('api_test.request.assertions.text')" :value="options.TEXT"/>
<el-option :label="$t('api_test.request.assertions.regex')" :value="options.REGEX"/> <el-option :label="$t('api_test.request.assertions.regex')" :value="options.REGEX"/>
<el-option :label="'JSONPath'" :value="options.JSON_PATH"/>
<el-option :label="$t('api_test.request.assertions.response_time')" :value="options.DURATION"/> <el-option :label="$t('api_test.request.assertions.response_time')" :value="options.DURATION"/>
</el-select> </el-select>
</el-col> </el-col>
<el-col :span="20"> <el-col :span="20">
<ms-api-assertion-text :is-read-only="isReadOnly" :list="assertions.regex" v-if="type === options.TEXT" :callback="after"/> <ms-api-assertion-text :is-read-only="isReadOnly" :list="assertions.regex" v-if="type === options.TEXT" :callback="after"/>
<ms-api-assertion-regex :is-read-only="isReadOnly" :list="assertions.regex" v-if="type === options.REGEX" :callback="after"/> <ms-api-assertion-regex :is-read-only="isReadOnly" :list="assertions.regex" v-if="type === options.REGEX" :callback="after"/>
<ms-api-assertion-json-path :is-read-only="isReadOnly" :list="assertions.jsonPath" v-if="type === options.JSON_PATH" :callback="after"/>
<ms-api-assertion-duration :is-read-only="isReadOnly" v-model="time" :duration="assertions.duration" <ms-api-assertion-duration :is-read-only="isReadOnly" v-model="time" :duration="assertions.duration"
v-if="type === options.DURATION" :callback="after"/> v-if="type === options.DURATION" :callback="after"/>
<el-button v-if="!type" :disabled="true" type="primary" size="small">Add</el-button> <el-button v-if="!type" :disabled="true" type="primary" size="small">Add</el-button>
@ -30,11 +32,14 @@
import MsApiAssertionDuration from "./ApiAssertionDuration"; import MsApiAssertionDuration from "./ApiAssertionDuration";
import {ASSERTION_TYPE, Assertions} from "../../model/ScenarioModel"; import {ASSERTION_TYPE, Assertions} from "../../model/ScenarioModel";
import MsApiAssertionsEdit from "./ApiAssertionsEdit"; import MsApiAssertionsEdit from "./ApiAssertionsEdit";
import MsApiAssertionJsonPath from "./ApiAssertionJsonPath";
export default { export default {
name: "MsApiAssertions", name: "MsApiAssertions",
components: {MsApiAssertionsEdit, MsApiAssertionDuration, MsApiAssertionRegex, MsApiAssertionText}, components: {
MsApiAssertionJsonPath,
MsApiAssertionsEdit, MsApiAssertionDuration, MsApiAssertionRegex, MsApiAssertionText},
props: { props: {
assertions: Assertions, assertions: Assertions,

View File

@ -9,6 +9,15 @@
</div> </div>
</div> </div>
<div class="assertion-item-editing json_path" v-if="assertions.jsonPath.length > 0">
<div>
{{'JSONPath'}}
</div>
<div class="regex-item" v-for="(jsonPath, index) in assertions.jsonPath" :key="index">
<ms-api-assertion-json-path :is-read-only="isReadOnly" :list="assertions.jsonPath" :json-path="jsonPath" :edit="true" :index="index"/>
</div>
</div>
<div class="assertion-item-editing response-time" v-if="isShow"> <div class="assertion-item-editing response-time" v-if="isShow">
<div> <div>
{{$t("api_test.request.assertions.response_time")}} {{$t("api_test.request.assertions.response_time")}}
@ -23,11 +32,12 @@
import MsApiAssertionRegex from "./ApiAssertionRegex"; import MsApiAssertionRegex from "./ApiAssertionRegex";
import MsApiAssertionDuration from "./ApiAssertionDuration"; import MsApiAssertionDuration from "./ApiAssertionDuration";
import {Assertions} from "../../model/ScenarioModel"; import {Assertions} from "../../model/ScenarioModel";
import MsApiAssertionJsonPath from "./ApiAssertionJsonPath";
export default { export default {
name: "MsApiAssertionsEdit", name: "MsApiAssertionsEdit",
components: {MsApiAssertionDuration, MsApiAssertionRegex}, components: {MsApiAssertionJsonPath, MsApiAssertionDuration, MsApiAssertionRegex},
props: { props: {
assertions: Assertions, assertions: Assertions,
@ -56,6 +66,10 @@
border-left: 2px solid #7B0274; border-left: 2px solid #7B0274;
} }
.assertion-item-editing.json_path {
border-left: 2px solid #44B3D2;
}
.assertion-item-editing.response-time { .assertion-item-editing.response-time {
border-left: 2px solid #DD0240; border-left: 2px solid #DD0240;
} }

View File

@ -352,6 +352,20 @@ export class ResponseAssertion extends DefaultTestElement {
} }
} }
export class JSONPathAssertion extends DefaultTestElement {
constructor(testName, jsonPath) {
super('JSONPathAssertion', 'JSONPathAssertionGui', 'JSONPathAssertion', testName);
this.jsonPath = jsonPath || {};
this.stringProp('JSON_PATH', this.jsonPath.expression);
this.stringProp('EXPECTED_VALUE', this.jsonPath.expect);
this.boolProp('JSONVALIDATION', true);
this.boolProp('EXPECT_NULL', false);
this.boolProp('INVERT', false);
this.boolProp('ISREGEX', true);
}
}
export class ResponseCodeAssertion extends ResponseAssertion { export class ResponseCodeAssertion extends ResponseAssertion {
constructor(testName, type, value, message) { constructor(testName, type, value, message) {
let assertion = { let assertion = {

View File

@ -12,7 +12,7 @@ import {
ResponseCodeAssertion, ResponseCodeAssertion,
ResponseDataAssertion, ResponseDataAssertion,
ResponseHeadersAssertion, ResponseHeadersAssertion,
RegexExtractor, JSONPostProcessor, XPath2Extractor, DubboSample, RegexExtractor, JSONPostProcessor, XPath2Extractor, DubboSample, JSONPathAssertion,
} from "./JMX"; } from "./JMX";
export const uuid = function () { export const uuid = function () {
@ -47,6 +47,7 @@ export const BODY_FORMAT = {
export const ASSERTION_TYPE = { export const ASSERTION_TYPE = {
TEXT: "Text", TEXT: "Text",
REGEX: "Regex", REGEX: "Regex",
JSON_PATH: "JSON",
DURATION: "Duration" DURATION: "Duration"
} }
@ -514,10 +515,11 @@ export class Assertions extends BaseConfig {
super(); super();
this.text = []; this.text = [];
this.regex = []; this.regex = [];
this.jsonPath = [];
this.duration = undefined; this.duration = undefined;
this.set(options); this.set(options);
this.sets({text: Text, regex: Regex}, options); this.sets({text: Text, regex: Regex, jsonPath: JSONPath}, options);
} }
initOptions(options) { initOptions(options) {
@ -560,6 +562,21 @@ export class Regex extends AssertionType {
} }
} }
export class JSONPath extends AssertionType {
constructor(options) {
super(ASSERTION_TYPE.JSON_PATH);
this.expression = undefined;
this.expect = undefined;
this.description = undefined;
this.set(options);
}
isValid() {
return !!this.expression;
}
}
export class Duration extends AssertionType { export class Duration extends AssertionType {
constructor(options) { constructor(options) {
super(ASSERTION_TYPE.DURATION); super(ASSERTION_TYPE.DURATION);
@ -886,7 +903,13 @@ class JMXGenerator {
let assertions = request.assertions; let assertions = request.assertions;
if (assertions.regex.length > 0) { if (assertions.regex.length > 0) {
assertions.regex.filter(this.filter).forEach(regex => { assertions.regex.filter(this.filter).forEach(regex => {
httpSamplerProxy.put(this.getAssertion(regex)); httpSamplerProxy.put(this.getResponseAssertion(regex));
})
}
if (assertions.jsonPath.length > 0) {
assertions.jsonPath.filter(this.filter).forEach(item => {
httpSamplerProxy.put(this.getJSONPathAssertion(item));
}) })
} }
@ -896,7 +919,12 @@ class JMXGenerator {
} }
} }
getAssertion(regex) { getJSONPathAssertion(jsonPath) {
let name = jsonPath.description;
return new JSONPathAssertion(name, jsonPath);
}
getResponseAssertion(regex) {
let name = regex.description; let name = regex.description;
let type = JMX_ASSERTION_CONDITION.CONTAINS; // 固定用Match自己写正则 let type = JMX_ASSERTION_CONDITION.CONTAINS; // 固定用Match自己写正则
let value = regex.expression; let value = regex.expression;

View File

@ -404,6 +404,7 @@ export default {
start_with: "Start with", start_with: "Start with",
end_with: "End With", end_with: "End With",
value: "Value", value: "Value",
expect: "Expect Value",
expression: "Expression", expression: "Expression",
response_in_time: "Response in time", response_in_time: "Response in time",
}, },

View File

@ -402,6 +402,7 @@ export default {
start_with: "以...开始", start_with: "以...开始",
end_with: "以...结束", end_with: "以...结束",
value: "值", value: "值",
expect: "期望值",
expression: "Perl型正则表达式", expression: "Perl型正则表达式",
response_in_time: "响应时间在...毫秒以内", response_in_time: "响应时间在...毫秒以内",
}, },

View File

@ -403,6 +403,7 @@ export default {
start_with: "以…開始", start_with: "以…開始",
end_with: "以…結束", end_with: "以…結束",
value: "值", value: "值",
expect: "期望值",
expression: "Perl型規則運算式", expression: "Perl型規則運算式",
response_in_time: "回應時間在…毫秒以內", response_in_time: "回應時間在…毫秒以內",
}, },