java 跨session token接口测试

这套框架的报告是自己封装的

一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

没有使用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     }
复制代码

这套框架的报告是自己封装的

一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

没有使用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     }
复制代码
 
来源:清风软件测试
 
 
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>

 

猜你喜欢

转载自www.cnblogs.com/zgq123456/p/12614451.html