Java 接口测试 --Rest assured框架用法

    科技2022-08-17  111

    依赖地址

    <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>json-path</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>json-schema-validator</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.1.0</version> <scope>compile</scope> </dependency>

    一、get请求用法

    import static io.restassured.RestAssured.get; import static io.restassured.RestAssured.given; public static void main(String[] args) { public void gethome() { Response response = get("/WoniuBoss4.0/"); } }

    二、post请求用法

    方式一:(参数也可以一起传 注意使用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"); }

    header,cookie设置

    public void queruRole() { Response response = given() .cookies(cookie) //设置cookie .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36") .contentType("application/x-www-form-urlencoded") .body("pageSize=10&pageIndex=1&userName=&empName=") .post("/WoniuBoss4.0/user/queryUser"); System.out.println(response.jsonPath().getString("totalPage"));//解析json }

    全代码

    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

    解析json

    response.jsonPath().get("totalPage") response.jsonPath().getString("totalPage") //获得json报文中的值

    利用fiddler抓取发送出去的请求

    RestAssured.proxy("127.0.0.1",8888); //这是设置全局的用fiddler抓包,8888是fiddler设置好的端口 given().proxy("127.0.0.1",8888); //单个接口
    Processed: 0.008, SQL: 9