fix(接口测试): 脚本断言示例代码,执行报错

--bug=1047055 --user=陈建星 【接口测试】接口场景添加脚本断言,获取到的实际值和期望值相等,但仍断言失败 https://www.tapd.cn/55049933/s/1585799
This commit is contained in:
AgAngle 2024-09-25 14:45:48 +08:00 committed by 刘瑞斌
parent 074c0d66cf
commit 4eb40c5224
1 changed files with 45 additions and 34 deletions

View File

@ -534,17 +534,15 @@ log.info("Response Data: " + responseData);
// 你可以进一步处理响应数据 // 你可以进一步处理响应数据
// 例如:保存响应数据到变量 // 例如:保存响应数据到变量
vars.put("responseData", responseData);`, vars.put("responseData", responseData);`,
assertion: assertion: `// 获取响应状态码
'// 获取响应状态码\n' + String responseCode = prev.getResponseCode();
'int responseCode = prev.getResponseCode();\n' + // 设置期望的状态码
'// 设置期望的状态码\n' + String expectedCode = "200";
'int expectedCode = 200;\n' + // 断言失败条件
'// 断言失败条件\n' + if (!responseCode.equals(expectedCode)) {
'if (responseCode != expectedCode) {\n' + Failure = true;
' AssertionResult.setFailure(true);\n' + FailureMessage = "Expected response code: " + expectedCode + ", but got: " + responseCode;
// eslint-disable-next-line no-template-curly-in-string }`,
' AssertionResult.setFailureMessage("Expected response code: ${expectedCode}, but got: ${responseCode}")\n' +
'}',
scenario: `import java.net.URI; scenario: `import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
@ -576,6 +574,22 @@ client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
return null; return null;
});`, });`,
}; };
export const BeanShellJSR233ScriptExampleMap = {
preOperation: BeanShellScriptExampleMap.preOperation,
postOperation: BeanShellScriptExampleMap.postOperation,
assertion: `// 获取响应状态码
String responseCode = prev.getResponseCode();
// 设置期望的状态码
String expectedCode = "200";
// 断言失败条件
if (!responseCode.equals(expectedCode)) {
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("Expected response code: " + expectedCode + ", but got: " + responseCode);
}`,
scenario: BeanShellScriptExampleMap.scenario,
};
export interface ScriptExampleMap { export interface ScriptExampleMap {
[key: string]: { [key: string]: {
preOperation: string; preOperation: string;
@ -614,17 +628,16 @@ vars.put("variable_name", "variable_value");
// 你可以进一步处理响应数据 // 你可以进一步处理响应数据
// 例如:保存响应数据到变量 // 例如:保存响应数据到变量
vars.put("responseData", responseData);`, vars.put("responseData", responseData);`,
assertion: assertion: `// 获取响应状态码
'// 获取响应状态码\n' + var responseCode = prev.getResponseCode();
'var responseCode = prev.getResponseCode();\n' + // 设置期望的状态码
'// 设置期望的状态码\n' + var expectedCode = 200;
'var expectedCode = 200;\n' + // 断言失败条件
'// 断言失败条件\n' + if (responseCode != expectedCode) {
'if (responseCode != expectedCode) {\n' + AssertionResult.setFailure(true);
' AssertionResult.setFailure(true);\n' + AssertionResult.setFailureMessage(\`Expected response code: \${expectedCode}, but got: \${responseCode}\`)
// eslint-disable-next-line no-template-curly-in-string }
' AssertionResult.setFailureMessage(`Expected response code: ${expectedCode}, but got: ${responseCode}`)\n' + `,
'}\n',
scenario: `// 导入必要的 Java 类 scenario: `// 导入必要的 Java 类
var URL = java.net.URL; var URL = java.net.URL;
var HttpURLConnection = java.net.HttpURLConnection; var HttpURLConnection = java.net.HttpURLConnection;
@ -725,7 +738,7 @@ with urllib.request.urlopen(request) as response:
log.info(f"Status Code: {status_code}")`, log.info(f"Status Code: {status_code}")`,
}, },
[LanguageEnum.BEANSHELL]: BeanShellScriptExampleMap, [LanguageEnum.BEANSHELL]: BeanShellScriptExampleMap,
[LanguageEnum.BEANSHELL_JSR233]: BeanShellScriptExampleMap, [LanguageEnum.BEANSHELL_JSR233]: BeanShellJSR233ScriptExampleMap,
[LanguageEnum.GROOVY]: { [LanguageEnum.GROOVY]: {
preOperation: `// 生成或获取 token preOperation: `// 生成或获取 token
def token = "Bearer " + "MeterSphere 123456" def token = "Bearer " + "MeterSphere 123456"
@ -749,17 +762,15 @@ vars.put("variable_name", "variable_value");
// 你可以进一步处理响应数据 // 你可以进一步处理响应数据
// 例如:保存响应数据到变量 // 例如:保存响应数据到变量
vars.put("responseData", responseData);`, vars.put("responseData", responseData);`,
assertion: assertion: `// 获取响应状态码
'// 获取响应状态码\n' + def responseCode = prev.getResponseCode()
'def responseCode = prev.getResponseCode()\n' + // 设置期望的状态码
'// 设置期望的状态码\n' + def expectedCode = "200"
'def expectedCode = 200\n' + // 断言失败条件
'// 断言失败条件\n' + if (responseCode != expectedCode) {
'if (responseCode != expectedCode) {\n' + AssertionResult.setFailure(true)
' AssertionResult.setFailure(true)\n' + AssertionResult.setFailureMessage("Expected response code: \${expectedCode}, but got: \${responseCode}")
// eslint-disable-next-line no-template-curly-in-string }`,
' AssertionResult.setFailureMessage("Expected response code: ${expectedCode}, but got: ${responseCode}")\n' +
'}',
scenario: `import org.apache.http.client.methods.CloseableHttpResponse scenario: `import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpPost import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.CloseableHttpClient