方式一:(参数也可以一起传 注意使用params)
public void login() { Response response = given() .params("userName","WNCD000","userPass","woniu123","checkcode","0000","remember","Y") .post("/WoniuBoss4.0/login/userLogin"); }方式二:(参数也可以分开传)
public void login() { Response response = given() .param("userName", "WNCD000") //登录需要的参数 .param("userPass", "woniu123") .param("checkcode", "0000") .param("remember", "Y") .post("/WoniuBoss4.0/login/userLogin");方式三:(用body字符串传参,推荐。类型是application/json 则只能用body()管理参数)
Response response = given() .contentType("application/x-www-form-urlencoded") .body("userName=WNCD000&userPass=woniu123&checkcode=0000&remember=Y") .post("/WoniuBoss4.0/login/userLogin");请求参数为json的发送方式
public void baidu(){ HashMap<String,Object> data=new HashMap<String, Object>(); data.put("id",6040); //请求参数为json的发送方式 data.put("title","通过代理安装 appium"); data.put("name","思寒"); //需要根节点可以再建立个MAP把data套进去 Response response=given().contentType(ContentType.JSON) .body(data) .post("http://www.baidu.com"); }全代码
import io.restassured.RestAssured; import io.restassured.config.SSLConfig; import io.restassured.response.Response; import java.util.Map; import static io.restassured.RestAssured.get; import static io.restassured.RestAssured.given; public class LoginTest { static String baseURI="http://192.168.1.106:8080"; static { RestAssured.proxy("127.0.0.1",8888); //用fiddler抓包 } Map<String, String> cookie; //接受cookie public static void main(String[] args) { LoginTest login = new LoginTest(); login.login(); login.queruRole(); } public void gethome() { Response response = get("/WoniuBoss4.0/"); } public void login() { Response response = given() .contentType("application/x-www-form-urlencoded") // 配置SSL 让所有请求支持所有的主机名 .config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation()))) .body("userName=WNCD000&userPass=woniu123&checkcode=0000&remember=Y") .post("/WoniuBoss4.0/login/userLogin"); cookie = response.getCookies(); //获取cookie } public void queruRole() { Response response = given() .cookies(cookie) //设置cookie .contentType("application/x-www-form-urlencoded") .body("pageSize=10&pageIndex=1&userName=&empName=") .post("/WoniuBoss4.0/user/queryUser"); response.getBody().print(); //获得body注意不需要打印了 System.out.println(response.jsonPath().getString("totalPage"));//解析json } }1.获得响应体,是内存地址
response.getBody()2.打印响应body,可以用字符串类型对象接收,第二个不打印
response.print(); response.getBody().asString()3.获取状态码
response.getStatusCode()4.获取具体的某一段cookie
response.getCookie("JSESSIONID")5.获取所有的cookies 得到一个集合
response.getCookies(); //获取cookie