Interface Automation Ideas _java

Source: Internet
Author: User
Tags what interface soapui testng stringbuffer

Write at the beginning: Technical slag do interface automation, the Gods please light spray! Multi-mention optimization and problem points.

Interface testing has been done through Postman and soapui to do, Postman is a plug-in chrome case more bad management, at the same time the implementation of trouble, get a click to execute, usually do debugging can be used to do interface automation is not appropriate.
SoapUI (changed name ready! API) is a good tool, while supporting the SOAP type and rest type of interface, but also support security, performance and other testing, of course, it is a business tool, advanced features are required to charge, think that what is also possible, but the tool provides a convenient same scalability poor point, And I often encounter a puzzling tool problem, perhaps because I am using that version, the stability of the poor bar.
is also a direct use of jmeter or LoadRunner to do interface automation, of course, when the use case management is also more troublesome, there is a framework robot frame, keyword driver to do interface is a friend repeatedly recommended practice, but I am not familiar with Python, I give up though it's easy to get started.

There is an open source framework rest-assured, the specific address: https://code.google.com/p/rest-assured/is also quite interesting, after a few days to introduce the framework, later ... Our development to write the interface is not standard, had to bear to kill.
Now I switch to such a set of ideas to do, because at present my interface protocol is HTTP, I simply use the Java httpclient, and then the interface return is JSON format, I introduced the json-lib to facilitate the parse get, Also because of the need to interact with the database so that the introduction of MyBatis, while the use case is still used testng, in order to more convenient management interface (address, path, parameters, etc.) through Excel to manage the interface, through Excel to drive the test.
This way down the entire project involves a framework package such as Httpclient+json+mybaits+testng+poi (Excel) +log4j.

In this process do some encapsulation some of the main code is as follows:
For the httpclient package, simply pass in the full request of the interface and if the request is normal, return the JSON returned by the interface:

[Java]View PlainCopy
  1. Public static JSON Getapi (Uri uri) throws URISyntaxException, IOException {
  2. Closeablehttpclient httpclient = Httpclients.createdefault ();
  3. try {
  4. HttpGet = new HttpGet (URI);
  5. Log.info ("Execute API Request" + Httpget.getrequestline ());
  6. responsehandler<string> ResponseHandler = new responsehandler<string> () {
  7. Public String Handleresponse (
  8. Final HttpResponse response) throws Clientprotocolexception, IOException {
  9. int status = Response.getstatusline (). Getstatuscode ();
  10. if (status >= && status < ) {
  11. httpentity entity = response.getentity ();
  12. return entity ! = null?  Entityutils.tostring (entity): null;
  13. } Else {
  14. Log.error ("request error, Status code:" +response.getstatusline (). Getstatuscode ());
  15. throw New Clientprotocolexception ("Unexpected State return:" + status);
  16. }
  17. }
  18. };
  19. String responsebody = Httpclient.execute (HttpGet, ResponseHandler);
  20. Jsonobject dataObject = Jsonobject.fromobject (responsebody);
  21. return dataObject;
  22. } finally {
  23. Httpclient.close ();
  24. }
  25. }


That request can be initiated, how does the interface URL come? Just said to manage the interface through Excel, for example I created the following file:

The TID is used to identify what line of data I need to read, Tname just to explain what interface this interface is, method to indicate what type of interface it is, call different httpclient encapsulation methods according to different types, and then the URL to splice the interface:

The key code for stitching URLs is as follows:

[Java]View PlainCopy
  1. /**
  2. * Request Link stitching
  3. *
  4. * @param tid which line of Excel data
  5. * @param parameters Parameters
  6. * @return URI
  7. */
  8. public static URI Returnuri (String tid, string ... parameters) {
  9. String path = System.getproperty ("User.dir") + file.separator + "src" + file.separator + "main" + File.separat  or + "resources" + File.separator + "Excel" + File.separator + "Api.xls";
  10. Excelengine.filepath = path;
  11. Excelengine.sheetname = "API";
  12. //Get data from Excel
  13. List apidata = Getapidata (TID);
  14. String getOrPost = (string) apidata.get (2)). toLowerCase ();
  15. System.out.println (getOrPost);
  16. String scheme = (string) apidata.get (3);
  17. String apihost = (string) apidata.get (4);
  18. String Apipath = (string) apidata.get (5);
  19. //URI stitching
  20. StringBuffer StringBuffer = new StringBuffer (scheme + "://" + Apihost + Apipath + "?");
  21. hashmap<string, string> HashMap = new HashMap ();
  22. int p = 0;
  23. While (P < parameters.length) {
  24. Hashmap.put (String) apidata.get (p + 6), parameters[p]);
  25. p = p + 1;
  26. }
  27. Hashmap.put ("AutoKey", String.valueof (Parameters.timestamp_long ()));
  28. int i = Hashmap.size ();
  29. SYSTEM.OUT.PRINTLN ("Map size" + i);
  30. int num = 0;
  31. SYSTEM.OUT.PRINTLN ("Value inside Map" + hashmap.entryset ());
  32. for (hashmap.entry<string, string> entry:hashmap.entrySet ()) {
  33. num++;
  34. if (num = = Hashmap.size ()) {
  35. Stringbuffer.append (Entry.getkey () + "=" + Entry.getvalue ());
  36. } Else {
  37. Stringbuffer.append (Entry.getkey () + "=" + entry.getvalue () + "&");
  38. }
  39. }
  40. String URL = stringbuffer.tostring ();
  41. Uri uri = uri.create (URL);
  42. return URI;
  43. }


Now the interface of the URL stitching completed, the request also initiated, you can get the JSON data returned, and finally have to check the returned data, here I was re-encapsulated under the testng provides the Assert class for the Taquassert class, in fact, not much change added a few methods just, Here is an example of a complete login:

[Java]View PlainCopy
  1. Public class Logintest {
  2. static Loggercontroler log = Loggercontroler.getlogger (logintest.   Class);
  3. //Accounts table
  4. Accountstest accountstest = new Accountstest ();
  5. String tid = "V1_account_login";
  6. @Test
  7. public void Loginsuccess () {
  8. //Initiating API requests
  9. Jsonobject json = Apiengine.taquapi (tid, "username", "password");
  10. //Print out JSON
  11. Log.info (JSON);
  12. //Get the user's account_id through the API and then query the accounts table
  13. String account_id = Json.getjsonobject ("info"). Getjsonobject ("data"). GetString ("account_id");
  14. Accounts Accounts = accountstest.byaccount_id (integer.valueof (account_id));
  15. //Verify the status returned is not success.
  16. String response_status = json.getstring ("Response_status");
  17. Taquassert.assertequals ("Check Response_status", "Success", Response_status);
  18. //From the database to get the user name and interface returned by the user name to do a comparison check consistency
  19. String account_name_sql = Accounts.getaccount_name ();
  20. String Account_name_api = Json.getjsonobject ("info"). Getjsonobject ("data"). GetString ("nickname");
  21. Taquassert.assertequals ("Check account_name", Account_name_api, Account_name_sql);
  22. }
  23. }


Later will put this piece to the Jenkins environment to run, before the Jenkins environment has been ruined, bitter force still have to re-build.
OK above is the current interface to do the idea of automation, which certainly there is a lot of need to do optimization, hope that the great God to mention optimization ideas and programs, greatly appreciated.

Interface Automation Ideas _java

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.