接口自动化框架(java)--2.接口用例POST请求,参数配置

Post类型的接口通常有请求参数,请求参数也是json类型,所以需要写一个类将请求参数序列化成json对象

以常见的登录接口为例

新建一个package,和postParameters类

接口自动化框架(java)--2.接口用例POST请求,参数配置_第1张图片

 

package com.qa.Parameters;

public class postParameters {
    private String userName;
    private String password;

    public postParameters(){

    }
    //login
    public postParameters(String userName , String password){
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName){
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password){
        this.password = password;
    }
}

2.在config.properties中配置根url,以及excel地址

#TestCase1
Host = https://xxxxxx.cn
testCase1data = F:\\gitcode\\API_AutoFramework\\src\\main\\java\\com\\qa\\data\\testCase1data.xlsx

3.excel参数

接口自动化框架(java)--2.接口用例POST请求,参数配置_第2张图片

 

4.在tests目录下建一个测试类继承TestBase,再引入testng,编写测试用例

package com.qa.tests;

import com.alibaba.fastjson.JSON;
import com.qa.base.TestBase;
import com.qa.Parameters.postParameters;
import com.qa.restclient.RestClient;
import com.qa.util.TestUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;

import static com.qa.util.TestUtil.dtt;

public class testCase1 extends TestBase {
    TestBase testBase;
    RestClient restClient;
    CloseableHttpResponse closeableHttpResponse;
    //host根url
    String host;
    //Excel路径
    String testCaseExcel;
    //header
    HashMap postHeader = new HashMap();
    @BeforeClass
    public void setUp(){
        testBase = new TestBase();
        restClient = new RestClient();
        postHeader.put("Content-Type","application/json");
        //载入配置文件,接口endpoint
        host = prop.getProperty("Host");
        //载入配置文件,post接口参数
        testCaseExcel=prop.getProperty("testCase1data");

    }

    @DataProvider(name = "postData")
    public Object[][] post() throws IOException {
        return dtt(testCaseExcel,0);

    }

    @Test(dataProvider = "postData")
    public void login(String loginUrl,String username, String passWord) throws Exception {
        //使用构造函数将传入的用户名密码初始化成登录请求参数
        postParameters loginParameters = new postParameters(username,passWord);
        //将登录请求对象序列化成json对象
        String userJsonString = JSON.toJSONString(loginParameters);
        //发送登录请求
        closeableHttpResponse = restClient.postApi(host+loginUrl,userJsonString,postHeader);
        //从返回结果中获取状态码
        int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
        Assert.assertEquals(statusCode,200);

    }
    @BeforeClass
    public void endTest(){
        System.out.print("测试结束");
    }

}

5.调试testcase,测试通过

接口自动化框架(java)--2.接口用例POST请求,参数配置_第3张图片

你可能感兴趣的:(接口自动化框架(java))