这套框架的报告是自己封装的
一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值
没有使用testng.xml的情况下调试testCase,需要设置一下dependsOnMethods,否则token将无法传递给其他test步骤
附上TestUtil.getToken()方法:
1 //获取返回的token ,使用JsonPath获取json路径 2 public static HashMap<String,String> getToken(CloseableHttpResponse closeableHttpResponse,String jsonPath) throws Exception{ 3 HashMap<String,String> responseToken = new HashMap<String, String>(); 4 String responseString = EntityUtils.toString( closeableHttpResponse.getEntity(),"UTF-8"); 5 ReadContext ctx = JsonPath.parse(responseString); 6 String Token = ctx.read(jsonPath); //"$.EFPV3AuthenticationResult.Token" 7 if(null == Token||"".equals(Token)){ 8 new Exception("token不存在"); 9 } 10 11 responseToken.put("x-ba-token",Token); 12 return responseToken; 13 }
![java 跨session token接口测试_编程_03](https://s2.51cto.com/images/blog/202107/19/33fb46503bedcecfd1c7dc8ea804e163.gif?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
这套框架的报告是自己封装的
一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值
没有使用testng.xml的情况下调试testCase,需要设置一下dependsOnMethods,否则token将无法传递给其他test步骤
附上TestUtil.getToken()方法:
1 //获取返回的token ,使用JsonPath获取json路径 2 public static HashMap<String,String> getToken(CloseableHttpResponse closeableHttpResponse,String jsonPath) throws Exception{ 3 HashMap<String,String> responseToken = new HashMap<String, String>(); 4 String responseString = EntityUtils.toString( closeableHttpResponse.getEntity(),"UTF-8"); 5 ReadContext ctx = JsonPath.parse(responseString); 6 String Token = ctx.read(jsonPath); //"$.EFPV3AuthenticationResult.Token" 7 if(null == Token||"".equals(Token)){ 8 new Exception("token不存在"); 9 } 10 11 responseToken.put("x-ba-token",Token); 12 return responseToken; 13 }
<!-- 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>