1.當(dāng)只需要校驗(yàn)response而不需要校驗(yàn)返回code時(shí),響應(yīng)斷言里勾選“ignore statue”即可外潜。
2.put請(qǐng)求治唤,需要把路徑和參數(shù)全都放在“路徑”里钝计,參數(shù)之間用&隔開酿炸。eg:api/v1/example?appVer=6&DeviceId={token}&nick=lily
3.要把參數(shù)放在body data瘫絮,必須添加信息頭管理器,content-type=application/json填硕。
4.返回401 nauthorized麦萤。
前面登錄接口肯定返回了一個(gè)類似token的值,把這個(gè)值加上Bearer 作為字段Authorization的值添加到信息頭管理器內(nèi)即可扁眯。如:
5.提取返回值壮莹,優(yōu)先考慮json Extractor,然后再考慮正則表達(dá)式姻檀。json Extractor語法為.data[0].token。
6.在jmeter內(nèi)施敢,利用BeanShell Sampler做MD5加密:
MD5算法文件生成jar包放到j(luò)meter的lib/ext文件夾下周荐,然后在jmeter內(nèi)測(cè)試計(jì)劃下add directory or jar to classpath點(diǎn)擊添加把jar文件路徑加上;最后執(zhí)行接口后僵娃,生成的加密字符串可通過輸出變量myMD5調(diào)用概作。
MD5算法:
package com.md5;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author mapapa
*
*/
public class TestMd5 {
static protected char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
static protected MessageDigest messagedigest=null;
public static byte[] addBytes(byte[] bytes1,byte[] bytes2) {
byte[] newByte=new byte[bytes1.length+bytes2.length];
System.arraycopy(bytes1, 0, newByte, 0, bytes1.length);
System.arraycopy(bytes2, 0, newByte,bytes1.length,bytes2.length);
return newByte;
}
public static String getMD5String(byte[] bytes) {
try {
messagedigest=MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
messagedigest.update(bytes);
return bufferToHex(messagedigest.digest());
}
public static String getMD5String(String md5) throws NoSuchAlgorithmException{
byte[] bytes = md5.getBytes();
messagedigest=MessageDigest.getInstance("MD5");
messagedigest.update(bytes);
return bufferToHex(messagedigest.digest());
}
private static String bufferToHex(byte bytes[]){
return bufferToHex(bytes,0,bytes.length);
}
private static String bufferToHex(byte bytes[],int m,int n){
StringBuffer sb=new StringBuffer(2*n);
int k=m+n;
for (int i = m; i < k; i++) {
char c0=hexDigits[(bytes[i] & 0xf0)>>4];
char c1=hexDigits[bytes[i] & 0xf];
sb.append(c0);
sb.append(c1);
}
return sb.toString();
}
}
BeanShell代碼:
import com.md5.TestMd5;
String tests = "${token}${timestamp}aaabbb";
byte[] bytes = tests.getBytes();
String md5String = TestMd5.getMD5String(bytes);
vars.put("myMD5",md5String)
7.BeanShell Sampler可能存在緩存,即之前引入的jar文件執(zhí)行成功后默怨,刪除該jar文件導(dǎo)入一個(gè)新的修改過的jar讯榕,執(zhí)行的仍有可能是之前的jar,需要將所有路徑下先前的jar刪除匙睹,重啟jmeter愚屁,再引入新的jar。
8.JDBC Request痕檬,sql語句里霎槐,可以{variable name}會(huì)拿不到值丘跌,可能的原因是前一個(gè)sql語句返回的查詢結(jié)果并不是嚴(yán)格按照variable name = XX返回,而是如下:
user_id_#=1
user_id_1=16728
此時(shí)要引用user_id唁桩,則需要在語句里寫成${user_id_1}闭树。
9.重定向接口