在上一篇中,我們寫了第一個get請求的測試類,這一篇我們來對他進行初步優化和封裝
3.1 分離請求發送類
首先想到的問題是,以后我們的接口自動化測試框架會大量用到發送http請求的功能。
那么這一部分的處理,可以將他分離出來,以后的測試類只需要調用請求類的方法實現發送請求和接收反饋的功能。
在我們的項目目錄src/main/java下,新建一個包名為com.test.client,在包下新建restfulClient.java。
這個類我們把上一篇寫的發送請求和處理反饋的代碼遷移過來,並做出一些改動:
package com.test.client; import java.io.IOException; import java.util.HashMap; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class RestfulClient { CloseableHttpClient httpclient; HttpGet httpGet; CloseableHttpResponse httpResponse; int responseCode; JSONObject responseBody; HashMap<String, String> responseHeads; //通過httpclient獲取請求的反饋 public void getResponse(String url) throws ClientProtocolException, IOException{ httpclient = HttpClients.createDefault(); httpGet = new HttpGet(url); httpResponse = httpclient.execute(httpGet); } //以JSON格式獲取到反饋的主體 public JSONObject getBodyInJSON() throws ParseException, IOException{ HttpEntity entity; String entityToString; entity = httpResponse.getEntity(); entityToString = EntityUtils.toString(entity); responseBody = JSON.parseObject(entityToString); System.out.println("This is your response body" + responseBody); return responseBody; } //以哈希圖的方式獲取到反饋頭部 public HashMap<String, String> getHeaderInHash(){ Header[] headers; headers = httpResponse.getAllHeaders(); responseHeads = new HashMap<String, String>(); for(Header header:headers){ responseHeads.put(header.getName(), header.getValue()); } System.out.println("This is your response header" + responseHeads); return responseHeads; } //獲取反饋狀態碼 public int getCodeInNumber(){ responseCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("This is your response code" + responseCode); return responseCode; } }
我們將代碼重新構造后,寫了四個方法:
- getResponse:發送請求並獲取反饋;
- getBodyInJSON:獲取JSON格式的反饋主體;
- getCodeInNumber:獲取反饋狀態碼;
- getHeaderInHash:獲取哈希圖形式的反饋頭;
后續我們考慮在測試類里面,直接調用這些方法。
3.2 引入JSON解析工具類
由於我們在獲取反饋主體時,是將其存為了JSON對象,在我們后續做驗證時,就需要去解讀這個JSON對象。
我們考慮創建一個工具類,專門用來做JSON解析。
在項目目錄src/main/java下創建一個包名為com.test.utils,新建JSONParser.java,代碼如下:
package com.test.utils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class JSONParser { JSONObject internalJSON; public String getCity(JSONObject jo){
String city = ""; try {
//先獲取反饋中的result這個一個內部JSON對象 JSONObject internalJSON = jo.getJSONObject("result"); //再根據鍵名查找鍵值 province = internalJSON.getString("city") ; }catch (Exception e){ e.printStackTrace(); } return city; } }
這里用到了fastjson庫,提前在pom中加入依賴即可。
由於我們測試的這個接口主要是用來查找手機歸屬地,所以考慮暫時我們只需要通過反饋中最關鍵的手機所屬城市來做驗證。
於是這個所謂的JSONParser工具類里,我們暫時只實現了查找‘城市’。
后續我們再考慮將這個工具進一步完善。
3.3 引入TestNG
上一篇中我們還沒有很好的執行測試和驗證結果,這里我們考慮引入testNG來幫助完成這部分功能。
3.3.1 創建TestNG測試
在項目目錄src/test/java下新建名為com.test.api的包,包下新建testNG類名為testGet.java。新建時帶上BeforeClass()注釋。
寫入以下測試代碼:
package com.test.api; import org.testng.annotations.Test; import com.alibaba.fastjson.JSONObject; import com.test.client.RestfulClient; import com.test.utils.JSONParser; import java.io.IOException; import java.net.URL; import org.apache.http.ParseException; import org.testng.Assert; import org.testng.annotations.BeforeClass; public class testGet { RestfulClient client; JSONObject responseBody; JSONParser jParser; int responseCode; String city; String url = "https://api.apishop.net/communication/phone/getLocationByPhoneNum?apiKey=nMke6NK29c40b1d1331690abb50b3eec8aa0808389b16c4&phoneNum=1861195236"; @Test public void TestGetRequest() {
//斷言反饋中城市是否正確 Assert.assertEquals(city, "北京");
//斷言反饋中的狀態碼是否正確 Assert.assertEquals(responseCode, 200); } @BeforeClass public void beforeClass() throws ParseException, IOException {
//發送請求,獲取反饋 client = new RestfulClient(); client.getResponse(url); responseBody = client.getBodyInJSON(); responseCode = client.getCodeInNumber();
//調用JSONParser獲取反饋中的城市信息 jParser = new JSONParser(); city = jParser.getCity(responseBody); } }
其中,我們在beforeclass里去做發送請求接收反饋,然后調用上一節中封裝好的方法獲取反饋頭部、主體和狀態碼。
然后通過JSONParser去獲取反饋中的所屬城市信息,做為核心驗證點。
在test里我們做兩個斷言:
1. 判斷狀態碼是否正確。
2. 判斷手機所屬城市是否正確。
3.3.2 TestNG測試結果
運行testNG測試,測試通過。
下一篇我們實現POST方法請求的測試,並進一步優化代碼。