Interface based on Java + HttpClient + automated testing framework TestNG (v) ------ test function interface implementation class and classes

  In the first one, we focuses on the treatment parameters.

  However, these parameters are basically direct assignment. The actual interface testing, we will make a lot of test data as required, these test data will be used according to different situations. For example, I might need a randomly generated a Chinese string, or a need to be able to quickly find a file or the value of the string of MD5. In this case, we need hands to write some functions to make the call.

  However, if it is written in Excel (or another text editor) in the test case, how can let the program know that I was able to call a function, and this function can return a value to a variable, I want to correct the incoming place? Here, we take a look at the specific implementation.

       First, we need to determine what, how to get the program to recognize this is a function, not a string. Here, there is a difficulty.

  Since it is a function, the parameters of the problem, there may be an argument, there may be more, there may be no. Therefore, we are here is to deal with the uncertain parameters. (Of course, the specific function of the number of parameters, we can define a)

  Before using similar form $ {param_name}, we use the function in place of a similar form __funcName (args) is determined to make this program a function.

  Let's design a prototype of a function, you can use the interface class, used to refer to the function we want to use when sending interface. For a function, here we need it is two parts, one is the name of the function, to perform a specific function.

      See the side section of code:

public interface functionInterface {
    String execute(String[] args);

    String getReferenceKey();
}

  Here interfaces class has two methods, execution returns a string, obtaining a name of the function. Here, it is understood, String [] args to define the parameters of our method of execution, is uncertain parameters.

Below, we class based on this interface to achieve our first class of functions (acquisition date function date)

Import the java.text.SimpleDateFormat;
 Import java.util.Date;
 Import utils.stringUtil; 

public  class dateFunctions   the implements functionInterface {
     // override method of performing 
    @Override
     public String Execute (String [] args) {
         // If no parameter, returns the Unix timestamp (time in milliseconds), otherwise the current system time. 
        IF (args.length stringUtil.isEmpty == 0 || (args [0 ])) {
             return String.format ( "% S", new new a Date () the getTime ().); 
        } the else {
             // the current time returned . It can be a custom format, where the first written "yyyy-MM-dd", may be modified as needed 
            return getCurrentDate("yyyy-MM-dd");
        }
    }

    private String getCurrentDate(String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String str = format.format(new Date());
        return str;
    }
    
    //定义该函数的名字为“date”
    @Override
    public String getReferenceKey() {
        // TODO Auto-generated method stub
        return "date";
    }
}

Here, we customized a method of execution, a name. The content execution method which can also change according to their own needs. In using the form below:

  • __date (param1): generating a formatted string when performing this function. param1 format conversion, the default is "yyyy-MM-dd". Of course, we can also specify other formats, such as "yyyy-MM-dd-HH-mm-ss" and so on. In short, you can get to the data you want by the function argument.

According to this line of thought, we can write some of the frequently used functions.

The second function (MD5 value of the request document)

 Note: The need to import apache-commons-codec package the jar in the project .

Import java.io.File;
 Import a java.io.FileInputStream;
 Import java.io.FileNotFoundException;
 Import java.io.IOException;
 Import the java.net.URL; 

Import org.apache.commons.codec.digest.DigestUtils; 

public  class md5Functions the implements functionInterface {
     // override method of performing 
    @Override
     public String execute (String [] args) {
         the try { 
            String filePath = args [0 ]; 
       begin // if filepath is http, then treated as a link.
IF (filePath.startsWith ( "HTTP")) { return DigestUtils.md5Hex(new URL(filePath).openStream()); } else { return DigestUtils.md5Hex(new FileInputStream(new File( filePath))); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override public String getReferenceKey() { // TODO Auto-generated method stub return "MD5"; } }

Here we have completed the function called md5 performs this function need to enter a parameter, the path to the file. Of course, we can change a little bit, the function md5 value of it is to take the written string, it is also very simple.

String md5_value = DigestUtils.md5Hex(target_str);

This is the core of a can.

  Use the form or the same as before:

  • __MD5 (param1): param1 here is the string path, the path can be http, it can also be a path to the file.

  Here, leaving a small problem: Modify the code so that the function value can order md5 file and path, you can also evaluate md5 string.  

  Based on the above ideas, we can also do some minimum and maximum values, summed quadrature and so commonly used tool function, called when the interface to facilitate testing. Here is not started, please interested in expanding their own small partners.

 

Guess you like

Origin www.cnblogs.com/generalli2019/p/12202513.html