Java接口自动化测试框架设计-4-POST请求方法封装过程和测试

atu168 / 2024-01-22 / 原文

       这个接口自动化测试框架到目前为止,我们已经完成了Get请求的封装和必要的工具类的支持。接下来这篇,我来介绍如何完成POST请求的封装过程。一般来说,在一个项目中,接口测试很多时候就是测试Get和POST方法,其他的请求方式的接口很少,占的比重几乎不计。所以,这个Java接口自动化测试框架的核心就是Get和POST请求方法的封装过程。

 

1.POST接口举例

浏览器打开https://reqres.in/,下拉一屏。点击第一个POST请求,这个接口的介绍信息如下。

      这个接口的作用是创建用户,参数是一个json类型的数据,一个name一个job,两个JSON对象。发送请求之后,返回的JSON数据有name和job和id,以及创建时间这几个数据。

 

2.Postman手动实现

 

我们先在本地postman环境,先来手动测试实现下这个post接口的请求过程。

这个post接口请求还是比较简单,很容易在postman上实现该请求。

 

3.Java代码自动化实现

 

       我们已经可以正确地在postman上实现创建用户这个接口的手动测试,那么我们想要这个过程自动化实现,如何做呢。下面我在RestClient.java封装了两个方法,一个是带请求头信息的Get请求,一个是带请求头信息的POST请求方法。这篇,了解了POST请求方法,带请求头的Get方法封装就很好理解。

  1.  
    package com.qa.restclient;
  2.  
     
  3.  
    import java.io.IOException;
  4.  
    import java.util.HashMap;
  5.  
    import java.util.Map;
  6.  
     
  7.  
    import org.apache.http.client.ClientProtocolException;
  8.  
    import org.apache.http.client.methods.CloseableHttpResponse;
  9.  
    import org.apache.http.client.methods.HttpGet;
  10.  
    import org.apache.http.client.methods.HttpPost;
  11.  
    import org.apache.http.entity.StringEntity;
  12.  
    import org.apache.http.impl.client.CloseableHttpClient;
  13.  
    import org.apache.http.impl.client.HttpClients;
  14.  
     
  15.  
    public class RestClient {
  16.  
        
  17.  
        
  18.  
        //1. Get 请求方法
  19.  
        public CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
  20.  
            
  21.  
            //创建一个可关闭的HttpClient对象
  22.  
            CloseableHttpClient httpclient = HttpClients.createDefault();
  23.  
            //创建一个HttpGet的请求对象
  24.  
            HttpGet httpget = new HttpGet(url);
  25.  
            //执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
  26.  
            CloseableHttpResponse httpResponse = httpclient.execute(httpget);
  27.  
            
  28.  
            return httpResponse;
  29.  
        }
  30.  
        
  31.  
        //2. Get 请求方法(带请求头信息)
  32.  
        public CloseableHttpResponse get(String url,HashMap<String,String> headermap) throws ClientProtocolException, IOException {
  33.  
                
  34.  
            //创建一个可关闭的HttpClient对象
  35.  
            CloseableHttpClient httpclient = HttpClients.createDefault();
  36.  
            //创建一个HttpGet的请求对象
  37.  
            HttpGet httpget = new HttpGet(url);
  38.  
            //加载请求头到httpget对象
  39.  
            for(Map.Entry<String, String> entry : headermap.entrySet()) {
  40.  
                httpget.addHeader(entry.getKey(), entry.getValue());
  41.  
            }
  42.  
            //执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
  43.  
            CloseableHttpResponse httpResponse = httpclient.execute(httpget);
  44.  
                
  45.  
            return httpResponse;
  46.  
        }
  47.  
        
  48.  
        //3. POST方法
  49.  
        public CloseableHttpResponse post(String url, String entityString, HashMap<String,String> headermap) throws ClientProtocolException, IOException {
  50.  
            //创建一个可关闭的HttpClient对象
  51.  
            CloseableHttpClient httpclient = HttpClients.createDefault();
  52.  
            //创建一个HttpPost的请求对象
  53.  
            HttpPost httppost = new HttpPost(url);
  54.  
            //设置payload
  55.  
            httppost.setEntity(new StringEntity(entityString));
  56.  
            
  57.  
            //加载请求头到httppost对象
  58.  
            for(Map.Entry<String, String> entry : headermap.entrySet()) {
  59.  
                httppost.addHeader(entry.getKey(), entry.getValue());
  60.  
            }
  61.  
            //发送post请求
  62.  
            CloseableHttpResponse httpResponse = httpclient.execute(httppost);
  63.  
            return httpResponse;
  64.  
        }
  65.  
        
  66.  
        
  67.  
    }
  68.  
     
  69.  
     

 

      然后,我们需要写一个TestNG测试用例来测试下这个封装的post方法好不好用。由于我们去前面几篇文章介绍了TestNG测试get方法的代码,这里我们就直接拷贝和修改部分代码就行。

      在写测试用例之前,我们需要提前准备好json数据,一般来说,在Java中JSON数据都是放在JAVA Bean类中,通过JSON把高级对象序列化成JSON对象。

      在src/main/java中新建包:com.qa.data,然后新建一个Users.java,这个命名就参考接口的url单词就行。在postman或者网站该post方法,我们知道,需要name和job这两个json对象。我们新建一个bean类,同alt+shift+s,然后选择生成构造方法和set和get方法。

  1.  
    package com.qa.data;
  2.  
     
  3.  
    public class Users {
  4.  
     
  5.  
    private String name;
  6.  
    private String job;
  7.  
     
  8.  
    public Users() {
  9.  
    super();
  10.  
    }
  11.  
     
  12.  
    public Users(String name, String job) {
  13.  
    super();
  14.  
    this.name = name;
  15.  
    this.job = job;
  16.  
    }
  17.  
     
  18.  
    public String getName() {
  19.  
    return name;
  20.  
    }
  21.  
     
  22.  
    public void setName(String name) {
  23.  
    this.name = name;
  24.  
    }
  25.  
     
  26.  
    public String getJob() {
  27.  
    return job;
  28.  
    }
  29.  
     
  30.  
    public void setJob(String job) {
  31.  
    this.job = job;
  32.  
    }
  33.  
     
  34.  
    }

     好了,在src/test/java下的com.qa.tests我们新建一个POST测试用例,现在我们的TestNG测试类代码如下:

  1.  
    package com.qa.tests;
  2.  
     
  3.  
    import java.io.IOException;
  4.  
    import java.util.HashMap;
  5.  
     
  6.  
    import org.apache.http.client.ClientProtocolException;
  7.  
    import org.apache.http.client.methods.CloseableHttpResponse;
  8.  
    import org.apache.http.util.EntityUtils;
  9.  
    import org.testng.Assert;
  10.  
    import org.testng.annotations.BeforeClass;
  11.  
    import org.testng.annotations.Test;
  12.  
     
  13.  
    import com.alibaba.fastjson.JSON;
  14.  
    import com.alibaba.fastjson.JSONObject;
  15.  
    import com.qa.base.TestBase;
  16.  
    import com.qa.data.Users;
  17.  
    import com.qa.restclient.RestClient;
  18.  
    import com.qa.util.TestUtil;
  19.  
     
  20.  
    public class PostApiTest extends TestBase {
  21.  
    TestBase testBase;
  22.  
    String host;
  23.  
    String url;
  24.  
    RestClient restClient;
  25.  
    CloseableHttpResponse closeableHttpResponse;
  26.  
     
  27.  
     
  28.  
    @BeforeClass
  29.  
    public void setUp() {
  30.  
    testBase = new TestBase();
  31.  
    host = prop.getProperty("HOST");
  32.  
    url = host + "/api/users";
  33.  
     
  34.  
    }
  35.  
     
  36.  
    @Test
  37.  
    public void postApiTest() throws ClientProtocolException, IOException {
  38.  
    restClient = new RestClient();
  39.  
    //准备请求头信息
  40.  
    HashMap<String,String> headermap = new HashMap<String,String>();
  41.  
    headermap.put("Content-Type", "application/json"); //这个在postman中可以查询到
  42.  
     
  43.  
    //对象转换成Json字符串
  44.  
    Users user = new Users("Anthony","tester");
  45.  
    String userJsonString = JSON.toJSONString(user);
  46.  
    //System.out.println(userJsonString);
  47.  
     
  48.  
    closeableHttpResponse = restClient.post(url, userJsonString, headermap);
  49.  
     
  50.  
    //验证状态码是不是200
  51.  
    int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
  52.  
    Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_201,"status code is not 201");
  53.  
     
  54.  
    //断言响应json内容中name和job是不是期待结果
  55.  
    String responseString = EntityUtils.toString(closeableHttpResponse.getEntity());
  56.  
    JSONObject responseJson = JSON.parseObject(responseString);
  57.  
    //System.out.println(responseString);
  58.  
    String name = TestUtil.getValueByJPath(responseJson, "name");
  59.  
    String job = TestUtil.getValueByJPath(responseJson, "job");
  60.  
    Assert.assertEquals(name, "Anthony","name is not same");
  61.  
    Assert.assertEquals(job, "tester","job is not same");
  62.  
     
  63.  
    }
  64.  
     
  65.  
    }

      建议,在写测试用例过程中,需要和postman上的请求结果参考,特别是Headers这里的键值对。这里留一个作业,上面我写了一个Get带请求头的封装方法,你可以对照postman中的请求头去写一个单元测试用例去测试下这个带headers的Get方法。

       目前,Java接口自动化测试框架的核心部分,http请求方法的封装已经完成。接下里还有测试日志,测试报告,或者其他需要抽取优化的模块去完成。