FunTester 接口自动化通用验证类

FunTester · 2020年03月31日 · 676 次阅读

在做自动化的过程中,一定会遇到很多验证的点,但是有些验证功能是通用的,所以我封装了一个通用的验证类,来解决重复验证的问题,之前也写过一个,现在这个增加了一下数组的验证,还有一些隐藏 bug 的修复。话不多说,分享代码,供大家参考:

package com.fun.frame;

import com.fun.base.bean.RequestInfo;
import com.fun.base.interfaces.IBase;
import com.fun.frame.httpclient.FanLibrary;
import com.fun.utils.Regex;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 通用验证方法封装
 */
public class Verify extends SourceCode {

    private static Logger logger = LoggerFactory.getLogger(Verify.class);

    /**
     * 断言的json对象
     */
    private JSONObject verifyJson;

    /**
     * 断言的code
     */
    private int code;

    /**
     * 断言的json对象分行解析
     */
    private List<String> lines = new ArrayList<>();

    public Verify(JSONObject jsonObject) {
        this.verifyJson = jsonObject;
        this.lines = parseJsonLines(jsonObject);
    }

    /**
     * 获取 code
     *<p>这里的requestinfo主要的目的是为了拦截一些不必要的checkcode验证的,主要有black_host名单提供,在使用时,注意requestinfo的非空校验</p>
     *
     * @return
     */
    public int getCode() {
        return FanLibrary.getiBase().checkCode(verifyJson, null);
    }


    /**
     * 校验code码是否正确,==0
     *
     * @return
     */
    public boolean isRight() {
        return 0 == this.getCode();
    }

    /**
     * 获取节点值
     *
     * @param key 节点
     * @return 返回节点值
     */
    public String getValue(String key) {
        int size = lines.size();
        for (int i = 0; i < size; i++) {
            String line = lines.get(i);
            if (line.startsWith(key + ":"))
                return line.replaceFirst(key + ":", EMPTY);
        }
        return EMPTY;
    }

    /**
     * 校验是否包含文本
     *
     * @param text 需要校验的文本
     * @return 返回 Boolean 
     */
    public boolean isContains(String... text) {
        boolean result = true;
        String content = verifyJson.toString();
        int length = text.length;
        for (int i = 0; i < length; i++) {
            if (!result) break;
            result = content.contains(text[i]) & result;
        }
        return result;
    }

    /**
     * 校验节点值为数字
     *
     * @param value 节点名
     * @return 返回 Boolean 
     */
    public boolean isNum(String... value) {
        boolean result = true;
        int length = value.length;
        for (int i = 0; i < length; i++) {
            String key = value[i] + ":";
            if (!verifyJson.toString().contains(value[i]) || !result)
                return false;
            for (int k = 0; k < lines.size(); k++) {
                String line = lines.get(k);
                if (line.startsWith(key)) {
                    String lineValue = line.replaceFirst(key, EMPTY);
                    result = isNumber(lineValue) & result;
                }
            }
        }
        return result;
    }

    /**
     * 校验节点值不为空
     *
     * @param keys 节点名
     * @return 返回 Boolean 值,为空返回false,不为空返回true
     */
    public boolean notNull(String... keys) {
        boolean result = true;
        for (int i = 0; i < keys.length; i++) {
            String key = keys[i] + ":";
            if (!verifyJson.toString().contains(keys[i]) || !result)
                return false;
            for (int k = 0; k < lines.size(); k++) {
                String line = lines.get(k);
                if (line.startsWith(key)) {
                    String lineValue = line.replaceFirst(key, EMPTY);
                    result = lineValue != null & !lineValue.isEmpty() & result;
                }
            }
        }
        return result;
    }

    /**
     * 验证是否为列表,根据字段后面的符号是否是[
     *
     * @param key 返回体的字段值
     * @return
     */
    public boolean isArray(String key) {
        String json = verifyJson.toString();
        int index = json.indexOf(key);
        char a = json.charAt(index + key.length() + 2);
        return a == '[';
    }

    /**
     * 验证是否是json,根据后面跟的符号是否是{
     *
     * @param key 返回体的字段值
     * @return
     */
    public boolean isJson(String key) {
        String json = verifyJson.toString();
        int index = json.indexOf(key);
        char a = json.charAt(index + key.length() + 2);
        if (a == '{')
            return true;
        return false;
    }

    /**
     * 是否是Boolean
     *
     * @return
     */
    public boolean isBoolean(String... value) {
        boolean result = true;
        int length = value.length;
        for (int i = 0; i < length; i++) {
            String key = value[i] + ":";
            if (!verifyJson.toString().contains(value[i]) || !result)
                return false;
            for (int k = 0; k < lines.size(); k++) {
                String line = lines.get(k);
                if (line.startsWith(key)) {
                    String lineValue = line.replaceFirst(key, EMPTY);
                    result = Regex.isRegex(lineValue, "^(false)|(true)$") & result;
                }
            }
        }
        return result;
    }

    /**
     * 验证正则匹配结果
     *
     * @param regex
     * @return
     */
    public boolean isRegex(String regex) {
        String text = verifyJson.toString();
        return Regex.isRegex(text, regex);
    }

    /**
     * 解析json信息
     *
     * @param response json格式的响应实体
     * @return json每个字段和值,key:value形式
     */
    public static List<String> parseJsonLines(JSONObject response) {
        String jsonStr = response.toString();// 先将json对象转化为string对象
        jsonStr = jsonStr.replaceAll(",", LINE);
        jsonStr = jsonStr.replaceAll("\"", EMPTY);
        jsonStr = jsonStr.replaceAll("\\\\/", OR);
        jsonStr = jsonStr.replaceAll("\\{", LINE);
        jsonStr = jsonStr.replaceAll("\\[", LINE);
        jsonStr = jsonStr.replaceAll("}", LINE);
        jsonStr = jsonStr.replaceAll("]", LINE);
        List<String> jsonLines = Arrays.asList(jsonStr.split(LINE));
        return jsonLines;
    }
}

技术类文章精选

  1. java 一行代码打印心形
  2. Linux 性能监控软件 netdata 中文汉化版
  3. 接口测试代码覆盖率(jacoco)方案分享
  4. 性能测试框架
  5. 如何在 Linux 命令行界面愉快进行性能测试
  6. 图解 HTTP 脑图
  7. 如何测试概率型业务接口
  8. httpclient 处理多用户同时在线
  9. 将 swagger 文档自动变成测试代码
  10. 五行代码构建静态博客
  11. httpclient 如何处理 302 重定向
  12. 基于 java 的直线型接口测试框架初探
  13. Tcloud 云测平台 -- 集大成者
  14. 如何测试概率型业务接口
  15. Android App 测试工具及知识大集合

非技术文章精选

  1. 为什么选择软件测试作为职业道路?
  2. 成为杰出 Java 开发人员的 10 个步骤
  3. 写给所有人的编程思维
  4. 自动化测试的障碍
  5. 自动化测试的问题所在
  6. 测试之《代码不朽》脑图
  7. 成为优秀自动化测试工程师的 7 个步骤
  8. 优秀软件开发人员的态度
  9. 如何正确执行功能 API 测试
  10. 未来 10 年软件测试的新趋势 - 上
  11. 未来 10 年软件测试的新趋势 - 上
  12. 自动化测试解决了什么问题
  13. 17 种软件测试人员常用的高效技能 - 上
  14. 17 种软件测试人员常用的高效技能 - 下

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册