转载

自动化项目基类实践--视频演示

相信一万行代码的理论!

测试框架讲完了,今天开始分享一些自己写过的接口测试项目的代码,先分享一个基类的实践。主要分享看 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”首发,欢迎关注交流,禁止第三方转载。
原文  https://juejin.im/post/5e894eb851882573716a8fb7
正文到此结束
Loading...