diff --git a/test-common/pom.xml b/test-common/pom.xml
index 281613a..9efeb62 100644
--- a/test-common/pom.xml
+++ b/test-common/pom.xml
@@ -146,6 +146,11 @@
gson
2.10.1
+
+ com.jayway.jsonpath
+ json-path
+ 2.8.0
+
diff --git a/test-common/src/main/java/com/test/common/core/domain/model/JmeterRequest.java b/test-common/src/main/java/com/test/common/core/domain/model/JmeterRequest.java
new file mode 100644
index 0000000..61a8a9a
--- /dev/null
+++ b/test-common/src/main/java/com/test/common/core/domain/model/JmeterRequest.java
@@ -0,0 +1,112 @@
+package com.test.common.core.domain.model;
+
+/**
+ * @author liangdaliang
+ * @Description:Jmeter调用参数
+ * @date 2025-02-19 10:55
+ */
+public class JmeterRequest {
+ /**
+ * 用例步骤id
+ */
+ private Long id;
+
+ /**
+ * 请求完整url
+ */
+ private String url;
+
+ /**
+ * 请求端口号
+ */
+ private int port;
+
+ /**
+ * 请求方式
+ */
+ private String method;
+
+ /**
+ * 请求body
+ */
+ private String requestBody;
+
+ /**
+ * 请求参数,url?后面的参数
+ */
+ private String requestParams;
+
+ /**
+ * 请求头Json
+ */
+ private String requestHeader;
+
+ /**
+ * jmeter安装home路径
+ */
+ private String jmeterHomePath;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public String getMethod() {
+ return method;
+ }
+
+ public void setMethod(String method) {
+ this.method = method;
+ }
+
+ public String getRequestBody() {
+ return requestBody;
+ }
+
+ public void setRequestBody(String requestBody) {
+ this.requestBody = requestBody;
+ }
+
+ public String getRequestParams() {
+ return requestParams;
+ }
+
+ public void setRequestParams(String requestParams) {
+ this.requestParams = requestParams;
+ }
+
+ public String getRequestHeader() {
+ return requestHeader;
+ }
+
+ public void setRequestHeader(String requestHeader) {
+ this.requestHeader = requestHeader;
+ }
+
+ public String getJmeterHomePath() {
+ return jmeterHomePath;
+ }
+
+ public void setJmeterHomePath(String jmeterHomePath) {
+ this.jmeterHomePath = jmeterHomePath;
+ }
+}
diff --git a/test-common/src/main/java/com/test/common/utils/JMeterUtil.java b/test-common/src/main/java/com/test/common/utils/JMeterUtil.java
index 5df1bac..8f3ab7d 100644
--- a/test-common/src/main/java/com/test/common/utils/JMeterUtil.java
+++ b/test-common/src/main/java/com/test/common/utils/JMeterUtil.java
@@ -2,6 +2,9 @@ package com.test.common.utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
+import com.jayway.jsonpath.JsonPath;
+import com.jayway.jsonpath.PathNotFoundException;
+import com.test.common.core.domain.model.JmeterRequest;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
@@ -54,16 +57,18 @@ public class JMeterUtil {
/**
* 生成jmeter测试执行计划并获取执行结果
- * @param id
- * @param url
- * @param port
- * @param method
- * @param paramJson
- * @param requestHeader
- * @param jmeterHomePath
+ * @param jmeterRequest
* @return
*/
- public static Map getJmeterResult(Long id, String url, int port, String method, String paramJson, String requestHeader, String jmeterHomePath) {
+ public static Map getJmeterResult(JmeterRequest jmeterRequest) {
+ Long id = jmeterRequest.getId();
+ String url = jmeterRequest.getUrl();
+ int port = jmeterRequest.getPort();
+ String method = jmeterRequest.getMethod().toUpperCase();
+ String requestBody = jmeterRequest.getRequestBody();
+ String requestParams = jmeterRequest.getRequestParams();
+ String requestHeader = jmeterRequest.getRequestHeader();
+ String jmeterHomePath = jmeterRequest.getJmeterHomePath();
Map result = null;
try {
// 1. 初始化 JMeter
@@ -83,13 +88,14 @@ public class JMeterUtil {
httpSampler = createPostHttpSampler(
url, // 请求地址
port,
- paramJson // 请求参数
+ requestBody, // 请求体
+ requestParams // 查询参数
);
} else {
httpSampler = createGetHttpSampler(
url, // 请求地址
port,
- paramJson // 请求参数
+ requestParams // 查询参数
);
}
// 5. 创建请求头管理器
@@ -189,11 +195,11 @@ public class JMeterUtil {
* 创建get请求方式
* @param urlString
* @param port
- * @param requestJson
+ * @param requestParams
* @return
* @throws MalformedURLException
*/
- private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, String requestJson) throws MalformedURLException {
+ private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, String requestParams) throws MalformedURLException {
URL url = new URL(urlString);
// 提取域名(不包括端口号)
String domain = url.getHost();
@@ -204,6 +210,9 @@ public class JMeterUtil {
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
httpSampler.setDomain(domain); // 提取域名
+ if (!StringUtils.isEmpty(requestParams)) {
+ path = path + "?" + requestParams;
+ }
httpSampler.setPath(path); // 提取路径
httpSampler.setPort(port);
httpSampler.setMethod("GET");
@@ -211,17 +220,17 @@ public class JMeterUtil {
httpSampler.setUseKeepAlive(true);
// 添加请求参数
- Map parameters = new HashMap<>();
- if (!StringUtils.isEmpty(requestJson)) {
- Gson gson = new Gson();
- // 定义 Map 的类型
- Type type = new TypeToken