2018-04-22接口自動化測試學習心得(1)

根據接口文檔寫接口測試用例-->添加接口自動化測試項目相關依賴(httpclient+testng+poi-ooxml+log4j+mail+mysql-connector-java)-->寫接口測試方法-->執行測試java

-- 接口測試
1.一個接口就是一個函數
2.咱們要保證一個接口可以在url地址欄裏面訪問到,必須知足一下兩個條件
一是這個接口首先是部署到服務器上的web容器中,
而是此接口必須實現了http協議
-- 接口文檔
接口地址
請求方式
參數
變量名
類型
說明
備註

-- 接口測試工具:
soapui,jmeter,pastman,fiddler
都操做一次
soapui:準備url建立項目,根據接口文檔肯定提交的方式,準備傳參,發送請求,獲取接口返回數據

-- ui層面的功能測試跟接口測試的區別:
1.ui層面能夠經過頁面填寫數據,傳數據到後臺,
可是接口測試沒有頁面,咱們只能藉助接口測試工具模擬頁面去提交接口數據。
2.UI層面的功能測試會有後臺接口返回的數據,而且數據會經過js以提示信息的方式顯示在頁面幫助用戶或者測試人員去判斷功能是否正常。
可是接口測試就沒有頁面提示信息,咱們能看到的是接口返回的最原始的數據。

接口自動化實現:
java有專門的第三方框架能夠提供了一套基於http協議的接口請求和處理技術。--httpclient

-- 環境搭建
建立maven項目-eclipse集成testng-集成testng框架
添加依賴
    <dependencies>
        <!-- 管理用例,提供多種測試場景實現 -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
            <scope>test</scope>
        </dependency>
        <!-- 接口提交(get/post) -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <!-- 解析excel文件 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <!-- 記錄日誌框架 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- 發送郵件框架 -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>
        <!-- jdbc技術,數據庫驅動 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

-- httpPost
1,以post方式提交請求到服務器
2,參數封裝到請求體當中
3,數據長度是沒有限制的
4,支持post方式提交的接口每每是吧數據提交到服務器
關鍵api函數
構造函數:
HttpPost post=new HttpPost(url);
建立一個post對象,以post方式提交接口請求
設置參數到請求體
post.setEntity(new UrlEncodedFormEntity(params));
經過此方法將接口參數設置到請求中

-- httpGet
1,以get方式提交請求到服務器
2,get參數不是封裝在請求體當中的(由沒有setEntify方法就能夠看出)
3,數據長度是有限制的
4,支持get提交的接口通常都是從服務器拿下來數據
關鍵api函數
構造函數:
HttpGet httpGet=new HttpGet(interfacesurl);
建立一個get對象,以get方式提交接口請求
注意:若是以get提交的接口請求有須要傳參,蠶食一般是直接拼接在url後面
例子
public class RegisterInterface {
    @Test
    public void test1() throws Exception{
        //準備url;
        String url="http://8080/lmcanon_web_auto/mvc/member/api/member";
        //接口提交的方式;
        HttpPost post=new HttpPost(url);
        //準備傳參;
        List<NameValuePair>params=new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("mobilephone", "135173"));
        params.add(new BasicNameValuePair("pwd", "e10adc3949ba59abbe56e057f"));
        //將參數設置到post請求中
        post.setEntity(new UrlEncodedFormEntity(params));
        //發送請求;
        CloseableHttpClient httpclient=HttpClients.createDefault();
        CloseableHttpResponse response=httpclient.execute(post);
        //獲取接口返回數據.
        String jsonStr=EntityUtils.toString(response.getEntity());
        System.out.println(jsonStr);
    }}
    
    @Test
    public void get() throws Exception{
        //定義接口地址
        String interfacesurl="http://8080/futureload/mvc/api/member/list";
        //決定接口提交方式
        HttpGet httpGet=new HttpGet(interfacesurl);
        //直接提交接口請求,準備客戶端
        CloseableHttpClient httpClient=HttpClients.createDefault();
        CloseableHttpResponse response=httpClient.execute(httpGet);
        //獲取相應數據
        String jsonString=EntityUtils.toString(response.getEntity());
        System.out.println(jsonString);
    }}
    
--httpRequeste
接口類型,表示是從客戶端發送服務端請求
HttpGet和httpPost都是httpRequest實現類,屬於子類對象

-- httpResponse    
接口類型,表示是從服務端到客戶端的響應
從響應對象中獲取返回數據:getEntity()
從響應對象中獲取響應狀態:getStatusLine()
從響應對象中獲取響應頭信息:getAllHeaders()
從響應對象中設置響應頭信息:setHeader

-- 93框架實現步驟
1,testng管理用例
2,實現httpUtil類提供公共的方法處理post和get請求
3,設計測試用例
4,實現數據驅動(獲取url,接口類型,傳參,封裝成方法)
5,接口測試數據回寫
6,日誌集成
7,報表集成
8,jenkins集成

--get和post公共方法
public static String getResultStringByPost(String uri,List<NameValuePair>params){
        //調用函數StringUtils.isEmpty(uri),對uri是否爲空的處理
        if(StringUtils.isEmpty(uri)){
            return "";
            }
        //建立httpPost對象
        HttpPost httpPost=new HttpPost(uri);
        String resultString="";
        //設置參數到httpPost對象中
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            //準備httpclient客戶端
            CloseableHttpClient httpClient= HttpClients.createDefault();
            //發送請求
            CloseableHttpResponse response=httpClient.execute(httpPost);
            //解析數據
            resultString=EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultString;
        
    }
    /**get請求通用處理函數
     * @param uri:接口地址
     * @param params:提交參數
     * @return:接口響應數據
     */
    public static String getResultStringByGet(String uri,List<NameValuePair>params){
        //調用函數StringUtils.isEmpty(uri),對uri是否爲空的處理
        if(StringUtils.isEmpty(uri)){
            return "";
            }
        if(params==null){
            return "";
        }
        for (int i=0;i<params.size();i++) {
            NameValuePair nameValuePair=params.get(i);
            if(i==0){
            uri+=("?"+nameValuePair.getName()+"="+nameValuePair.getValue());
            }else{
                uri+=("&"+nameValuePair.getName()+"="+nameValuePair.getValue());
            }
        }
        //建立httpGet對象
        HttpGet httpGet=new HttpGet(uri);
        String resultString="";
        //設置參數到httpPost對象中
        try {
//            httpGet.setEntity(new UrlEncodedFormEntity(params));
            //準備httpclient客戶端
            CloseableHttpClient httpClient= HttpClients.createDefault();
            //發送請求
            CloseableHttpResponse response=httpClient.execute(httpGet);
            //解析數據
            resultString=EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultString;
        
    }
//StringUtils判空方法
public class StringUtils {
    public static boolean isEmpty(String content){
        boolean isEmpty=(content==null||content!=null&&content.trim().length()==0);
        return isEmpty;
    }
}

mysql

相關文章
相關標籤/搜索