1. 程式人生 > >JAVA介面自動化框架3——session提取及傳遞

JAVA介面自動化框架3——session提取及傳遞

使用場景:

一些場景必須登入後才可以使用,這就需要在後續的介面中使用登入後產生的session,本章主要介紹如何獲取session,及把引數傳遞到其他的介面中;我的介面是查詢介面,無引數

一、登入需要的引數

package com.qa.parameters; public class Manager {     private String account;     private String loginPwd;     private String partnerCode;          public Manager(){              }          public Manager(String account,String loginPwd,String partnerCode){         super();         this.loginPwd=loginPwd;         this.account=account;         this.partnerCode=partnerCode;     }          public String getAccount() {         return account;     }     public void setAccount(String account) {         this.account = account;     }     public String getLoginPwd() {         return loginPwd;     }     public void setLoginPwd(String loginPwd) {         this.loginPwd = loginPwd;     }     public String getPartnerCode() {         return partnerCode;     }     public void setPartnerCode(String partnerCode) {         this.partnerCode = partnerCode;     }  }

二、在base類中定義session,我把他放在此處的想法是因為後續的test類都需要繼承此類,若在此定義了即為全域性變數,後續只要使用到session的地方可以直接使用;把登入也放到此類也是這種想法

package com.qa.base; import java.io.IOException; import java.util.Map; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.util.EntityUtils; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.alibaba.fastjson.JSONObject; import com.qa.parameters.Manager; import com.qa.restclient.RestClient; import com.qa.utils.PropertiesUtils; import com.qa.utils.fatjson.FastjsonUtils;

/**  * 該類可以當成所有測試類的模板基類,其他需要測試的類繼承該類  * session,token等需要全域性使用的均需要在此類中進行定義;若測試需要登入可在本類進行登入  * @author jff  * @date 2018年9月25日  * @version V1.0.1  */ public abstract class BaseApi {     protected String hostManager;     protected static String sessionKey;          @BeforeClass     public void setUp() {         hostManager=PropertiesUtils.getConfigValue("HOSTMANAGER");             }          //由於登陸後後面的介面需要使用它的返回值,所以提取到此類,以後的測試類只需繼承,新增依賴即可     @Test     public void loginManagerAppTestd() throws ClientProtocolException, IOException{

        String url=hostManager+"/parkingManager/applogin/loginIn";         Manager manager = new Manager("yanczapp","8ddcff3a80f4189ca1c9d4d902c3c909","0571001");         Map<String, String> map=FastjsonUtils.toMap(FastjsonUtils.toJson(manager));         System.out.println("my out**********"+map);         CloseableHttpResponse closeableHttpResponse = RestClient.postForm(url, map, null);                  //斷言狀態碼是不是200         int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();         Assert.assertEquals(statusCode,Constants.RESPNSE_STATUS_CODE_200,"status code is not 200");              //斷言響應json內容中name和job是不是期待結果         String responseString = EntityUtils.toString(closeableHttpResponse.getEntity());         System.out.println("my out**********"+responseString);         JSONObject res = FastjsonUtils.toJsonObject(responseString);

         //提取session,並把它賦值給sessionkey,sessionKey 作為全域性變數其他介面可以直接使用         sessionKey = FastjsonUtils.toMap(res.getString("data")).get("session_key");         System.out.println("data**********: " + res.getString("message"));         System.out.println("data**********: " + sessionKey);                  String account=FastjsonUtils.toMap(res.getString("data")).get("account");         Assert.assertEquals(account,"yanczapp" ,"account code is not error");     } }

三、config.properties

HOSTMANAGER=http://m.test.mwpark.cn

四、編寫測試類,這個需要新增一個依賴,依賴登入介面,意思就是說登入介面成功後此用例才能執行

新增dependsOnMethods即可

package manager; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import org.testng.Assert; import org.testng.annotations.Test; import com.alibaba.fastjson.JSONObject; import com.qa.base.BaseApi; import com.qa.base.Constants; import com.qa.restclient.RestClient; import com.qa.utils.fatjson.FastjsonUtils;

public class LoginManagerAppTest2 extends BaseApi{     private final static Logger Log=Logger.getLogger(LoginManagerAppTest2.class);        //依賴那個方法,如果依賴多個方法可使用逗號分隔loginManagerAppTestd,loginManagerApp     @Test(dependsOnMethods = {"loginManagerAppTestd"})     public void query() throws ClientProtocolException, IOException {         String url=hostManager+"/parkingManager/apprecovered/querySumOrderAmountByCondition";                           Map<String, String> headers = new HashMap<String, String>();         headers.put("token", "aaaa");                  Map<String, String> params = new HashMap<String, String>();         params.put("session_key", sessionKey);         CloseableHttpResponse closeableHttpResponse = RestClient.postForm(url, params, headers);                  //斷言響應是不是期待結果         String responseString = EntityUtils.toString(closeableHttpResponse.getEntity());         Log.info("my out**********"+responseString);         JSONObject res1  = FastjsonUtils.toJsonObject(responseString);         Log.info("code**********: " + res1.getString("code"));                  //斷言狀態碼是不是200         int statusCode = RestClient.getStatusCode(closeableHttpResponse);         Assert.assertEquals(statusCode,Constants.RESPNSE_STATUS_CODE_200,"status code is not 200");     }   }