一、封装公共参数
1.登录前公共参数
import java.util.TreeMap; public class RequestCommonParams { public TreeMap<String, Object> staticParams; public TreeMap<String, Object> getStaticParams() { return staticParams; } public void setStaticParams(TreeMap<String, Object> staticParams) { this.staticParams = staticParams; } /** * 移动端公共参数 * @return TreeMap */ public RequestCommonParams(){ TreeMap<String, Object> map = new TreeMap<>(); map.put("clientMac", "9e:ee:fb:0f:5b:b8"); map.put("clientDensity", "3.0"); map.put("latitude", "255"); map.put("longitude", "255"); map.put("isJailbroken", "0"); map.put("JailReason", "NO Jail"); map.put("clientVersion", "3.1.0"); map.put("deviceId", "9e:ee:fb:0f:5b:b8"); map.put("platform", "Android"); //Android map.put("deviceInfo", "{"deviceModel":"MI 5","deviceOs":"23_6.0.1"}"); map.put("network", "wifi"); map.put("screenSize", "1920*1080"); map.put("clientIp", "10.0.3.15"); setStaticParams(map); } }
2.登录后所有公共参数封装
import java.util.TreeMap; import static com.uc.qa.cases.assistant.BaseParameters.lendRequest; public class SalesRequestRunTimeParamsV3 extends SalesRequestCommonParamsV3 { public TreeMap<String,Object> runTimeParams; public TreeMap<String, Object> getRunTimeParams() { return runTimeParams; } public void setRunTimeParams(TreeMap<String, Object> runTimeParams) { this.runTimeParams = runTimeParams; } public SalesRequestRunTimeParamsV3() { //将Token赋值给customer-token staticParams.put("customer-token", lendRequest.getToken());//下面有数据来源
//将sellerId赋值给userId staticParams.put("userId", lendRequest.getSellerId()); staticParams.put("timestamp", String.valueOf(System.currentTimeMillis())); setRunTimeParams(staticParams); setStaticParams(null); } }
二、调用公共参数
1.创建map对象,并添加入参,最后输出
TreeMap<String, Object> map = (new SalesRequestCommonParams()).getStaticParams(); //新增入参 map.put("username", "chu01"); map.put("password", 123);
System.out.println("---------------map is-------------"+map);
---------------map is-------------{JailReason=NO Jail, clientDensity=3.0, clientIp=10.0.3.15, clientMac=9e:ee:fb:0f:5b:b8, clientVersion=3.1.0, deviceId=9e:ee:fb:0f:5b:b8, deviceInfo={"deviceModel":"MI 5","deviceOs":"23_6.0.1"}, isJailbroken=0, latitude=255, longitude=255, network=wifi, password=123, platform=Android, screenSize=1920*1080, username=10057340}
2.创建MultiValueMap一键多值引用
TreeMap2MultiValueMap treeMap2MultiValueMap = new TreeMap2MultiValueMap(); MultiValueMap<String, Object> body = treeMap2MultiValueMap.getMultiValueMap(map);
System.out.println("----------body is----------------"+body);
System.out.println("---------------------body.getClass------is------"+body.getClass());
Reporter.log("Request URL is :"+body.toSingleValueMap().toString(),true);
----------body is----------------{JailReason=[NO Jail], clientDensity=[3.0], clientIp=[10.0.3.15], clientMac=[9e:ee:fb:0f:5b:b8], clientVersion=[3.1.0], deviceId=[9e:ee:fb:0f:5b:b8], deviceInfo=[{"deviceModel":"MI 5","deviceOs":"23_6.0.1"}], isJailbroken=[0], latitude=[255], longitude=[255], network=[wifi], password=[123], platform=[Android], screenSize=[1920*1080], username=[10057340]}
---------------------body.getClass------is------class org.springframework.util.LinkedMultiValueMap
{JailReason=NO Jail, clientDensity=3.0, clientIp=10.0.3.15, clientMac=9e:ee:fb:0f:5b:b8, clientVersion=3.1.0, deviceId=9e:ee:fb:0f:5b:b8, deviceInfo={"deviceModel":"MI 5","deviceOs":"23_6.0.1"}, isJailbroken=0, latitude=255, longitude=255, network=wifi, password=123, platform=Android, screenSize=1920*1080, username=11111111}
3.将body转化成请求实体
HttpEntity<?> request = new HttpEntity<>(body);
System.out.println("---------------request------is----------------"+request);
System.out.println("---------------request.getClass------is-------"+request.getClass());
---------------request------is----------------<{JailReason=[NO Jail], clientDensity=[3.0], clientIp=[10.0.3.15], clientMac=[9e:ee:fb:0f:5b:b8], clientVersion=[3.1.0], deviceId=[9e:ee:fb:0f:5b:b8], deviceInfo=[{"deviceModel":"MI 5","deviceOs":"23_6.0.1"}], isJailbroken=[0], latitude=[255], longitude=[255], network=[wifi], password=[123], platform=[Android], screenSize=[1920*1080], username=[10057340]},{}>
---------------request.getClass------is-------class org.springframework.http.HttpEntity
三、发送请求
/** * 全局请求处理器 */ public static UcRESTTemplate BASE_REST;
ResponseEntity<String> responseEntity =BASE_REST.postEntity(域名+接口地址,request);
Reporter.log("Get Response is:"+responseEntity.toString(),true);
assertNotNull(responseEntity.getBody());
Get Response is:<200, { "code":0, "data":Object{...}, "mask":"dfsdfsdfsdfsfsdfds23123123", "msg":"success", "timestamp":1566269542 } ,{Server=[Tengine/2.1.1], Date=[Tue, 20 Aug 2019 02:52:22 GMT], Content-Type=[application/json;charset=UTF-8], Content-Length=[820], Connection=[keep-alive]}>
四、返回处理
1.返回响应校验,以及提取
{ "code":0, "data":{ "appAllowed":true, "approved":false, "createTime":00000000000000, "employeeNumber":"000000000000", "enabled":true, "errorCode":0, "errorMessage":"", "groupId":39, "groupName":"地址", "head_location":"", "id":"32312343532423432", "mail":"4234234234324234", "manager":false, "managerName":"姓名", "managerOf":[ ], "mobile":"19866664321", "mobx":"232", "name":"姓名", "oldGroupId":0, "positionChange":"", "postOfficeBox":0, "role":0, "secretKey":"HGHHKHKHKHKH6969797979878", "showManager":true, "syncTime":1564994711000, "title":"职位", "userType":"", "username":"10057340", "uuid":"3213123-3213123-312312-313213", "version":0 }, "mask":"3213123-32132-13-21-312-3-213-21", "msg":"success", "timestamp":1566269542 }
/** * 全局运行时环境参数管理器 */ public static Map<String, String> BASE_GLOBAL_MAP;
Reporter.log("登录成功!username="+lendRequest.getUsername(),true); //取出body部分 String content = responseEntity.getBody();
//将body部分转化成json对象 JSONObject json = JSONObject.parseObject(content);
//比较code是否等于0 assertEquals(0,json.getInteger("code").intValue()); assertTrue("success".equalsIgnoreCase(json.getString("msg")));
//取出data部分,并赋予一个json对象 JSONObject data = json.getJSONObject("data");
//取出uuid赋值给Token lendRequest.setToken(data.getString("uuid")); lendRequest.setUsername(data.getString("employeeNumber")); lendRequest.setSellerId(data.getString("id")); BASE_GLOBAL_MAP.put("secret",data.getString("secretKey"));
2.介绍intValue,parseInt,valueOf
//intValue()是把Integer对象类型变成int的基础数据类型; //parseInt()是把String 变成int的基础数据类型; //Valueof()是把给定的String参数转化成Integer对象类型; public class TestValuesOf { public static void main(String[] args) { Integer i =100; String str ="111"; System.out.println(i.intValue()); System.out.println(Integer.parseInt(str)); System.out.println(Integer.valueOf(str)+" "+Integer.valueOf(str).getClass()); } } ------------------------------------------------- 100 111 111 class java.lang.Integer