Processing Java + HttpClient + TestNG interface automated testing framework (VI) based on a random function -------

  Before we use interface class and interface implementation class, to define the tools we use.

  And all the tools, the random function is more difficult to handle. Because of the random object is not clear, the random number is not clear. We can use the definition of random classes to simple and fast process we need to randomly generated objects.

  First, let's define a randomUtil class, in this class, we will want to generate random function specific definition. Here, we give a few examples. For example, a random integer generated randomly generates a fixed-length (or variable length), a string with characters (Chinese characters or without) randomly generates an array like.

Look at the specific category of RandomUtil code:

Import java.util.Random; 

public  class RandomUtil {
     // definition of letters and numbers 
    public  static String randomBase = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
     public  static String randomNumberBase = "0123456789" ; 
    
    
    // the Unicode character code range substantially co 0x4e00 ~ 0x9fa5 20902, the It is found from the internet. Therefore, we should give this a random range calculated. 
    Private  Final  static  int HANZI_LENGTH = 20902; public static the Random Random = new new the Random ();   / ** 
     * randomly generated characters 
     * @return * / protected
    
     
    
    
     
     static  char getRandomHanZi () { 
        the Random RAN = new new the Random (); 
     // 0x4e00 character code from the beginning to the rear between 20,902
return ( char ) (+ 0x4e00 ran.nextInt (HANZI_LENGTH)); } / ** * Generate a random letter * @return * / protected static char getRandomStr () { the random RAN = new new the random (); return ( char ) randomBase.charAt (ran.nextInt (62 is )); } / ** * with a randomly generated characters in Chinese string * @paramlength is the length of the parameter string * @return * / public static String getRandomText ( int length) { the Random Random = new new the Random (); the StringBuffer SB = new new the StringBuffer (); for ( int I = 0; I <length; I ++ ) {
       // switch to use a random number as
int randomNum = random.nextInt (2 ); char CH = 0 ; IF (randomNum == 0) { // generate characters CH = getRandomHanZi (); }the else IF (randomNum ==. 1) { // generates a digit or character CH = getRandomStr (); } sb.append (String.valueOf (CH)); } return sb.toString (); }

     / **
     * with a randomly generated string of Chinese
     * @param length as the length of the parameter string, false as a string, true digital
     * @return
     * /

public static String getRandom(int length, boolean onlyNumber) {
        String base;
        if (onlyNumber) {
            base = randomNumberBase;
        } else {
            base = randomBase;
        }
        
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char chr;
            do {
                int number = random.nextInt(base.length());
                chr = base.charAt(number);
            } while (i==0&&chr=='0') ;//第一个字符不能为0,
            
            sb.append(chr);
        }
        return sb.toString();
    }

    
    public static String getRandomArr(int arrLength, int length, boolean flag) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < arrLength; i++) {
            sBuffer.append(getRandom(length, flag)).append(",");
        }
        int leng = sBuffer.toString().length();
        return sBuffer.toString().substring(0, leng-1);
    }

    /**
     * 生成定长的字符串数组
     * @param arrLength  数组长度
     * @param paramStr   
     * @return
     */
    public static String generateStrArr(int arrLength, String paramStr) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < arrLength; i++) {
            sBuffer.append("\"").append(paramStr).append("\"").append(",");
        }
        int leng = sBuffer.toString().length();
        return sBuffer.toString().substring(0, leng-1);
    }

}

  In this randomized class, we define a number of methods for random generated objects, and the exact definition of the parameters. Thus, according to this we have a class, and interface front class definition, random function can quickly define relevant. E.g:

public  class RandomFunction the implements functionInterface { 
    @Override 
    public String Execute (String [] args) {
         int len = args.length;
         int length =. 6; // default. 6 
        Boolean In Flag = to false ; // defaults to false 
        IF (len> 0 ) { // first parameter string length 
            length = Integer.valueOf (args [0 ]); 
        } 
        iF (len>. 1) { // if the second argument plain string 
            flag = Boolean.valueOf (args [1 ]); 
        } 
        return RandomUtil.getRandom(length, flag);
    }

    @Override
    public String getReferenceKey() {
        return "random";
    }

}

  Here, we define the random function. Use as follows:

  • __random (param1, param2): generating a random string of fixed-length (excluding Chinese). param1: length (optional, defaults to 6), param2: pure digital identification (optional, default is false).

Next, we'll continue with the second random function:

public  class RandomStrArrFucntion the implements functionInterface { 

    @Override 
    public String Execute (String [] args) {
         // The first parameter is the number of parameters to generate an array of length i.e.
         // second parameter is a parameter length
         // third parameter whether only digital Signage 
        int len = args.length;
         int arrLength =. 1; // The default length of the array. 1 
        int length =. 6; // default parameters of length. 6 
        Boolean In flag = to false ; // defaults to false 
        IF (len ==. 1 ) { 
            arrLength = Integer.valueOf (args [0 ]);
        } else if (len == 2) {
            arrLength = Integer.valueOf(args[0]);
            length = Integer.valueOf(args[1]);
        } else if (len == 3) {
            arrLength = Integer.valueOf(args[0]);
            length = Integer.valueOf(args[1]);
            flag = Boolean.valueOf(args[1]);
        }
        return RandomUtil.getRandomArr(arrLength, length, flag);
    }
@Override
public String getReferenceKey() { // TODO Auto-generated method stub return "randomStrArr"; } }

  Here we define a function randomStrArr randomly generated string array using the following form:

  • __randomStrArr (param1, param2, param3): randomly generating a fixed-length string array. param1: the array length (optional, defaults to 1), param2: a single string length (optional, default 6), param3: pure digital identification (optional, default is false).

Next, we look at the third random function:

public class RandomTextFunction implements functionInterface{

    @Override
    public String execute(String[] args) {
        int length = 6;// 默认为6
        if (StringUtil.isNotEmpty(args[0])) {
            length = Integer.valueOf(args[0]);// 参数是长度
        }
        return RandomUtil.getRandomText(length);
    }

    @Override
    public String getReferenceKey() {
        // TODO Auto-generated method stub
        return "randomText";
    }

}

  Here we define a third random function randomText, used as follows:

  • __randomText (param1): generating a random string of fixed length (including Chinese). param1: length (optional, defaults to 6)

  You can see, we'll define more than three times the random function to achieve the class, we call the method defined in RandomUtil. Therefore, if we define the relevant method in RandomUtil it used to generate random function is very fast and convenient.

 

Guess you like

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