1. 程式人生 > >接口自動化框架(java)--4.接口Token傳遞

接口自動化框架(java)--4.接口Token傳遞

pst log utf excel 封裝 使用 配置 eth article

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

一般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

接口自動化框架(java)--4.接口Token傳遞