tester999 我贴一个示例吧,对于新接触的小白比如我还是很有用的,因为做UI的很多都没有接触过java或者groovy,平台的写法又跟两种语言有点不一样😂,看前面的官方文档和下面示例应该能很快完成一些必须用接口调用才能完成UI自动化的中间步骤
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.cloud.sonic.agent.tools.SpringTool;
import org.springframework.web.client.RestTemplate;
import org.cloud.sonic.agent.tests.LogUtil;
import org.cloud.sonic.agent.common.interfaces.StepType;
import static org.testng.Assert.*;
def login(userName,password){
LogUtil log = androidStepHandler.log
RestTemplate restTemplate = SpringTool.getBean(RestTemplate.class)
String url = "https://gateway-mice.smartmice.cn/identity/login";
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
String bodyy = "client_id=xxx&client_secret=xxx&grant_type=password&userName=" + userName + "&password=" + password;
HttpEntity entity = new HttpEntity(bodyy, headers);
JSONObject result = restTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class).getBody();
String token = result.getString("access_token")
String userId = result.getString("userId")
androidStepHandler.globalParams.put("access_token",token)
androidStepHandler.globalParams.put("userId",userId)
// String token= androidStepHandler.globalParams.getString("access_token")
//String userid = androidStepHandler.globalParams.getString("userId")
androidStepHandler.log.sendStepLog(1, "获取全局参数token", "值:" + token)
androidStepHandler.log.sendStepLog(1, "获取全局参数userId", "值:" + userid)
}
def createmice(){
LogUtil log = androidStepHandler.log
RestTemplate restTemplate = SpringTool.getBean(RestTemplate.class)
String url = "https://gateway-mice.smartmice.cn/api/engine/event/Mice/Save"
String token = androidStepHandler.globalParams.getString("access_token")
String usrid = androidStepHandler.globalParams.getString("userId")
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Bearer " + token );
JSONObject param = new JSONObject();
Map<String, Object> requestMap = new HashMap<>();
requestMap.put("tuUserId", usrid);
Map<String, Object> itsExtData = new HashMap<>();
itsExtData.put("dtStartTime", "09:00:00");
itsExtData.put("dtEndTime", "12:00:00");
itsExtData.put("isNewYes", "1");
Map<String, Object> country = new HashMap<>();
country.put("val", "中国");
country.put("txt", "中国");
itsExtData.put("country", country);
Map<String, Object> province = new HashMap<>();
province.put("val", "上海市");
province.put("txt", "上海市");
itsExtData.put("province", province);
Map<String, Object> city = new HashMap<>();
city.put("val", 1);
city.put("txt", "上海市");
itsExtData.put("city", city);
Map<String, Object> gps = new HashMap<>();
gps.put("longitude", "121.472644");
gps.put("latitude", "31.231706");
itsExtData.put("gps", gps);
Map<String, Object> place = new HashMap<>();
itsExtData.put("place", place);
requestMap.put("itsExtData", itsExtData);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestMap, headers);
JSONObject result = restTemplate.exchange(url, HttpMethod.POST, requestEntity, JSONObject.class).getBody();
String iid = result.getString("content")
androidStepHandler.globalParams.put("ID",iid)
String ID = androidStepHandler.globalParams.getString("iid")
androidStepHandler.log.sendStepLog(1, "获取会议ID", "值:" + ID)
}
login("xxx","1")
createmice()