【親測】Java 接口自動化步驟

GET請求(Maven)java

1、 src/main/javaapache

一、【地址--HOST】建立並設置配置文件(後綴名.properties的文本文件):存放接口請求的host地址json

二、【地址】TestBase.java類,全部接口請求測試的父類,寫一個構造方法,實現加載讀取properties文件,配置文件的代碼寫在空參構造函數裏,好處就是,每初始化這個類的對象就會執行構造函數的代碼,即執行讀取配置文件這麼一個做用api

三、【請求方式】RestClient.java類,實現get請求的代碼,和獲得相應狀態碼和響應頭信息,以及響應主體的json內容數組

2、src/test/java網絡

一、【報告】測試類:GetApiTest.java類,測試上面的get請求app

 

其餘大佬的實例框架

一、前提條件:dom

(1).本機環境安裝了maven並配置環境變量,若是是idea不用安裝,已經集成了maven

(2).本機環境安裝了idea軟件

(3).本機環境安裝了Java jdk 8版本

(4).本機須要能鏈接上互聯網

新建maven項目就不寫了,前面ui自動化寫過了

二、添加必要的依賴包:httpclient、httpcore、Fastjson、Testng

  1.  
    <dependency>
  2.  
    <groupId>org.apache.httpcomponents</groupId>
  3.  
    <artifactId>httpclient</artifactId>
  4.  
    <version>4.5.6</version>
  5.  
    </dependency>
  6.  
     
  7.  
    <dependency>
  8.  
    <groupId>org.apache.httpcomponents</groupId>
  9.  
    <artifactId>httpcore</artifactId>
  10.  
    <version>4.4.10</version>
  11.  
    </dependency>
  12.  
     
  13.  
    <dependency>
  14.  
    <groupId>org.testng</groupId>
  15.  
    <artifactId>testng</artifactId>
  16.  
    <version>6.10</version>
  17.  
    </dependency>
  18.  
     
  19.  
    <dependency>
  20.  
    <groupId>com.alibaba</groupId>
  21.  
    <artifactId>fastjson</artifactId>
  22.  
    <version>1.2.29</version>
  23.  
    </dependency>

Httpcore主要是網絡相關的組件,咱們使用Http請求就須要網絡相關底層方法。Testng主要是是一個單元測試框架,方便咱們編寫接口自動化用例。Fastjson是阿里巴巴的一個json的開源的組件,聽說是最快的json的組件,主要用來json序列化和反序列操做。

三、寫一個get請求的例子

(1)打開網站:https://reqres.in/,往下拉看到如圖所示:

經過這個圖,咱們可以獲取這些信息

1)網站host地址:https://reqres.in/

2)用戶展現請求方式是: Get

3)接口的url 是: /api/users

4)接口的響應狀態碼是200,還能夠看到響應body的JSON內容。

有了這些信息咱們能夠在Jmeter或者postman上面來測試一下:

執行一下:

和網站圖片上顯示同樣,說明測試經過了。

(2)開始寫代碼:

1)設計配置文件:在src/main/java下新建一個包:com.qa.config,而後在新包下新建一個config.properties文件,文件內容以下。

2)新建一個TestBase.java,在src/main/java下新建一個包:com.qa.base包,將這個類寫在該包中,該類做爲全部接口請求測試的父類,都須要繼承這個父類。目前咱們就寫一個構造方法,實現加載讀取properties文件:

  1.  
    package com.qa.base;
  2.  
     
  3.  
    import org.testng.TestException;
  4.  
     
  5.  
    import java.io.FileInputStream;
  6.  
    import java.io.FileNotFoundException;
  7.  
    import java.io.IOException;
  8.  
    import java.util.Properties;
  9.  
     
  10.  
    public class TestBase {
  11.  
    public Properties prop;
  12.  
     
  13.  
    public TestBase(){
  14.  
    try{
  15.  
    prop= new Properties();
  16.  
    FileInputStream fis= new FileInputStream(System.getProperty("user.dir")+"/src/main/java/com/qa/config/config.properties");
  17.  
    prop.load(fis);
  18.  
    } catch(FileNotFoundException e){
  19.  
    e.printStackTrace();
  20.  
    } catch(IOException e){
  21.  
    e.printStackTrace();
  22.  
    }
  23.  
    }
  24.  
    //mian函數主要是爲了檢測user.dir目錄是否正確,執行結果:E:\Java_project\MavenProject_script正是當前項目的目錄
  25.  
    public static void main(String[] args){
  26.  
    System.out.println(System.getProperty( "user.dir"));
  27.  
    }
  28.  
     
  29.  
    }

上面咱們把加載配置文件的代碼寫在空參構造函數裏,好處就是,每初始化這個類的對象就會執行構造函數的代碼,即執行讀取配置文件這麼一個做用

3)新建一個RestClient.java類,實現get請求的代碼。在src/main/java下新建一個包:com.qa.restclient,最要實現:實現了get請求,和獲得相應狀態碼和響應頭信息,以及響應主體的json內容

  1.  
    package com.qa.restclient;
  2.  
     
  3.  
    import com.alibaba.fastjson.JSON;
  4.  
    import com.alibaba.fastjson.JSONObject;
  5.  
    import org.apache.http.Header;
  6.  
    import org.apache.http.client.ClientProtocolException;
  7.  
    import org.apache.http.client.methods.CloseableHttpResponse;
  8.  
    import org.apache.http.client.methods.HttpGet;
  9.  
    import org.apache.http.impl.client.CloseableHttpClient;
  10.  
    import org.apache.http.impl.client.HttpClients;
  11.  
    import org.apache.http.util.EntityUtils;
  12.  
     
  13.  
    import java.io.IOException;
  14.  
    import java.util.HashMap;
  15.  
     
  16.  
    public class RestClient {
  17.  
    //1. Get 請求方法
  18.  
    public void get(String url) throws ClientProtocolException, IOException {
  19.  
    //建立一個可關閉的HttpClient對象
  20.  
    CloseableHttpClient httpclient= HttpClients.createDefault();
  21.  
    //建立一個HttpGet的請求對象
  22.  
    HttpGet httpget= new HttpGet(url);
  23.  
    //執行請求,至關於jmeter上點擊執行按鈕,而後賦值給HttpResponse對象接收
  24.  
    CloseableHttpResponse httpResponse=httpclient.execute(httpget);
  25.  
    //拿到Http響應狀態碼,例如和200,404,500去比較
  26.  
    int respinseStatusCode=httpResponse.getStatusLine().getStatusCode();
  27.  
    System.out.println( "response status code-->"+respinseStatusCode);
  28.  
    //把響應內容存儲在字符串對象
  29.  
    String responseString= EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
  30.  
    //建立Json對象,把上面字符串序列化成Json對象
  31.  
    JSONObject responseJson= JSON.parseObject(responseString);
  32.  
    System.out.println( "respon json from API->"+responseJson);
  33.  
    //獲取響應頭信息,返回是一個數組
  34.  
    Header[] headerArray=httpResponse.getAllHeaders();
  35.  
    //建立一個hashmap對象,經過jmeter能夠看到請求響應頭信息都是Key和value得形式,因此咱們想起了HashMap
  36.  
    HashMap<String,String> hm= new HashMap<String,String>();
  37.  
    //加強for循環遍歷headerArray數組,依次把元素添加到hashmap集合
  38.  
    for(Header header:headerArray){
  39.  
    hm.put(header.getName(),header.getValue());
  40.  
    }
  41.  
    //打印HashMap
  42.  
    System.out.println( "response headers-->"+hm);
  43.  
     
  44.  
     
  45.  
    }
  46.  
     
  47.  
    }

4)寫一個測試類:GetApiTest.java類,在src/test/java下新建一個包:com.qa.tests。測試上面的get請求

  1.  
    package com.qa.tests;
  2.  
     
  3.  
    import com.qa.base.TestBase;
  4.  
    import com.qa.restclient.RestClient;
  5.  
    import org.apache.http.client.ClientProtocolException;
  6.  
    import org.testng.annotations.BeforeClass;
  7.  
    import org.testng.annotations.Test;
  8.  
     
  9.  
    import java.io.IOException;
  10.  
     
  11.  
     
  12.  
    public class GetApiTest extends TestBase {
  13.  
    TestBase testBase;
  14.  
    String host;
  15.  
    String url;
  16.  
    RestClient restClient;
  17.  
     
  18.  
    @BeforeClass
  19.  
    public void setUp(){
  20.  
    testBase = new TestBase();
  21.  
    host=prop.getProperty( "HOST");
  22.  
    url=host+ "/api/users";
  23.  
     
  24.  
    }
  25.  
    @Test
  26.  
    public void getAPITest() throws ClientProtocolException, IOException {
  27.  
    restClient= new RestClient();
  28.  
    restClient.get(url);
  29.  
    }
  30.  
     
  31.  
    }

運行結果:

  1.  
    status code--> 200
  2.  
    respon json from API->{ "per_page":3,"total":12,"data":[{"last_name":"Bluth","id":1,"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg","first_name":"George"},{"last_name":"Weaver","id":2,"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg","first_name":"Janet"},{"last_name":"Wong","id":3,"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg","first_name":"Emma"}],"page":1,"total_pages":4}
  3.  
    response headers-->{Transfer-Encoding=chunked, Server=cloudflare, CF-RAY= 459a199309239559-NRT, Access-Control-Allow-Origin=*, ETag=W/"1bb-D+c3sZ5g5u/nmLPQRl1uVo2heAo", Connection=keep-alive, Set-Cookie=__cfduid=d746298a777ed31a0deaa8ed5264067471536836319; expires=Fri, 13-Sep-19 10:58:39 GMT; path=/; domain=.reqres.in; HttpOnly, Date=Thu, 13 Sep 2018 10:58:39 GMT, Content-Type=application/json; charset=utf-8, X-Powered-By=Express, Expect-CT=max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"}

請求成功,目前的目錄以下 :

相關文章
相關標籤/搜索