接口:特殊的URL,通常来说是它给我们提供(操作)数据的.不要用标准化的思想去测试接口,否则碰到特殊接口会有问题,比如说restful api,有的时候put去添加,delete去查询。 所以需要看开发的示例,来决定我们应该怎么去发送请求。
接口测试: 按照规定好的规则去执行请求。swagger接口文档。 本质上就是发送请求验证结果,发现错误
四要素:
URL地址 -- 当前运行这个程序的电脑ip(host) 和 端口号 (port)+ URL 地址
请求方式 -- GET POST PUT DELETE ...
请求参数
返回值 (200 success status验证,还需要验证body的数据,不能只验证statuscode)
如果还是不通? -- 400
请求头: 规定了当前参数的格式 application/json form text (没有加请求头导致的400,是的后端没办法解析请求参数)

当接口需要登陆后的场景时,需要进行授权信息的添加 - token (token 会过期失效)
后一个接口会用到前一个接口的最新数据,怎么处理接口关联。
从响应body里获取response里面的token值,设置一个变量来存token, 后面的请求参数添加token的信息
所有的json数据都可以用jsonpath来获取。每次调用 JsonPath.read(…) 都会对文档进行解析。为了避免次次这个问题,可以先解析 json。
eg: List<String> authors = JsonPath.read(jsonStr, "$.store.book[*].author");
List<String> author = JsonPath.parse(jsonStr).read("$.store.book[*].author");

加密接口的测试:只能用密文发送,而不是明文的接口,传参必须加密
所以有密钥的加密解密问题,java中我们用AES 算法进行加密。在这个包里面

import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;
import java.security.*;
import java.util.Base64;
public class AESEncryption {
private static final String AES_ALGORITHM = "AES";
private static final String AES_CBC_PADDING = "AES/CBC/PKCS5Padding";
private static final String AES_GCM_NO_PADDING = "AES/GCM/NoPadding";
private static final int GCM_TAG_LENGTH = 128; // 位
private static final int GCM_IV_LENGTH = 12; // 字节
private static final int IV_LENGTH = 16; // AES-CBC IV 长度
// 生成 AES 密钥
public static String generateKey(int keySize) throws NoSuchAlgorithmException {
if (keySize != 128 && keySize != 192 && keySize != 256) {
throw new IllegalArgumentException("Key size must be 128/192/256 bits");
}
KeyGenerator keyGen = KeyGenerator.getInstance(AES_ALGORITHM);
keyGen.init(keySize);
SecretKey secretKey = keyGen.generateKey();
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
// AES-CBC 加密
public static String encryptCBC(String plaintext, String base64Key) throws Exception {
SecretKey secretKey = getSecretKeyFromBase64(base64Key);
Cipher cipher = Cipher.getInstance(AES_CBC_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] iv = cipher.getIV();
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
// 合并 IV 和密文
ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + encryptedBytes.length);
byteBuffer.put(iv);
byteBuffer.put(encryptedBytes);
byte[] combined = byteBuffer.array();
return Base64.getEncoder().encodeToString(combined);
}
// AES-CBC 解密
public static String decryptCBC(String ciphertext, String base64Key) throws Exception {
SecretKey secretKey = getSecretKeyFromBase64(base64Key);
byte[] combined = Base64.getDecoder().decode(ciphertext);
// 分离 IV 和密文
byte[] iv = new byte[IV_LENGTH];
byte[] encryptedBytes = new byte[combined.length - IV_LENGTH];
System.arraycopy(combined, 0, iv, 0, IV_LENGTH);
System.arraycopy(combined, IV_LENGTH, encryptedBytes, 0, encryptedBytes.length);
Cipher cipher = Cipher.getInstance(AES_CBC_PADDING);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
// AES-GCM 加密(推荐)
public static String encryptGCM(String plaintext, String base64Key) throws Exception {
SecretKey secretKey = getSecretKeyFromBase64(base64Key);
SecureRandom secureRandom = new SecureRandom();
byte[] iv = new byte[GCM_IV_LENGTH];
secureRandom.nextBytes(iv);
Cipher cipher = Cipher.getInstance(AES_GCM_NO_PADDING);
GCMParameterSpec parameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
// 合并 IV 和密文
ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + encryptedBytes.length);
byteBuffer.put(iv);
byteBuffer.put(encryptedBytes);
return Base64.getEncoder().encodeToString(byteBuffer.array());
}
// AES-GCM 解密(推荐)
public static String decryptGCM(String ciphertext, String base64Key) throws Exception {
SecretKey secretKey = getSecretKeyFromBase64(base64Key);
byte[] combined = Base64.getDecoder().decode(ciphertext);
// 分离 IV 和密文
byte[] iv = new byte[GCM_IV_LENGTH];
byte[] encryptedBytes = new byte[combined.length - GCM_IV_LENGTH];
System.arraycopy(combined, 0, iv, 0, GCM_IV_LENGTH);
System.arraycopy(combined, GCM_IV_LENGTH, encryptedBytes, 0, encryptedBytes.length);
Cipher cipher = Cipher.getInstance(AES_GCM_NO_PADDING);
GCMParameterSpec parameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
private static SecretKey getSecretKeyFromBase64(String base64Key) {
byte[] keyBytes = Base64.getDecoder().decode(base64Key);
return new SecretKeySpec(keyBytes, AES_ALGORITHM);
}
// 主函数示例
public static void main(String[] args) {
try {
// 生成密钥(128/192/256 位)
String key = generateKey(256);
System.out.println("Generated Key: " + key);
String plaintext = "Hello, AES Encryption!";
// 使用 CBC 模式加密/解密
String encryptedCBC = encryptCBC(plaintext, key);
String decryptedCBC = decryptCBC(encryptedCBC, key);
System.out.println("CBC Encrypted: " + encryptedCBC);
System.out.println("CBC Decrypted: " + decryptedCBC);
// 使用 GCM 模式加密/解密(推荐)
String encryptedGCM = encryptGCM(plaintext, key);
String decryptedGCM = decryptGCM(encryptedGCM, key);
System.out.println("GCM Encrypted: " + encryptedGCM);
System.out.println("GCM Decrypted: " + decryptedGCM);
} catch (Exception e) {
e.printStackTrace();
}
}
}
接口自动化框架,数据拆分,参数化,用不同的一段代码,对每个接口进行测试,拿到response存起来。 对应的参数,存储在常用的文件中去。比如excel文件json文件。

浙公网安备 33010602011771号