1. 程式人生 > 程式設計 >Java跨session實現token介面測試過程圖解

Java跨session實現token介面測試過程圖解

這套框架的報告是自己封裝的

一般token會在登入介面返回結果中呈現,從程式碼層面獲取token的方式有很多種,我是使用jsonpath這個json路徑語言去匹配token所在路徑的key值

Java跨session實現token介面測試過程圖解

Java跨session實現token介面測試過程圖解

沒有使用testng.xml的情況下除錯testCase,需要設定一下dependsOnMethods,否則token將無法傳遞給其他test步驟

附上TestUtil.getToken()方法:

 //獲取返回的token,使用JsonPath獲取json路徑
   public static HashMap<String,String> getToken(CloseableHttpResponse closeableHttpResponse,String jsonPath) throws Exception{
     HashMap<String,String> responseToken = new HashMap<String,String>();
     String responseString = EntityUtils.toString( closeableHttpResponse.getEntity(),"UTF-8");
     ReadContext ctx = JsonPath.parse(responseString);
     String Token = ctx.read(jsonPath); //"$.EFPV3AuthenticationResult.Token"
     if(null == Token||"".equals(Token)){
       new Exception("token不存在");
     } 
     responseToken.put("x-ba-token",Token);
     return responseToken;
   }

maven地址

<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.1</version>
</dependency>

介面測試神器:https://github.com/wiztools/rest-client

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。