线下第二期_接口测试实战_0728

业务流程

  • 是否可以操纵数据库
  • 登陆
  • 清空所有关注的股票
  • 查询股票, facebook、google、拼多多
  • 添加关注
  • 取消关注
  • 分组
  • 刷新已关注
  • 刷新每个分类的股票
  • 查询股票
  • 买入
  • 卖出
  • 退出

用例编写

代码:https://github.com/seveniruby/RestAssuredDemo0728
Jenkins: http://jenkins.testing-studio.com:8081/job/RestAssuredDemo0728/

import io.restassured.RestAssured;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.useRelaxedHTTPSValidation;
import static org.hamcrest.Matchers.*;

@RunWith(Parameterized.class)
public class TestStock {

    @Parameterized.Parameters(name = "category={0} type={1} size={2}")
    public static List<Object[]> data(){
        return Arrays.asList(new Object[][]{
//                {2, 1, 10000},
//                {2, 1, 0},
//                {2, 1, 1},
//                {2, 1, 5},
//                {2, 1, 200},
                {1, 1, 2}
        });
    }

    @Parameterized.Parameter
    public Integer category;
    @Parameterized.Parameter(1)
    public Integer type;
    @Parameterized.Parameter(2)
    public Integer size;



    @Test
    public void demo(){
        useRelaxedHTTPSValidation();
        //RestAssured.proxy("127.0.0.1", 8080);
        given()
                .header("Host", "api.xueqiu.com")
                .header("User-Agent", "Xueqiu Android 8.7-rc-1618")
                .cookie("xq_a_token", "5debab7905abbcd9b2c5856e8b2c3e8ed890eb5b")
                .cookie("u", "7160412574")
                .queryParam("category", category)
                .queryParam("_t", "1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.7160412574.1532745299823.1532746995117")
                .queryParam("size", size)
                .queryParam("x", "0.179")
                .queryParam("_s", "f03f03")
                .queryParam("type", type)
        .when()
                .log().all()
                .get("https://101.201.62.20:443/v4/stock/portfolio/stocks.json")
        .then()
                .log().all()
                .statusCode(200).body("stocks.size()", lessThanOrEqualTo(size+1))
        ;
    }
}

quote接口


import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.useRelaxedHTTPSValidation;
import static org.hamcrest.Matchers.lessThanOrEqualTo;

@RunWith(Parameterized.class)
public class TestQuote {

    @Parameterized.Parameters(name = "category={0} type={1} size={2}")
    public static List<Object[]> data(){
        return Arrays.asList(new Object[][]{
//                {2, 1, 10000},
//                {2, 1, 0},
//                {2, 1, 1},
//                {2, 1, 5},
//                {2, 1, 200},
                {1, 1, 2}
        });
    }

    @Parameterized.Parameter
    public Integer category;
    @Parameterized.Parameter(1)
    public Integer type;
    @Parameterized.Parameter(2)
    public Integer size;



    @Test
    public void demo(){
        useRelaxedHTTPSValidation();
        given()
                .header("Host", "api.xueqiu.com")
                .header("User-Agent", "Xueqiu Android 8.7-rc-1618")
                .cookie("xq_a_token", "5debab7905abbcd9b2c5856e8b2c3e8ed890eb5b")
                .queryParam("fields", "name,type,symbol,flag,current,change,percentage,marketCapital,tick_size,release_date")
                .queryParam("code", "SH600969,SH600182,SH600487,CSI010,DWT,SZ002508,SH600547,SH601169,MRK,SZ000538,CSI004,SZ300012,SH600085,SZ161035,SZ000423,JNJ,SH600436,SZ002275,SZ000651,SH000001,03888,01928,02369,03933,01093")
                .queryParam("return_hasexist", "0")
                .queryParam("isdelay", "1")
                .cookie("u", "7160412574")
                .queryParam("_s", "13e0bc")
                .queryParam("_t", "1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.7160412574.1532745299823.1532746995324")
                .when().log().all().get("https://101.201.62.20:443/v4/stock/quote.json?_t=1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.7160412574.1532745299823.1532746995324&amp;_s=13e0bc&amp;fields=name,type,symbol,flag,current,change,percentage,marketCapital,tick_size,release_date&amp;return_hasexist=0&amp;isdelay=1&amp;code=SH600969,SH600182,SH600487,CSI010,DWT,SZ002508,SH600547,SH601169,MRK,SZ000538,CSI004,SZ300012,SH600085,SZ161035,SZ000423,JNJ,SH600436,SZ002275,SZ000651,SH000001,03888,01928,02369,03933,01093")
                .then().log().all()
                .statusCode(200)    
        ;
    }
}

添加关注和取消关注

import io.restassured.RestAssured;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.useRelaxedHTTPSValidation;
import static org.hamcrest.Matchers.*;

@RunWith(Parameterized.class)
public class TestAddStock {

    @Parameterized.Parameters(name = "category={0} name={1}")
    public static List<Object[]> data(){
        return Arrays.asList(new Object[][]{
                {2, "FB"},
                {2, "PDD"},
                {2, "GLG"}
        });
    }

    @Parameterized.Parameter
    public Integer category;
    @Parameterized.Parameter(1)
    public String name;



    @Test
    public void 添加关注(){

        RestAssured.proxy("127.0.0.1", 8080);
        useRelaxedHTTPSValidation();
        given()
                .header("Host", "api.xueqiu.com")
                .header("User-Agent", "Xueqiu Android 8.7-rc-1618")
                .formParam("x", "0.248")
                .cookie("u", "5708953570")
                .formParam("category", category)
                .cookie("xq_a_token", "8cf0a4031156698597e6621cf7ad44f3f01acee1")
                .queryParam("_t", "1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.7160412574.1532745299823.1532748254236")
                .formParam("symbol", name)
                .queryParam("_s", "ca0da7")
                .when().log().all().post("https://101.201.62.20:443/v4/stock/portfolio/addstock.json")
                .then().log().all()
                .statusCode(200)
        ;


        useRelaxedHTTPSValidation();
        given()
                .header("Host", "api.xueqiu.com")
                .header("User-Agent", "Xueqiu Android 8.7-rc-1618")
                .queryParam("_s", "ed1ae7")
                .cookie("xq_a_token", "8cf0a4031156698597e6621cf7ad44f3f01acee1")
                .queryParam("type", "6")
                .queryParam("_t", "1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.5708953570.1532745299823.1532761646814")
                .queryParam("size", "10000")
                .queryParam("category", "2")
                .cookie("u", "5708953570")
                .queryParam("x", "0.112")
                .when().log().all().get("https://101.201.62.20:443/v4/stock/portfolio/stocks.json")
                .then().log().all()
                .statusCode(200)
                .body("stocks.code", hasItems(name))
        ;


    }

    @Test
    public void 取消关注(){
        useRelaxedHTTPSValidation();
        given()
                .header("Host", "api.xueqiu.com")
                .header("User-Agent", "Xueqiu Android 8.7-rc-1618")
                .cookie("xq_a_token", "8cf0a4031156698597e6621cf7ad44f3f01acee1")
                .queryParam("symbol", name)
                .queryParam("_s", "bbc123")
                .queryParam("_t", "1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.5708953570.1532745299823.1532762170703")
                .queryParam("x", "0.1381")
                .cookie("u", "5708953570")
                .when().log().all().post("https://101.201.62.20:443/v4/stock/portfolio/delstock.json?_t=1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.5708953570.1532745299823.1532762170703&amp;_s=bbc123")
                .then().log().all()
                .statusCode(200)
        ;

        useRelaxedHTTPSValidation();
        given()
                .header("Host", "api.xueqiu.com")
                .header("User-Agent", "Xueqiu Android 8.7-rc-1618")
                .queryParam("_s", "ed1ae7")
                .cookie("xq_a_token", "8cf0a4031156698597e6621cf7ad44f3f01acee1")
                .queryParam("type", "6")
                .queryParam("_t", "1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.5708953570.1532745299823.1532761646814")
                .queryParam("size", "10000")
                .queryParam("category", category)
                .cookie("u", "5708953570")
                .queryParam("x", "0.112")
                .when().log().all().get("https://101.201.62.20:443/v4/stock/portfolio/stocks.json")
                .then().log().all()
                .statusCode(200)
                .body("stocks.code", not(hasItems(name)))
        ;
    }
}

spec复用
StockBase父类初始化

package com.testerhome.api.xueqiu.stock;

import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import org.junit.BeforeClass;

public class StockBase {
    public static RequestSpecBuilder requestSpecBuilder=new RequestSpecBuilder();
    public static RequestSpecification requestSpecification;
    @BeforeClass
    public static void beforeClass(){
        requestSpecBuilder.addHeader("Host", "api.xueqiu.com");
        requestSpecBuilder.addHeader("User-Agent", "Xueqiu Android 8.7-rc-1618");
        requestSpecBuilder.addCookie("u", "5708953570");
        requestSpecBuilder.addCookie("xq_a_token", "8cf0a4031156698597e6621cf7ad44f3f01acee1");
        requestSpecBuilder.addQueryParam("_t", "1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.7160412574.1532745299823.1532748254236");
        requestSpecBuilder.addQueryParam("_s", "ca0da7");
        requestSpecBuilder.addQueryParam("x", "0.248");
        System.out.println("BeforeClass");
        requestSpecification=requestSpecBuilder.build();
    }

}

TestAddStock

package com.testerhome.api.xueqiu.stock;

import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.requestSpecification;
import static io.restassured.RestAssured.useRelaxedHTTPSValidation;
import static org.hamcrest.Matchers.*;

@RunWith(Parameterized.class)
public class TestAddStock extends StockBase {

    @Parameterized.Parameters(name = "category={0} name={1}")
    public static List<Object[]> data(){
        return Arrays.asList(new Object[][]{
                {2, "FB"},
                {2, "PDD"},
                {2, "GLG"}
        });
    }

    @Parameterized.Parameter
    public Integer category;
    @Parameterized.Parameter(1)
    public String name;

    @Test
    public void 添加关注(){
        RestAssured.proxy("127.0.0.1", 8080);
        useRelaxedHTTPSValidation();
        given().spec(requestSpecification)
                .formParam("category", category)
                .formParam("symbol", name)
                .when().log().all().post("https://101.201.62.20:443/v4/stock/portfolio/addstock.json")
                .then().log().all()
                .statusCode(200)
        ;


        useRelaxedHTTPSValidation();
        given().spec(requestSpecification)
                .queryParam("type", "6")
                .queryParam("size", "10000")
                .queryParam("category", "2")
                .when().log().all().get("https://101.201.62.20:443/v4/stock/portfolio/stocks.json")
                .then().log().all()
                .statusCode(200)
                .body("stocks.code", hasItems(name))
        ;


    }

    @Test
    public void 取消关注(){
        useRelaxedHTTPSValidation();
        given().spec(requestSpecification)
                .queryParam("symbol", name)
                .when().log().all().post("https://101.201.62.20:443/v4/stock/portfolio/delstock.json?_t=1GENYMOTION66adefb1e7e6e9882dc08ac85ab792b3.5708953570.1532745299823.1532762170703&amp;_s=bbc123")
                .then().log().all()
                .statusCode(200)
        ;

        useRelaxedHTTPSValidation();
        given().spec(requestSpecification)
                .queryParam("type", "6")
                .queryParam("size", "10000")
                .queryParam("category", category)
                .when().log().all().get("https://101.201.62.20:443/v4/stock/portfolio/stocks.json")
                .then().log().all()
                .statusCode(200)
                .body("stocks.code", not(hasItems(name)))
        ;
    }
}





练习

参数化雪球的股票列表接口

把雪球的四个主功能的接口覆盖下,并更新到github,把github的地址贴到回复里