Universal Verification Class for Interface Automation

Keywords: Programming JSON Java Linux Android

In the process of automation, we will encounter a lot of verification points, but some verification functions are universal, so I encapsulated a general verification class to solve the problem of repeated verification. I also wrote one before. Now this adds an array of verification, and there are some hidden bug fixes. Not much to say, share the code for your reference:

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;

/**
 * Universal Validation Method Packaging
 */
public class Verify extends SourceCode {

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

    /**
     * json object of assertion
     */
    private JSONObject verifyJson;

    /**
     * Code code of assertion
     */
    private int code;

    /**
     * Line-by-line parsing of asserted json objects
     */
    private List<String> lines = new ArrayList<>();

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

    /**
     * Get code
     *<p>The main purpose of requestinfo here is to intercept some unnecessary checkcode validation, mainly provided by the black_host list. When using it, pay attention to the non-empty validation of requestinfo.</p>
     *
     * @return
     */
    public int getCode() {
        return FanLibrary.getiBase().checkCode(verifyJson, null);
    }


    /**
     * Check whether the code code is correct, ==0
     *
     * @return
     */
    public boolean isRight() {
        return 0 == this.getCode();
    }

    /**
     * Get node values
     *
     * @param key node
     * @return Return node value
     */
    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;
    }

    /**
     * Check whether text is included
     *
     * @param text Text to be checked
     * @return Returns the Boolean value
     */
    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;
    }

    /**
     * Check node value is numeric
     *
     * @param value Node name
     * @return Returns the Boolean value
     */
    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;
    }

    /**
     * Check node value is not empty
     *
     * @param keys Node name
     * @return Returns the Boolean value, false for null, true for null
     */
    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;
    }

    /**
     * Verify that it is a list, depending on whether the symbol after the field is[
     *
     * @param key Field value of return body
     * @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 == '[';
    }

    /**
     * Verify that it is json, depending on whether the following symbol is{
     *
     * @param key Field value of return body
     * @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;
    }

    /**
     * Is it a Boolean value?
     *
     * @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;
    }

    /**
     * Verify the results of regular matching
     *
     * @param regex
     * @return
     */
    public boolean isRegex(String regex) {
        String text = verifyJson.toString();
        return Regex.isRegex(text, regex);
    }

    /**
     * Parsing json information
     *
     * @param response json Format response entity
     * @return json Each field and value, in key:value form
     */
    public static List<String> parseJsonLines(JSONObject response) {
        String jsonStr = response.toString();// First transform json object into string object
        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;
    }
}

Selection of Technical Articles

  1. One line of java code prints a heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  3. Interface Test Code Coverage (jacoco) Scheme Sharing
  4. Performance testing framework
  5. How to Enjoy Performance Testing on Linux Command Line Interface
  6. Graphic HTTP Brain Map
  7. How to Test Probabilistic Business Interface
  8. httpclient handles multi-user simultaneous online
  9. Automatically convert swagger documents into test code
  10. Five lines of code to build static blogs
  11. How httpclient handles 302 redirection
  12. A preliminary study on the testing framework of linear interface based on java
  13. Tcloud Cloud Measurement Platform
  14. How to Test Probabilistic Business Interface
  15. Android App Testing Tools and Knowledge Collection

Selection of non-technical articles

  1. Why choose software testing as a career path?
  2. Ten Steps to Become a Great Java Developer
  3. Writing to everyone about programming thinking
  4. Obstacles to automated testing
  5. The Problems of Automated Testing
  6. Tested "Code Immortality" Brain Map
  7. Seven Steps to Become an Excellent Automated Testing Engineer
  8. Attitudes of Excellent Software Developers
  9. How to Execute Functional API Testing Correctly
  10. New Trends in Software Testing in the Next 10 Years
  11. New Trends in Software Testing in the Next 10 Years
  12. What problems do automated testing solve?
  13. 17 kinds of efficient skills commonly used by software testers
  14. 17 kinds of efficient skills commonly used by software testers - below

Click on the Public Number Map

Posted by RickChase on Wed, 25 Sep 2019 20:50:46 -0700