feat(接口测试): xpath断言

This commit is contained in:
chenjianxing 2020-11-03 09:36:11 +08:00
parent 870f9f9388
commit b97cf5aeaf
9 changed files with 154 additions and 4 deletions

View File

@ -9,6 +9,7 @@ public class AssertionType {
public final static String JSON_PATH = "JSONPath";
public final static String JSR223 = "JSR223";
public final static String TEXT = "Text";
public final static String XPATH2 = "XPath2";
private String type;
}

View File

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

View File

@ -9,5 +9,6 @@ public class Assertions {
private List<AssertionRegex> regex;
private List<AssertionJsonPath> jsonPath;
private List<AssertionJSR223> jsr223;
private List<AssertionXPath2> xPath2;
private AssertionDuration duration;
}

View File

@ -305,9 +305,11 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
private ResponseAssertionResult getResponseAssertionResult(AssertionResult assertionResult) {
ResponseAssertionResult responseAssertionResult = new ResponseAssertionResult();
responseAssertionResult.setMessage(assertionResult.getFailureMessage());
responseAssertionResult.setName(assertionResult.getName());
responseAssertionResult.setPass(!assertionResult.isFailure() && !assertionResult.isError());
if (!responseAssertionResult.isPass()) {
responseAssertionResult.setMessage(assertionResult.getFailureMessage());
}
return responseAssertionResult;
}

View File

@ -0,0 +1,72 @@
<template>
<div>
<el-row :gutter="10" type="flex" justify="space-between" align="middle">
<el-col>
<el-input :disabled="isReadOnly" v-model="xPath2.expression" maxlength="200" size="small" show-word-limit
:placeholder="$t('api_test.request.extract.xpath_expression')"/>
</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>
{{ $t('api_test.request.assertions.add') }}
</el-button>
</el-col>
</el-row>
</div>
</template>
<script>
import {XPath2} from "../../model/ScenarioModel";
export default {
name: "MsApiAssertionXPath2",
props: {
xPath2: {
type: XPath2,
default: () => {
return new XPath2();
}
},
edit: {
type: Boolean,
default: false
},
index: Number,
list: Array,
callback: Function,
isReadOnly: {
type: Boolean,
default: false
}
},
methods: {
add: function () {
this.list.push(this.getXPath2());
this.callback();
},
remove: function () {
this.list.splice(this.index, 1);
},
getXPath2() {
return new XPath2(this.xPath2);
},
}
}
</script>
<style scoped>
.assertion-select {
width: 250px;
}
.assertion-item {
width: 100%;
}
.assertion-btn {
text-align: center;
width: 60px;
}
</style>

View File

@ -8,6 +8,7 @@
<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="'XPath'" :value="options.XPATH2"/>
<el-option :label="$t('api_test.request.assertions.response_time')" :value="options.DURATION"/>
<el-option :label="$t('api_test.request.assertions.jsr223')" :value="options.JSR223"/>
</el-select>
@ -16,6 +17,7 @@
<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-x-path2 :is-read-only="isReadOnly" :list="assertions.xPath2" v-if="type === options.XPATH2" :callback="after"/>
<ms-api-assertion-duration :is-read-only="isReadOnly" v-model="time" :duration="assertions.duration"
v-if="type === options.DURATION" :callback="after"/>
<ms-api-assertion-jsr223 :is-read-only="isReadOnly" :list="assertions.jsr223" v-if="type === options.JSR223" :callback="after"/>
@ -52,11 +54,13 @@
import MsApiAssertionJsonPath from "./ApiAssertionJsonPath";
import MsApiAssertionJsr223 from "@/business/components/api/test/components/assertion/ApiAssertionJsr223";
import MsApiJsonpathSuggestList from "./ApiJsonpathSuggestList";
import MsApiAssertionXPath2 from "./ApiAssertionXPath2";
export default {
name: "MsApiAssertions",
components: {
MsApiAssertionXPath2,
MsApiAssertionJsr223,
MsApiJsonpathSuggestList,
MsApiAssertionJsonPath,

View File

@ -20,6 +20,16 @@
</div>
</div>
<div class="assertion-item-editing x_path" v-if="assertions.xPath2.length > 0">
<div>
{{ 'XPath' }}
</div>
<div class="regex-item" v-for="(xPath, index) in assertions.xPath2" :key="index">
<ms-api-assertion-x-path2 :is-read-only="isReadOnly" :list="assertions.xPath2"
:x-path2="xPath" :edit="true" :index="index"/>
</div>
</div>
<div class="assertion-item-editing jsr223" v-if="assertions.jsr223.length > 0">
<div>
{{ $t("api_test.request.assertions.script") }}
@ -47,11 +57,14 @@ import MsApiAssertionDuration from "./ApiAssertionDuration";
import {Assertions} from "../../model/ScenarioModel";
import MsApiAssertionJsonPath from "./ApiAssertionJsonPath";
import MsApiAssertionJsr223 from "@/business/components/api/test/components/assertion/ApiAssertionJsr223";
import MsApiAssertionXPath2 from "./ApiAssertionXPath2";
export default {
name: "MsApiAssertionsEdit",
components: {MsApiAssertionJsr223, MsApiAssertionJsonPath, MsApiAssertionDuration, MsApiAssertionRegex},
components: {
MsApiAssertionXPath2,
MsApiAssertionJsr223, MsApiAssertionJsonPath, MsApiAssertionDuration, MsApiAssertionRegex},
props: {
assertions: Assertions,
@ -92,6 +105,10 @@ export default {
border-left: 2px solid #1FDD02;
}
.assertion-item-editing.x_path {
border-left: 2px solid #fca130;
}
.regex-item {
margin-top: 10px;
}

View File

@ -439,6 +439,16 @@ export class JSONPathAssertion extends DefaultTestElement {
}
}
export class XPath2Assertion extends DefaultTestElement {
constructor(testName, xPath) {
super('XPath2Assertion', 'XPath2AssertionGui', 'XPath2Assertion', testName);
this.xPath = xPath || {};
this.stringProp('XPath.xpath', this.xPath.expression);
this.stringProp('XPath.namespace');
this.boolProp('XPath.negate', false);
}
}
export class ResponseCodeAssertion extends ResponseAssertion {
constructor(testName, type, value, assumeSuccess, message) {
let assertion = {

View File

@ -25,7 +25,7 @@ import {
ThreadGroup,
XPath2Extractor,
IfController as JMXIfController,
ConstantTimer as JMXConstantTimer, TCPSampler, JSR223Assertion,
ConstantTimer as JMXConstantTimer, TCPSampler, JSR223Assertion, XPath2Assertion,
} from "./JMX";
import Mock from "mockjs";
import {funcFilters} from "@/common/js/func-filter";
@ -96,6 +96,7 @@ export const ASSERTION_TYPE = {
JSON_PATH: "JSON",
DURATION: "Duration",
JSR223: "JSR223",
XPATH2: "XPath2",
}
export const ASSERTION_REGEX_SUBJECT = {
@ -741,10 +742,11 @@ export class Assertions extends BaseConfig {
this.regex = [];
this.jsonPath = [];
this.jsr223 = [];
this.xPath2 = [];
this.duration = undefined;
this.set(options);
this.sets({text: Text, regex: Regex, jsonPath: JSONPath, jsr223: AssertionJSR223}, options);
this.sets({text: Text, regex: Regex, jsonPath: JSONPath, jsr223: AssertionJSR223, xPath2: XPath2}, options);
}
initOptions(options) {
@ -826,6 +828,23 @@ export class JSONPath extends AssertionType {
}
}
export class XPath2 extends AssertionType {
constructor(options) {
super(ASSERTION_TYPE.XPATH2);
this.expression = undefined;
this.description = undefined;
this.set(options);
}
// setJSONPathDescription() {
// this.description = this.expression + " expect: " + (this.expect ? this.expect : '');
// }
isValid() {
return !!this.expression;
}
}
export class Duration extends AssertionType {
constructor(options) {
super(ASSERTION_TYPE.DURATION);
@ -1431,6 +1450,12 @@ class JMXGenerator {
})
}
if (assertions.xPath2.length > 0) {
assertions.xPath2.filter(this.filter).forEach(item => {
httpSamplerProxy.put(this.getXpathAssertion(item));
})
}
if (assertions.jsr223.length > 0) {
assertions.jsr223.filter(this.filter).forEach(item => {
httpSamplerProxy.put(this.getJSR223Assertion(item));
@ -1453,6 +1478,11 @@ class JMXGenerator {
return new JSR223Assertion(name, item);
}
getXpathAssertion(item) {
let name = item.expression;
return new XPath2Assertion(name, item);
}
getResponseAssertion(regex) {
let name = regex.description;
let type = JMX_ASSERTION_CONDITION.CONTAINS; // 固定用Match自己写正则