相信一萬行程式碼的理論!
測試框架講完了,今天開始分享一些自己寫過的介面測試專案的程式碼,先分享一個基類的實踐。主要分享看ibase
的介面實現,以及在實際工作中遇到的密碼加密,響應結構體校驗等等功能,還有一個重點就是構造方法和獲取基類物件的static
方法。程式碼選了一個自己現在維護的比較熟悉的一個專案中間一些配置由於沒時間了,放到下期模組類裡面。歡迎各位多提提意見,關注FunTester
交流測試相關。
視訊專題:
- FunTester測試框架視訊講解(序)
- 獲取HTTP請求物件--測試框架視訊講解
- 傳送請求和解析響應—測試框架視訊解讀
- json物件基本操作--視訊講解
- GET請求實踐--測試框架視訊講解
- POST請求實踐--視訊演示
- 如何處理header和cookie--視訊演示
- FunRequest類功能--視訊演示
- 介面測試業務驗證--視訊演示
- JSONArray基本操作--視訊演示
介面測試中業務驗證
gitee地址:gitee.com/fanapi/test…
程式碼
package com.okayqa.studentapd.base;
import com.fun.base.bean.BeanUtil;
import com.fun.base.bean.RequestInfo;
import com.fun.base.interfaces.IBase;
import com.fun.config.HttpClientConstant;
import com.fun.config.SqlConstant;
import com.fun.config.SysInit;
import com.fun.frame.SourceCode;
import com.fun.frame.httpclient.FanLibrary;
import com.okayqa.common.Common;
import com.okayqa.common.RSAUtils;
import com.okayqa.common.Users;
import com.okayqa.studentapd.bean.UserInfoBean;
import com.okayqa.studentapd.profile.Profile;
import com.okayqa.studentapd.profile.UserApi;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
/**
* qa專案base類
*/
public class OkayBase extends SourceCode implements IBase {
private static Logger logger = LoggerFactory.getLogger(OkayBase.class);
private static OkayBase base;
static {
SqlConstant.REQUEST_TABLE = Common.SQL_REQUEST;
SqlConstant.flag = Common.SQL_KEY;
SqlConstant.PERFORMANCE_TABLE = Common.SQL_PERFORMANCE;
if (FanLibrary.getiBase() == null) FanLibrary.setiBase(new OkayBase());
}
public static String HOST = Profile.HOST;
private void getPublicKey() {
JSONObject response = getPostResponse(Profile.PUBLICK_KEY_API, getParams());
if (isRight(response)) {
logger.info("public_key:{}", response.getJSONObject("data").getString("key"));
}
}
/**
* 登入響應
*/
JSONObject loginResponse;
UserInfoBean userInfoBean = new UserInfoBean();
/**
* 獲取物件方法
* <p>
* 暫未進行使用者管理,同意使用單例
* </p>
*
* @return
*/
public static OkayBase getBase() {
if (base == null) base = new OkayBase(0);
return base;
}
public static OkayBase getBase(int i) {
return new OkayBase(i);
}
protected int uid;
protected String token;
protected String uname;
protected String pwd;
protected JSONObject cookies = new JSONObject();
@Override
public void login() {
String url = UserApi.LOGIN;
JSONObject params = getJson("uname=" + uname, "pwd=" + getPassword(pwd));
loginResponse = getPostResponse(url, params);
if (isRight(loginResponse)) {
JSONObject data = loginResponse.getJSONObject("data");
uid = data.getInteger("uid");
token = data.getString("token");
uname = data.getString("uname");
BeanUtil.copyProperties(userInfoBean, data);
logger.info("使用者:{},登入成功!", uname);
} else {
logger.info("使用者:{},登入失敗!", uname);
output(loginResponse);
}
}
/**
* 預設使用使用者id的後六位作為密碼
*
* @param uname
*/
public OkayBase(String uname) {
this(uname, uname.substring(uname.length() - 6));
}
public OkayBase(String uname, String pwd) {
this.uname = uname;
this.pwd = pwd;
login();
}
public OkayBase(int i) {
this(Users.getStuUser(i), Users.getStuUser(i));
}
public OkayBase() {
}
public static String getPassword(String pwd) {
return RSAUtils.getPassword(pwd);
}
public OkayBase(OkayBase okayBase) {
this.pwd = okayBase.pwd;
this.uid = okayBase.uid;
this.uname = okayBase.uname;
this.token = okayBase.token;
this.userInfoBean = okayBase.userInfoBean;
this.cookies = okayBase.cookies;
}
public JSONObject getParams() {
JSONObject json = getJson("uid=" + uid, "token=" + token, "uname=" + uname);
json.putAll(Profile.COMMON_PARAMS);
return json;
}
@Override
public void init(JSONObject jsonObject) {
}
@Override
public void recordRequest() {
}
@Override
public HttpRequestBase getRequest() {
return null;
}
public JSONObject getLoginResponse() {
return loginResponse;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getUid() {
return uid;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getUname() {
return uname;
}
public UserInfoBean getUserInfoBean() {
return userInfoBean;
}
@Override
public HttpGet getGet(String s) {
return FanLibrary.getHttpGet(HOST + s);
}
@Override
public HttpGet getGet(String s, JSONObject jsonObject) {
return FanLibrary.getHttpGet(HOST + s, jsonObject);
}
@Override
public HttpPost getPost(String s) {
return FanLibrary.getHttpPost(HOST + s);
}
@Override
public HttpPost getPost(String s, JSONObject jsonObject) {
return FanLibrary.getHttpPost(HOST + s, jsonObject.toString());
}
@Override
public HttpPost getPost(String s, JSONObject jsonObject, File file) {
return FanLibrary.getHttpPost(HOST + s, jsonObject, file);
}
@Override
public JSONObject getResponse(HttpRequestBase httpRequestBase) {
setHeaders(httpRequestBase);
JSONObject response = FanLibrary.getHttpResponse(httpRequestBase);
handleResponseHeader(response);
return response;
}
@Override
public void setHeaders(HttpRequestBase httpRequestBase) {
httpRequestBase.addHeader(Common.getRequestIdHeader());
httpRequestBase.addHeader(FanLibrary.getHeader("token", token));
httpRequestBase.addHeader(FanLibrary.getHeader("u", uname));
httpRequestBase.addHeader(FanLibrary.getHeader("appversion", "2.1.3"));
httpRequestBase.addHeader(FanLibrary.getHeader("m", "tv_yst"));
httpRequestBase.addHeader(FanLibrary.getHeader("appName", "com.example.demoapp"));
httpRequestBase.addHeader(FanLibrary.getHeader("rom", "OKAY_EBOOK_4.0.0_OKUI_5.2.0.1_20191128_T"));
if (!cookies.isEmpty()) httpRequestBase.addHeader(FanLibrary.getCookies(cookies));
}
@Override
public void handleResponseHeader(JSONObject response) {
if (!response.containsKey(HttpClientConstant.COOKIE)) return;
cookies.putAll(response.getJSONObject(HttpClientConstant.COOKIE));
response.remove(HttpClientConstant.COOKIE);
}
@Override
public JSONObject getGetResponse(String s) {
return getResponse(getGet(s));
}
@Override
public JSONObject getGetResponse(String s, JSONObject jsonObject) {
return getResponse(getGet(s, jsonObject));
}
@Override
public JSONObject getPostResponse(String s) {
return getResponse(getPost(s));
}
@Override
public JSONObject getPostResponse(String s, JSONObject jsonObject) {
return getResponse(getPost(s, jsonObject));
}
@Override
public JSONObject getPostResponse(String s, JSONObject jsonObject, File file) {
return getResponse(getPost(s, jsonObject, file));
}
@Override
public boolean isRight(JSONObject jsonObject) {
try {
int code = jsonObject.getJSONObject("meta").getInteger("ecode");
JSONObject data = jsonObject.getJSONObject("data");
return code == 0 && !data.isEmpty();
} catch (Exception e) {
return false;
}
}
/**
* 獲取並檢查code
*
* @param jsonObject
* @return
*/
public int checkCode(JSONObject jsonObject, RequestInfo requestInfo) {
if (SysInit.isBlack(requestInfo.getHost())) return TEST_ERROR_CODE;
try {
return jsonObject.getJSONObject("meta").getInteger("ecode");
} catch (Exception e) {
logger.warn("非標準響應:{}", jsonObject.toString());
}
return TEST_ERROR_CODE;
}
public JSONObject getCookies() {
return cookies;
}
public void setCookies(JSONObject cookies) {
this.cookies = cookies;
}
/**
* 測試結束,資源釋放
*/
public static void allOver() {
FanLibrary.testOver();
}
}
複製程式碼
- 鄭重宣告:公眾號“FunTester”首發,歡迎關注交流,禁止第三方轉載。
技術類文章精選
- Linux效能監控軟體netdata中文漢化版
- 效能測試框架第三版
- 圖解HTTP腦圖
- 效能測試中圖形化輸出測試資料
- 壓測中測量非同步寫入介面的延遲
- 多種登入方式定量效能測試方案
- JMeter吞吐量誤差分析
- 多專案登入互踢測試用例