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
- One line of java code prints a heart
- Chinese Language Version of Linux Performance Monitoring Software netdata
- Interface Test Code Coverage (jacoco) Scheme Sharing
- Performance testing framework
- How to Enjoy Performance Testing on Linux Command Line Interface
- Graphic HTTP Brain Map
- How to Test Probabilistic Business Interface
- httpclient handles multi-user simultaneous online
- Automatically convert swagger documents into test code
- Five lines of code to build static blogs
- How httpclient handles 302 redirection
- A preliminary study on the testing framework of linear interface based on java
- Tcloud Cloud Measurement Platform
- How to Test Probabilistic Business Interface
- Android App Testing Tools and Knowledge Collection
Selection of non-technical articles
- Why choose software testing as a career path?
- Ten Steps to Become a Great Java Developer
- Writing to everyone about programming thinking
- Obstacles to automated testing
- The Problems of Automated Testing
- Tested "Code Immortality" Brain Map
- Seven Steps to Become an Excellent Automated Testing Engineer
- Attitudes of Excellent Software Developers
- How to Execute Functional API Testing Correctly
- New Trends in Software Testing in the Next 10 Years
- New Trends in Software Testing in the Next 10 Years
- What problems do automated testing solve?
- 17 kinds of efficient skills commonly used by software testers
- 17 kinds of efficient skills commonly used by software testers - below