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 final static String REGEX = "Regex";
public final static String DURATION = "Duration";
public final static String JSON_PATH = "JSONPath";
public final static String TEXT = "Text";
private String type;

View File

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

View File

@ -1,6 +1,7 @@
package io.metersphere.api.jmeter;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.config.JmeterProperties;
import io.metersphere.i18n.Translator;
import org.apache.jmeter.config.Arguments;
@ -33,6 +34,7 @@ public class JMeterService {
LocalRunner runner = new LocalRunner(testPlan);
runner.run();
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
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">
<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="'JSONPath'" :value="options.JSON_PATH"/>
<el-option :label="$t('api_test.request.assertions.response_time')" :value="options.DURATION"/>
</el-select>
</el-col>
<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-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"
v-if="type === options.DURATION" :callback="after"/>
<el-button v-if="!type" :disabled="true" type="primary" size="small">Add</el-button>
@ -30,11 +32,14 @@
import MsApiAssertionDuration from "./ApiAssertionDuration";
import {ASSERTION_TYPE, Assertions} from "../../model/ScenarioModel";
import MsApiAssertionsEdit from "./ApiAssertionsEdit";
import MsApiAssertionJsonPath from "./ApiAssertionJsonPath";
export default {
name: "MsApiAssertions",
components: {MsApiAssertionsEdit, MsApiAssertionDuration, MsApiAssertionRegex, MsApiAssertionText},
components: {
MsApiAssertionJsonPath,
MsApiAssertionsEdit, MsApiAssertionDuration, MsApiAssertionRegex, MsApiAssertionText},
props: {
assertions: Assertions,

View File

@ -9,6 +9,15 @@
</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>
{{$t("api_test.request.assertions.response_time")}}
@ -23,11 +32,12 @@
import MsApiAssertionRegex from "./ApiAssertionRegex";
import MsApiAssertionDuration from "./ApiAssertionDuration";
import {Assertions} from "../../model/ScenarioModel";
import MsApiAssertionJsonPath from "./ApiAssertionJsonPath";
export default {
name: "MsApiAssertionsEdit",
components: {MsApiAssertionDuration, MsApiAssertionRegex},
components: {MsApiAssertionJsonPath, MsApiAssertionDuration, MsApiAssertionRegex},
props: {
assertions: Assertions,
@ -56,6 +66,10 @@
border-left: 2px solid #7B0274;
}
.assertion-item-editing.json_path {
border-left: 2px solid #44B3D2;
}
.assertion-item-editing.response-time {
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 {
constructor(testName, type, value, message) {
let assertion = {

View File

@ -12,7 +12,7 @@ import {
ResponseCodeAssertion,
ResponseDataAssertion,
ResponseHeadersAssertion,
RegexExtractor, JSONPostProcessor, XPath2Extractor, DubboSample,
RegexExtractor, JSONPostProcessor, XPath2Extractor, DubboSample, JSONPathAssertion,
} from "./JMX";
export const uuid = function () {
@ -47,6 +47,7 @@ export const BODY_FORMAT = {
export const ASSERTION_TYPE = {
TEXT: "Text",
REGEX: "Regex",
JSON_PATH: "JSON",
DURATION: "Duration"
}
@ -514,10 +515,11 @@ export class Assertions extends BaseConfig {
super();
this.text = [];
this.regex = [];
this.jsonPath = [];
this.duration = undefined;
this.set(options);
this.sets({text: Text, regex: Regex}, options);
this.sets({text: Text, regex: Regex, jsonPath: JSONPath}, 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 {
constructor(options) {
super(ASSERTION_TYPE.DURATION);
@ -886,7 +903,13 @@ class JMXGenerator {
let assertions = request.assertions;
if (assertions.regex.length > 0) {
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 type = JMX_ASSERTION_CONDITION.CONTAINS; // 固定用Match自己写正则
let value = regex.expression;

View File

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

View File

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

View File

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