HttpClient + TestNG Interface Automatic Test chapter III

Source: Internet
Author: User

After that, data preparation and logical implementation are not

This automation script does not use too many testng, only for his debugging convenience;

This chapter records some of the helper methods used:

1. Get request to print the return value, for testing or viewing the interface return content

 Public Static voidGet (hashmap<string, string>params, string HOST, String PATH) {closeablehttpclient httpclient=Httpclients.createdefault (); Try {            //Create a HttpGet request. HttpGet HttpGet =NewHttpGet (GetUrl (Getformparams (params), HOST, PATH)); System.out.println ("Executing request" +Httpget.geturi ()); //perform a GET requestCloseablehttpresponse response =Httpclient.execute (HttpGet); Try {                //put the response into the entity carrierhttpentity entity =response.getentity (); System.out.println ("--------------------------------------"); //print back to status barSystem.out.println (Response.getstatusline ()); if(Entity! =NULL) {                    //Print back Content lengthSystem.out.println ("Response Content Length:" +entity.getcontentlength ()); //Print back ContentSystem.out.println ("Response content:" +entityutils.tostring (entity)); } System.out.println ("------------------------------------"); } finally{response.close (); }        } Catch(clientprotocolexception e) {e.printstacktrace (); } Catch(ParseException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {            //Disconnect Connection            Try{httpclient.close (); } Catch(IOException e) {e.printstacktrace (); }        }    }

2. Remove elements from a long array containing short arrays

 Public Static string[] Unlockedseatarray (string[] Longerarray, string[] shorterarray) {    linkedlistNew Linkedlist<>();      for (String str:longerarray) {        list.add (str);    }      for (String str:shorterarray) {        if(List.contains (str)) {            list.remove (str);        }    }     = {};     return List.toarray (result);}

3. Merge each of the first n values in the array and form a new array

 Public StaticString[] Seatarray (string[] Targetarray,intindex) {string[] Seatarray=NULL; intLen =targetarray.length; intLen2 = 0; intj = 0;//seat count Loop variable//calculate the length of an array     if(0==len%index) {Len2= len/index; }Else{len2= (int) (Len/index) + 1 ; }     //initializing an arraySeatarray =NewString[len2]; Len2= 0; j = 0;  for(inti=0; i<len; i++) {        if(Index = =j) {J= 0; SEATARRAY[LEN2]= Seatarray[len2].substring (4, Seatarray[len2].length ()-1); Len2++; } Seatarray[len2]= Seatarray[len2] + targetarray[i] + ","; J++; } Seatarray[len2]= Seatarray[len2].substring (4, Seatarray[len2].length ()-1);//process the last data    returnSeatarray;}

4. Simple generation of log methods

 Public Static voidWriteFile (string response, String path) {Try{SimpleDateFormat time=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String timestring= Time.format (Newjava.util.Date ()); File File=NewFile (path); if(!file.exists ())            {File.createnewfile ();} FileWriter FW=NewFileWriter (file,true); BufferedWriter BW=NewBufferedWriter (FW);            Bw.append (response); Bw.write ("\ r \ n"); Bw.write ("---------" + "cute split line" + "----------" +timestring); Bw.write ("\ r \ n");            Bw.close ();        Fw.close (); } Catch(IOException e) {e.printstacktrace (); }            }

Almost some of the methods used here are recorded here.

HttpClient + TestNG Interface Automatic Test chapter III

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.