1. 程式人生 > >介面自動化:HttpClient + TestNG + Java(三) - 初步封裝和testng斷言

介面自動化:HttpClient + TestNG + Java(三) - 初步封裝和testng斷言

在上一篇中,我們寫了第一個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.2.2 TestNG測試結果

執行testNG測試,測試通過。

 

 

下一篇我們實現POST方法請求的測試,並進一步優化程式碼。