base包測試類的模板基類及常量類
package com.qa.base; import java.io.IOException; import java.util.Map; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.util.EntityUtils; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.alibaba.fastjson.JSONObject; import com.qa.base.Constants; import com.qa.model.Manager; import com.qa.restclient.RestClient; import com.qa.utils.PropertiesUtils; import com.qa.utils.fatjson.FastjsonUtils;
/** * 該類可以當成所有測試類的模板基類,其他需要測試的類繼承該類 * session,token等需要全域性使用的均需要在此類中進行定義;若測試需要登入可在本類進行登入 * @author jff * @date 2018年9月25日 * @version V1.0.1 */ public abstract class BaseApi { protected String host1; protected String host2; protected String url; protected static String sessionKey; @BeforeClass public void setUp() { host1=PropertiesUtils.getConfigValue("HOST"); host2=PropertiesUtils.getConfigValue("HOSTMANAGER"); } //由於登陸後後面的介面需要使用它的返回值,所以提取到此類,以後的測試類只需繼承,新增依賴即可 @Test public void loginManagerAppTestd() throws ClientProtocolException, IOException{
String url=host2+"/parkingManager/applogin/loginIn"; Manager manager = new Manager("yanczapp","8ddcff3a80f4189ca1c9d4d902c3c909","0571001"); Map<String, String> map=FastjsonUtils.toMap(FastjsonUtils.toJson(manager)); System.out.println("my out**********"+map); CloseableHttpResponse closeableHttpResponse = RestClient.postForm(url, map, null); //斷言狀態碼是不是200 int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); Assert.assertEquals(statusCode,Constants.RESPNSE_STATUS_CODE_200,"status code is not 200"); //斷言響應json內容中name和job是不是期待結果 String responseString = EntityUtils.toString(closeableHttpResponse.getEntity()); System.out.println("my out**********"+responseString); JSONObject res = FastjsonUtils.toJsonObject(responseString); sessionKey = FastjsonUtils.toMap(res.getString("data")).get("session_key"); System.out.println("data**********: " + res.getString("message")); System.out.println("data**********: " + sessionKey); String account=FastjsonUtils.toMap(res.getString("data")).get("account"); Assert.assertEquals(account,"yanczapp" ,"account code is not error"); } }
package com.qa.base;
/** * 常量類,相關的常量都統一放在該類中 * @author Administrator * */ public class Constants { //不要在程式碼裡寫死例狀態碼,用常量寫出來,方便每一個TestNG測試用例去呼叫去斷言 public static final int RESPNSE_STATUS_CODE_200 = 200; public static final int RESPNSE_STATUS_CODE_201 = 201; public static final int RESPNSE_STATUS_CODE_204 = 204; public static final int RESPNSE_STATUS_CODE_404 = 404; public static final int RESPNSE_STATUS_CODE_500 = 500; }