这套框架的报告是自己封装的
一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值
1 package com.qa.tests; 2 3 4 import com.alibaba.fastjson.JSON; 5 import com.qa.base.TestBase; 6 import com.qa.Parameters.postParameters; 7 import com.qa.restclient.RestClient; 8 import com.qa.util.TestUtil; 9 import org.apache.http.client.methods.CloseableHttpResponse; 10 import org.testng.Assert; 11 import org.testng.Reporter; 12 import org.testng.annotations.*; 13 import java.io.IOException; 14 import java.util.HashMap; 15 import static com.qa.util.TestUtil.dtt; 16 17 public class EFPStagingCN extends TestBase { 18 19 TestBase testBase; 20 RestClient restClient; 21 CloseableHttpResponse closeableHttpResponse; 22 //host 23 String host; 24 //Excel路径 25 String testCaseExcel; 26 //token路径 27 String tokenPath; 28 //header 29 HashMap<String ,String> postHeader = new HashMap<String, String>(); 30 //登录token 31 HashMap<String,String> loginToken = new HashMap<String, String>(); 32 @BeforeClass 33 public void setUp(){ 34 testBase = new TestBase(); 35 postHeader.put("Content-Type","application/json"); 36 restClient = new RestClient(); 37 //接口endpoint 38 host = prop.getProperty("Host"); 39 //读取配置文件Excel路径 40 testCaseExcel=prop.getProperty("testCase1data"); 41 //读取配置文件token路径 42 tokenPath = prop.getProperty("token"); 43 } 44 45 @DataProvider(name = "postData") 46 public Object[][] post() throws IOException { 47 return dtt(testCaseExcel,0); 48 49 } 50 @DataProvider(name = "getData") 51 public Object[][] get() throws IOException{ 52 return dtt(testCaseExcel,1); 53 54 } 55 56 @DataProvider(name = "deleteData") 57 public Object[][] delete() throws IOException{ 58 return dtt(testCaseExcel,2); 59 } 60 61 62 63 @Test(dataProvider = "postData") 64 public void login(String loginUrl,String username, String passWord) throws Exception { 65 postParameters loginParameters = new postParameters(username,passWord); 66 String userJsonString = JSON.toJSONString(loginParameters); 67 //发送登录请求 68 closeableHttpResponse = restClient.postApi(host+loginUrl,userJsonString,postHeader); 69 //获取登录后的token 70 loginToken = TestUtil.getToken(closeableHttpResponse,tokenPath); 71 int statusCode = TestUtil.getStatusCode(closeableHttpResponse); 72 Assert.assertEquals(statusCode,200); 73 74 } 75 76 @Test(dataProvider = "getData",dependsOnMethods = {"login"}) 77 public void getMothed(String url) throws Exception{ 78 //将token赋值后发送get请求 79 closeableHttpResponse = restClient.getApi(host+url,loginToken); 80 int statusCode = TestUtil.getStatusCode(closeableHttpResponse); 81 Assert.assertEquals(statusCode,200); 82 } 83 84 @Test(dataProvider = "deleteData",dependsOnMethods = {"getMothed"}) 85 public void deleteMothed(String url) throws IOException{ 86 closeableHttpResponse = restClient.deleteApi(url); 87 int statusCode = TestUtil.getStatusCode(closeableHttpResponse); 88 Assert.assertEquals(statusCode,204); 89 } 90 91 92 }
没有使用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://blog.csdn.net/qq_34693151/article/details/81906177
原文地址:https://www.cnblogs.com/111testing/p/10624778.html
时间: 2024-11-06 18:49:27