This article has participated in the third “High Yield more text” track of the Denver Creators Training Camp, check out the details:Digg project | Creator Boot Camp phase 3 is underway, “write” to make a personal impact.
background
In the implementation of interface automation test, no matter how the design, will walk on the data-driven road, ultimately comes down to the problem of parameterization/parameter correlation, the final problem of doing this kind of automation can be divided into two categories: one kind of technical class, one kind of business class, what is technology, business? Business process, that is, the sequence of request interfaces and data flow, is reflected in the design of test cases in the framework. If a single interface only focuses on input parameters, if a single interface needs to pay attention to the business association between interfaces, that is, request interfaces come first and come later, otherwise the data can not correspond! The technology is to implement the processing required in business scenario testing, such as assembling interface requests, parameter correlation, logging system, etc. Finally, it has to be mentioned that high cohesion and low coupling: ensure that the correlation between single/multiple interfaces is reduced, and make the best use of a function.Copy the code
design
When designing an automated test framework, we often parameterize some hard code data, dynamically respond from the interface or initiate requests through the substitution process of functions. For example:
From the point of view of the request address, this is a single interface test case design, login interface, analysis, in fact, it does not need parametric design, why, because it has only one expected result of success, the only correct is that it uses the correct mobile phone number login; So the other wrong parameter request, just why did it fail? If we give incorrect parameters, then we only need to overwrite several requests with incorrect parameters.
So in the example diagram, if it is a normal business scenario: user registration -- login -- real name -- card binding -- transaction -- view order -- balance -- exit; Then the possible situation is: the login account should be the registered account, the phone number of real-name card binding may be the registered phone number, etc.; In addition, the order number after the transaction is one of the request parameters to view the order, and all the interface of online operation requires token, etc., these interface requests are interlinked, and each operation has a direct dependency relationship. You need to parameterize the input parameters to implement alternative generic methods.
implementation
JMeter Java script for beanshell batch processing parameters
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// The value of a variable is received with a variable, but it cannot be used directly as a parameter
//String mobile=vars.get("phone");
//String empty=vars.get("empty");
//String passwd=vars.get("passwd");
// Json string, for example, should be the params parameter in the CSV use case; It can be retrieved from a CSV or other variable element
String content = vars.get("params");
String double quotes need to be escaped when encoding, but not when using variable parameters
//String "AppVersion params =" {\ \ ": \" V10.3 being \ ", \ "the channel \" : 0, \ "deviceName \" : \ "iOS \", \ "deviceType \" : \ "1 \", \ "deviceid \" : \ "123456 \", \ "lo ginType\":0,\"mobile\":\"#mobile#\",\"password\":\"#passwd#\",\"pushToken\":\"string\",\"systemVersion\":\"1\",\"verifyC ode\":\"1\",\"zone\":\"86\"}";
log.info("Get parameter type:"+ content.getClass());
log.info("Print the original parameter:"+content);
// Assign the value of the received variable to a variable that can be referenced with ${param}, which is used interchangeably with vars.get();
//vars.put("empty",empty);
//vars.put("passwd",passwd);
//vars.put("mobile",mobile);
// Regular expressions
String pattern="# (. +?) #";
// Regular expressions match compiled objects
Pattern pt = Pattern.compile(pattern);
Matcher m = pt.matcher(content);
// New character types: When modifying strings, use the StringBuffer and StringBuilder classes.
StringBuffer newContent = new StringBuffer();
// Note that the if condition is not used here,
while (m.find()) {
// If the above conditions are met, check whether there is a key
if (m.group().contains(m.group(1))) {
log.info("Find parameters:"+vars.get(m.group(1)));
// This append substitution method modifies the original string without recreating the string
m.appendReplacement(newContent, vars.get(m.group(1)));
log.info("After replacement:"+newContent);
}
}
m.appendTail(newContent);
log.info("Parameter after replacement:"+newContent.toString());
// Assign the replaced argument to the new variable
vars.put("newParams",newContent.toString());
// Finally pass ${newParams} into the sampler sampler
Copy the code
The principle of
When designing test cases, the characteristic characters of the request parameters that need to be replaced can be designed. The fields that need to be replaced can be found through code means, so as to exchange and re-assign values and then use; But the premise is that you must get it before you replace it, and then you assign it to replace; Finally, the replacement parameters are obtained, and the correct response can be obtained when the request is initiated. This is jMeter's process for designing and replacing interface request parameters. Of course, this is only the tip of the iceberg, more need to discover, good at thinking to solve the problem reasonably and effectively.Copy the code
conclusion
In fact, the same problem, whether tools or code implementation, can be thought of reuse; The same goes for programming languages. Hint: in the design of this article, there is a need for collaborative pre - and post-processing.Copy the code