fix(接口测试): 增加XPath2Extractor提取解决xml格式和html格式提取问题

--bug=1013099 --user=赵勇 [ 接口测试]-接口场景-添加xpath提取后-不执行 https://www.tapd.cn/55049933/s/1156906
This commit is contained in:
fit2-zhao 2022-05-12 15:16:47 +08:00 committed by TIanyang
parent cbccf98a92
commit fc0856970d
7 changed files with 63 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.extractor.JSR223PostProcessor;
import org.apache.jmeter.extractor.RegexExtractor;
import org.apache.jmeter.extractor.XPath2Extractor;
import org.apache.jmeter.extractor.XPathExtractor;
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.save.SaveService;
@ -25,6 +26,7 @@ import java.util.StringJoiner;
@JSONType(typeName = "Extract")
public class MsExtract extends MsTestElement {
private String clazzName = MsExtract.class.getCanonicalName();
private String xpathType;
private List<MsExtractRegex> regex;
private List<MsExtractJSONPath> json;
@ -50,9 +52,15 @@ public class MsExtract extends MsTestElement {
);
}
if (CollectionUtils.isNotEmpty(this.getXpath())) {
this.getXpath().stream().filter(MsExtractCommon::isValid).forEach(extractXPath ->
samplerHashTree.add(xPathExtractor(extractXPath, extract))
);
if (StringUtils.isEmpty(this.xpathType) || StringUtils.equalsAnyIgnoreCase(this.xpathType, "html")) {
this.getXpath().stream().filter(MsExtractCommon::isValid).forEach(extractXPath ->
samplerHashTree.add(xpathExtractor(extractXPath, extract))
);
} else {
this.getXpath().stream().filter(MsExtractCommon::isValid).forEach(extractXPath ->
samplerHashTree.add(xpath2Extractor(extractXPath, extract))
);
}
}
if (CollectionUtils.isNotEmpty(this.getJson())) {
this.getJson().stream().filter(MsExtractCommon::isValid).forEach(extractJSONPath ->
@ -94,7 +102,14 @@ public class MsExtract extends MsTestElement {
return extractor;
}
private XPathExtractor xPathExtractor(MsExtractXPath extractXPath, StringJoiner extract) {
/**
* 处理html
*
* @param extractXPath
* @param extract
* @return
*/
private XPathExtractor xpathExtractor(MsExtractXPath extractXPath, StringJoiner extract) {
XPathExtractor extractor = new XPathExtractor();
extractor.setEnabled(this.isEnable());
extractor.setTolerant(true);
@ -113,6 +128,31 @@ public class MsExtract extends MsTestElement {
return extractor;
}
/**
* 处理xml
*
* @param extractXPath
* @param extract
* @return
*/
private XPath2Extractor xpath2Extractor(MsExtractXPath extractXPath, StringJoiner extract) {
XPath2Extractor extractor = new XPath2Extractor();
extractor.setEnabled(this.isEnable());
extractor.setName(StringUtils.isNotEmpty(extractXPath.getVariable()) ? extractXPath.getVariable() : this.getName());
if (StringUtils.isEmpty(extractor.getName())) {
extractor.setName("XPath2Extractor");
}
extractor.setProperty(TestElement.TEST_CLASS, XPathExtractor.class.getName());
extractor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("XPath2ExtractorGui"));
extractor.setRefName(extractXPath.getVariable());
extractor.setXPathQuery(extractXPath.getExpression());
if (extractXPath.isMultipleMatching()) {
extractor.setMatchNumber(-1);
}
extract.add(extractor.getRefName());
return extractor;
}
private JSONPostProcessor jsonPostProcessor(MsExtractJSONPath extractJSONPath, StringJoiner extract) {
JSONPostProcessor extractor = new JSONPostProcessor();
extractor.setEnabled(this.isEnable());

View File

@ -98,6 +98,11 @@ export default {
default: false,
},
},
created(){
if(!this.extract.xpathType){
this.extract.xpathType = 'html';
}
},
data() {
return {
options: EXTRACT_TYPE,

View File

@ -23,6 +23,14 @@
<div class="extract-item-editing xpath" v-if="extract.xpath.length > 0">
<div>
XPath
<el-select v-model="extract.xpathType" size="mini" v-loading="loading" @change="reload">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
</el-select>
<el-tooltip placement="top">
<div slot="content">{{ $t('api_test.request.assertions.xpath_info') }}</div>
<i class="el-icon-question" style="cursor: pointer"/>
</el-tooltip>
</div>
<div class="regex-item" v-for="(xpath, index) in extract.xpath" :key="index">
<ms-api-extract-common :if-from-variable-advance="ifFromVariableAdvance" :is-read-only="isReadOnly" :extract-type="type.XPATH" :list="extract.xpath" :common="xpath"
@ -74,7 +82,8 @@
data() {
return {
type: EXTRACT_TYPE,
loading: false
loading: false,
options:[{value:'html',label:'html'},{value:'xml',label:'xml'}]
}
}
}

View File

@ -966,6 +966,7 @@ export class Extract extends BaseConfig {
super();
this.resourceId = uuid();
this.type = "Extract";
this.xpathType = 'html';
this.regex = [];
this.json = [];
this.xpath = [];
@ -996,7 +997,6 @@ export class ExtractCommon extends ExtractType {
this.expression = undefined;
this.description = undefined;
this.multipleMatching = undefined;
this.set(options);
}

View File

@ -1616,6 +1616,7 @@ export default {
debug_first: "First, debug to get the response",
suggest_tip: "Click the note to add the JSONPath assertion",
regex_info: 'Special characters "$ () * +. [] \\ ^ {} |" need to be escaped as "\\ "+"special characters", such as "\\$"',
xpath_info: 'Select the extraction method according to the content format of the request response',
regular_match: 'Regular match',
none: 'Do not verify []',
value_eq: 'Value-equal to [value=]',

View File

@ -1621,6 +1621,7 @@ export default {
set_failure_msg: "设置失败消息",
suggest_tip: "点击便签添加JSONPath断言",
regex_info: '特殊字符"$ ( ) * + . [ ] \\ ^ { } |"需转义为"\\ "+"特殊字符",如"\\$"',
xpath_info: '根据请求响应内容格式,选择提取方式',
regular_match: '正则匹配',
none: '不校验[]',
value_eq: '值-等于[value=]',

View File

@ -1621,6 +1621,7 @@ export default {
set_failure_msg: "設置失敗消息",
suggest_tip: "點擊便簽添加JSONPath斷言",
regex_info: '特殊字符"$ ( ) * + . [ ] \\ ^ { } |"需轉義為"\\ "+"特殊字符",如"\\$"',
xpath_info: '根據請求響應內容格式,選擇提取方式',
regular_match: '正則匹配',
none: '不校驗[]',
value_eq: '值-等於[value=]',