HttpClient + testng for interface testing

Source: Internet
Author: User
Tags testng

HttpClient Tutorial: https://www.yeetrack.com/?p=779

One, the required environment:

1,testng, httpclient, and related dependency packages

Second, using the httpclient login Csdn interface, using testng as the Unit Test framework:

1 Importorg.apache.http.HttpEntity;2 ImportOrg.apache.http.HttpResponse;3 ImportOrg.apache.http.NameValuePair;4 Importorg.apache.http.client.HttpClient;5 Importorg.apache.http.client.entity.UrlEncodedFormEntity;6 ImportOrg.apache.http.client.methods.HttpPost;7 Importorg.apache.http.impl.client.DefaultHttpClient;8 ImportOrg.apache.http.message.BasicNameValuePair;9 Importorg.apache.http.util.EntityUtils;Ten Importorg.testng.annotations.Test; One  A Importjava.util.ArrayList; - Importjava.util.List; -  the  Public classFirsttest { -  -     PrivateHttpClient HttpClient =Newdefaulthttpclient (); -     PrivateHttpPost HttpPost; +     PrivateHttpResponse response; -     Privatehttpentity entity; +     PrivateString Postresult =NULL; A  at @Test -      Public voidloginjdtest () { -  -String loginurl = "https://passport.jd.com/uc/loginService?uuid=ac394a05-05c0-4667-9940-2848f0c03809&&r= 0.8636558873495089&version=2015 "; -         //Create a HttpPost request -HttpPost =NewHttpPost (loginurl); in  -         //Create a POST request parameter toList<namevaluepair> formparams1 =NewArraylist<namevaluepair>(); +Formparams1.add (NewBasicnamevaluepair ("uuid", "ac394a05-05c0-4667-9940-2848f0c03809")); -Formparams1.add (NewBasicnamevaluepair ("Eid", " Z6rzkbbpuuoorphgelpneze6qhfeie2is4eqebwokh7vjapdriyucserj3dlpnxrinl2on3jc3iwhig6l6pqminsry ")); theFormparams1.add (NewBasicnamevaluepair ("FP", "4D981FBD1F0CF45FC497EAC85348C0F3")); *Formparams1.add (NewBasicnamevaluepair ("_t", "_NTHEAWJ")); $Formparams1.add (NewBasicnamevaluepair ("Logintype", "C"));Panax NotoginsengFormparams1.add (NewBasicnamevaluepair ("LoginName", "Test")); -Formparams1.add (NewBasicnamevaluepair ("Nloginpwd", "Test")); theFormparams1.add (NewBasicnamevaluepair ("Chkrememberme", "" ")); +Formparams1.add (NewBasicnamevaluepair ("Authcode", "" ")); AFormparams1.add (NewBasicnamevaluepair ("PubKey", "" ")); theFormparams1.add (New Basicnamevaluepair ("Sa_token", " B68c442be645754f33277e701208059033541c4bbd4be7a99a94927bd4c8d683e0ac371429cd887688e05a86c51f7c70ce1f231d2a69583750e3923ba 39e17118b83a1a1535baa4f550cdafd8e0d42172c6687de87e4a85ea190de5b35da60bb878c558875475cc8d7ac4ea721f380156dd8325b976c3c3266 e677eacb204c70c0ae07df75629f5d845602b5968a00d446f66a103cee76ced0ed85d144b22580f9a9997b4c6606bba4acda2e5675e1753923a7ed53a 8cf7426c57380a1eeb3acc25e396c9d72455d2f8a2ee727961c86c40637a355f83a8a4ac98ab4db238ea80705100e51ead01c3e00868dbd94d782ecf8 9a937893739d7eb48cd4a388d6da51af477822a57e7c2708e31dceb0adca88a95d3fb8d20278b263b38d3679dfcd935e41a9612638671d699f59f0e49 4bf90b522095ff1e38ddb16541245ede6703833cd15f1ae6d59575c55942b5306c749927362e5d3d7643ba060f53acfc46f7222511019ebabbca226f6 56dc896c7655a90663537c9bbb13657879fa7b99474d4ec9db2879b409fce973fa0a9e4e60f4160c3961b55c3401c7cc402917f6a0984544d666fb2c4 0b6001e1d740c97dad646e692403db4a3191f9c638549616672180317153d3edc2fa55ea740bc4bae3f5032fd1e3ce7bda785ddfd5b65afd0a4cac07b18ab8b48ffe65e6c25d8e9d9c6b89d4ac495c2fe7937233ec85c60c8c3ea4a9368697eb136b02766908846fe18cf29ca73a07a0d9c3ea0eeb7a75369 ")); +Formparams1.add (NewBasicnamevaluepair ("Seqsid", "3845068114278942000")); -  $         Try { $Httppost.setentity (NewUrlencodedformentity (formparams1, "UTF-8")); -Response =Httpclient.execute (httppost); -entity =response.getentity (); the             //Here you can use tools such as Jsoup to analyze the returned results to determine whether the creation was successful -Postresult = entityutils.tostring (Entity, "UTF-8");Wuyi  theSYSTEM.OUT.PRINTLN ("Viewing the results of login interface requests returned:" +postresult); -}Catch(Exception e) { Wu e.printstacktrace (); -         } About  $ httppost.releaseconnection (); -     } -}

Third, start the test class by testng:

Right-click and select "Run" Loginjdtest "

The results after the run are as follows:

HttpClient + testng for interface testing

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.