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
-
<dependency>
-
<groupId>org.apache.httpcomponents</groupId>
-
<artifactId>httpclient</artifactId>
-
<version>4.5.6</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.apache.httpcomponents</groupId>
-
<artifactId>httpcore</artifactId>
-
<version>4.4.10</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.testng</groupId>
-
<artifactId>testng</artifactId>
-
<version>6.10</version>
-
</dependency>
-
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>fastjson</artifactId>
-
<version>1.2.29</version>
-
</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文件:
-
package com.qa.base;
-
-
import org.testng.TestException;
-
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.IOException;
-
import java.util.Properties;
-
-
public class TestBase {
-
public Properties prop;
-
-
public TestBase(){
-
try{
-
prop= new Properties();
-
FileInputStream fis= new FileInputStream(System.getProperty("user.dir")+"/src/main/java/com/qa/config/config.properties");
-
prop.load(fis);
-
} catch(FileNotFoundException e){
-
e.printStackTrace();
-
} catch(IOException e){
-
e.printStackTrace();
-
}
-
}
-
//mian函數主要是爲了檢測user.dir目錄是否正確,執行結果:E:\Java_project\MavenProject_script正是當前項目的目錄
-
public static void main(String[] args){
-
System.out.println(System.getProperty( "user.dir"));
-
}
-
-
}
上面咱們把加載配置文件的代碼寫在空參構造函數裏,好處就是,每初始化這個類的對象就會執行構造函數的代碼,即執行讀取配置文件這麼一個做用
3)新建一個RestClient.java類,實現get請求的代碼。在src/main/java下新建一個包:com.qa.restclient,最要實現:實現了get請求,和獲得相應狀態碼和響應頭信息,以及響應主體的json內容
-
package com.qa.restclient;
-
-
import com.alibaba.fastjson.JSON;
-
import com.alibaba.fastjson.JSONObject;
-
import org.apache.http.Header;
-
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 java.io.IOException;
-
import java.util.HashMap;
-
-
public class RestClient {
-
//1. Get 請求方法
-
public void get(String url) throws ClientProtocolException, IOException {
-
//建立一個可關閉的HttpClient對象
-
CloseableHttpClient httpclient= HttpClients.createDefault();
-
//建立一個HttpGet的請求對象
-
HttpGet httpget= new HttpGet(url);
-
//執行請求,至關於jmeter上點擊執行按鈕,而後賦值給HttpResponse對象接收
-
CloseableHttpResponse httpResponse=httpclient.execute(httpget);
-
//拿到Http響應狀態碼,例如和200,404,500去比較
-
int respinseStatusCode=httpResponse.getStatusLine().getStatusCode();
-
System.out.println( "response status code-->"+respinseStatusCode);
-
//把響應內容存儲在字符串對象
-
String responseString= EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
-
//建立Json對象,把上面字符串序列化成Json對象
-
JSONObject responseJson= JSON.parseObject(responseString);
-
System.out.println( "respon json from API->"+responseJson);
-
//獲取響應頭信息,返回是一個數組
-
Header[] headerArray=httpResponse.getAllHeaders();
-
//建立一個hashmap對象,經過jmeter能夠看到請求響應頭信息都是Key和value得形式,因此咱們想起了HashMap
-
HashMap<String,String> hm= new HashMap<String,String>();
-
//加強for循環遍歷headerArray數組,依次把元素添加到hashmap集合
-
for(Header header:headerArray){
-
hm.put(header.getName(),header.getValue());
-
}
-
//打印HashMap
-
System.out.println( "response headers-->"+hm);
-
-
-
}
-
-
}
4)寫一個測試類:GetApiTest.java類,在src/test/java下新建一個包:com.qa.tests。測試上面的get請求
-
package com.qa.tests;
-
-
import com.qa.base.TestBase;
-
import com.qa.restclient.RestClient;
-
import org.apache.http.client.ClientProtocolException;
-
import org.testng.annotations.BeforeClass;
-
import org.testng.annotations.Test;
-
-
import java.io.IOException;
-
-
-
public class GetApiTest extends TestBase {
-
TestBase testBase;
-
String host;
-
String url;
-
RestClient restClient;
-
-
@BeforeClass
-
public void setUp(){
-
testBase = new TestBase();
-
host=prop.getProperty( "HOST");
-
url=host+ "/api/users";
-
-
}
-
@Test
-
public void getAPITest() throws ClientProtocolException, IOException {
-
restClient= new RestClient();
-
restClient.get(url);
-
}
-
-
}
運行結果:
-
status code--> 200
-
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}
-
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"}
請求成功,目前的目錄以下 :