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


     这套框架的报告是自己封装的

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

    以常见的登录接口为例

    新建一个package,和postParameters类

     1 package com.qa.Parameters;
     2  
     3 public class postParameters {
     4     private String userName;
     5     private String password;
     6  
     7     public postParameters(){
     8  
     9     }
    10     //login
    11     public postParameters(String userName , String password){
    12         this.userName = userName;
    13         this.password = password;
    14     }
    15  
    16     public String getUserName() {
    17         return userName;
    18     }
    19  
    20     public void setUserName(String userName){
    21         this.userName = userName;
    22     }
    23  
    24     public String getPassword() {
    25         return password;
    26     }
    27  
    28     public void setPassword(String password){
    29         this.password = password;
    30     }
    31 }

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

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

     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<String ,String> postHeader = new HashMap<String, String>();
        @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("测试结束");
        }
     
    }

     原文地址https://blog.csdn.net/qq_34693151/article/details/81874656

  • 相关阅读:
    3、取邮件
    9、将标准字符流写到文件中
    8、使用转换流处理标准输入
    6、数据流
    关闭子窗体时刷新父页面
    sql索引规则
    DataTable DataRow[] 排序 多列
    Oracle 自定义函数创建同义词、存储过程或表创建同义词
    C# DataTable 排序
    sql 分组后每组查询10个
  • 原文地址:https://www.cnblogs.com/111testing/p/10624727.html
Copyright © 2020-2023  润新知