8-5接口测试用例设计与编写-4

1.查看testerhome精华帖的数量

package com.csj2018;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.restassured.internal.ValidatableResponseImpl;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import org.testng.annotations.Test;
import io.restassured.RestAssured.*;
import io.restassured.matcher.RestAssuredMatchers.*;
import org.hamcrest.Matchers.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.restassured.module.jsv.JsonSchemaValidator.*;
import static io.restassured.RestAssured.given;
import static io.restassured.path.xml.XmlPath.from;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

/**
 * 公司接口用例
 */
public class StartRestAssured {
    /**
     * 登陆接口
     */
    @Test
    public void Login(){
        String param = "{" +
                "\"httpCode\":\"xxxxxxxxx\","+
                "\"service\": \"Service\","+
                "\"method\":\"Login\","+
                "\"type\":\"mt\","+
                "\"args\":{" +
                "\"patientCard\":\"123456789012345678\"," +
                "\"password\":\"12345678\"," +
                "\"type\":\"3\"" +
                "}," +
                "\"version\":\"7.2\"," +
                "\"token\":false" +
                "}";

        Response res = given().param("key",param).get(BaseTest.url);
        assertThat(res.statusCode(),equalTo(200));
        assertThat(res.getBody().jsonPath().getString("message"),equalTo("操作成功"));
        System.out.println(res.getBody().jsonPath().getString(""));
    }

    /**
     * 获取地区列表
     */
    @Test
    public void getArea(){
        String key = "{" +
                "\"args\":{}," +
                "\"method\":\"QueryAreaList\"," +
                "\"service\":\"areaService\"," +
                "\"httpCode\":\"12345678\"," +
                "\"type\":\"mt\"," +
                "\"version\":\"7.2\"," +
                "\"token\":false" +
                "}";
        /*
        两个坑:
        1.不要传入参数apk
        String apk = "yygh"; //注意:接口测试不需要传入apk,否则提示http姐妹失败
        2.公司接口返回的是字符串,不是json,导致使用body断言出错
        given().param("key",key).get(BaseTest.url).then().body("message",equalTo("操作成功"));
        解决方法:1.使用正则表达式匹配 2.判断包含某个字符串 3.通过Response对象,再解析Json路径
         */

        given().param("key",key).get(BaseTest.url).then().body(containsString("操作成功"));//坑2:返回的是字符串,不是json,需要转换

    }
    @Test
    public void testBaidu(){
        String url = "https://api.bilibili.com/x/web-interface/nav";
        given().param("from","report").get(url).then().body("code",equalTo(-101));
    }
    @Test
    public void testBili(){
        String url = "https://s.search.bilibili.com/main/hotword";
        given().param("mid","").param("buvid","5D258CCD-5139-41FF-A713-65B8B24A4EB7155807infoc").get(url).print();
    }
    @Test
    public void testExcellent(){
        String url = "https://testerhome.com/api/v3/topics.json";
        given().when().get(url).then().body("topics.findAll { it.id == 20735}.title",hasItem("面试遇到个问题,请教各位"));

    }
}

参考https://testerhome.com/topics/7060#json-using-jsonpath

posted on 2019-10-13 11:37  singleSpace  阅读(181)  评论(0编辑  收藏  举报