标签:对象 end tms lease 工具类 bec add equal etag
public class WXopenDemo {
/**
* 异常测试:错误的参数
*/
@Test(groups = "testtoken")
public void getToken1(){
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
String corpid = "123456";
String corpsecret = "123456";
given().param("corpid",corpid).param("corpsecret",corpsecret).get(url).then().body("errcode",equalTo(40013));//prettyPeek连带消息头一起打印
}
/**
* 获取token
*/
@Test(groups = "testtoken")
public void getToken2(){
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
String corpid = "XXXXXXXXXXXX";
String corpsecret = "XXXXXXXXXXXXX";
given().param("corpid",corpid).param("corpsecret",corpsecret).get(url).then().statusCode(equalTo(200));//prettyPeek连带消息头一起打印
}
}
使用xml组织用例,并运行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" verbose="1">
<test verbose="2" preserve-order="true" name="lesson1"><!--测试名称-->
<groups>
<run>
<include name="testtoken" /><!--只跑add组-->
</run>
</groups>
<classes>
<class name="com.csj2018.WXopenDemo" />
</classes>
</test>
</suite>
/**
* 发消息
*/
@Test
public void sendMessage(){
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
//先获取token
String corpid = "XXXXXXXXXXXX";
String corpsecret = "XXXXXXXXX";
Response res = given().param("corpid",corpid).param("corpsecret",corpsecret).get(url).prettyPeek();
assertThat(res.statusCode(),equalTo(200));
String accessToken = res.getBody().jsonPath().getString("access_token");
assertThat(accessToken,notNullValue());
//发送消息
String messageUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+accessToken;
String reqContent = "{\n" +
" \"toparty\" : \"4\",\n" +
" \"msgtype\" : \"text\",\n" +
" \"agentid\" : 1000004,\n" +
" \"text\" : {\n" +
" \"content\" : \"你的快递已到,请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>,聪明避开排队。\"\n" +
" },\n" +
" \"safe\":0,\n" +
" \"enable_id_trans\": 0\n" +
"}";
given().contentType(ContentType.JSON).body(reqContent).post(messageUrl).prettyPeek().then().body("errmsg",equalTo("ok"));
}
package com.csj2018.utils;
import io.restassured.response.Response;
import java.io.File;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
public class WXUtils {
private static String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
private static String corpid = "XXXXXXXXXX";
private static String corpsecret = "XXXXXXXXXXXXXXXXX";
private static String uploadImageUrl= "https://qyapi.weixin.qq.com/cgi-bin/media/upload";
public static String getToken(){
Response res = given().param("corpid",corpid).param("corpsecret",corpsecret).get(url).prettyPeek();
assertThat(res.statusCode(),equalTo(200));
String accessToken = res.getBody().jsonPath().getString("access_token");
assertThat(accessToken,notNullValue());
return accessToken;
}
}
请求参数类
package com.csj2018.model;
import java.util.Map;
public class WXTextMessageModel {
/**
* {
* "touser" : "UserID1|UserID2|UserID3",
* "toparty" : "PartyID1|PartyID2",
* "totag" : "TagID1 | TagID2",
* "msgtype" : "text",
* "agentid" : 1,
* "text" : {
* "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
* },
* "safe":0,
* "enable_id_trans": 0
* }
*/
private String touser;
private String toparty;
private String totag;
private String msgtype;
private Integer agentid;
private Map<String, String> text;
private Map<String, String> image;
private Integer safe;
private Integer enable_id_trans;
//省略getter和setter方法 mac快捷键ctrl + enter
/**
* 发送文本
*/
@Test
public void sendMessage2(){
String accessToken = WXUtils.getToken();
//发送消息
String messageUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+accessToken;
WXTextMessageModel wxTextMessageModel = new WXTextMessageModel();
wxTextMessageModel.setToparty("4");
wxTextMessageModel.setMsgtype("text");
wxTextMessageModel.setAgentid(1234567);
Map<String, String> map = new HashMap<>();
map.put("content","你的快递已到,请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>,聪明避开排队。");
wxTextMessageModel.setText(map);
wxTextMessageModel.setSafe(0);
wxTextMessageModel.setEnable_id_trans(0);
given().contentType(ContentType.JSON).body(wxTextMessageModel).post(messageUrl).prettyPeek().then().body("errmsg",equalTo("ok"));
//如果报错cannot serialize object because no JSON serializer fount in classpath.Please put either Jackson(Databind) or Gson in the classpath.可能是json-schema-validator缺失,倒入即可
}
public static String getWXMediaId(){
String accessToken = getToken();
System.out.println(accessToken);
Response res = given().contentType("multipart/form-data")
.queryParam("access_token",accessToken)
.queryParam("type","image")
.multiPart("meida",new File("src/test/image/singleDog.jpg")).when().post(uploadImageUrl).prettyPeek();
String mediaId = res.getBody().jsonPath().getString("media_id");
System.out.println(32+mediaId);
assertThat(mediaId,notNullValue());
return mediaId;
}
/**
* 发送图片
*/
@Test
public void sendImage(){
//发送消息
String messageUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+WXUtils.getToken();
WXTextMessageModel wxTextMessageModel = new WXTextMessageModel();
wxTextMessageModel.setToparty("4");
wxTextMessageModel.setMsgtype("image");
wxTextMessageModel.setAgentid(1000004);
Map<String, String> imageMap = new HashMap<>();
imageMap.put("media_id",WXUtils.getWXMediaId());
wxTextMessageModel.setImage(imageMap);
wxTextMessageModel.setSafe(0);
wxTextMessageModel.setEnable_id_trans(0);
given().contentType(ContentType.JSON).body(wxTextMessageModel).post(messageUrl).prettyPeek().then().body("errmsg",equalTo("ok"));
}
print只打印响应体;prettyPeek会讲响应头和响应体一起打印
param用于get请求中;
queryParam和formParam用于post请求中,queryParam用于拼接URL,formParam用于提交表单
使用body传递参数,可以指定编码,如contentType("application/json; charset=UTF-16")
Message message = new Message();
message.setMessage("My messagee");
given().
contentType("application/json").
body(message).
when().
post("/message");
使用formParam传递参数
Message message = new Message();
message.setMessage("My messagee");
given().
contentType("application/json; charset=UTF-16").
formParam("param1", message).
when().
post("/message");
更多查看testerhome rest-assured使用指南和github
标签:对象 end tms lease 工具类 bec add equal etag
原文地址:https://www.cnblogs.com/csj2018/p/11664843.html