zoukankan      html  css  js  c++  java
  • Java接口自动化测试之HTTPClient学习(四)

    pom.xml  文件中dependency

     1 <dependencies>
     2         <dependency>
     3             <groupId>org.testng</groupId>
     4             <artifactId>testng</artifactId>
     5             <version>6.14.3</version>
     6         </dependency>
     7         <dependency>
     8             <groupId>com.relevantcodes</groupId>
     9             <artifactId>extentreports</artifactId>
    10             <version>2.41.1</version>
    11         </dependency>
    12         <dependency>
    13             <groupId>com.vimalselvam</groupId>
    14             <artifactId>testng-extentsreport</artifactId>
    15             <version>1.3.1</version>
    16         </dependency>
    17         <dependency>
    18             <groupId>com.aventstack</groupId>
    19             <artifactId>extentreports</artifactId>
    20             <version>3.0.6</version>
    21         </dependency>
    22         <dependency>
    23             <groupId>org.apache.httpcomponents</groupId>
    24             <artifactId>httpclient</artifactId>
    25             <version>4.1.2</version>
    26         </dependency>
    27         <dependency>
    28             <groupId>com.alibaba</groupId>
    29             <artifactId>fastjson</artifactId>
    30             <version>1.2.47</version>
    31             <scope>compile</scope>
    32         </dependency>
    33         <dependency>
    34             <groupId>org.projectlombok</groupId>
    35             <artifactId>lombok</artifactId>
    36             <version>1.16.14</version>
    37         </dependency>
    38     </dependencies>

    application.properties 文件, 配置一些常量, 例如:

     1 # 请求URL
     2 test.uri=http://localhost:8889
     3 test.post.path1=/postDemo
     4 test.post.path2=/postDemoWithCookie
     5 test.get.path1=/getDemo?
     6 test.get.path2=/getDemoWithCookie?
     7 
     8 # 请求头信息
     9 header.accept=*/*
    10 header.user.agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36
    11 header.content.type=application/json;charset=utf-8
    12 header.accept.charset=utf-8
    13 header.cookie=login=true

    公共类的提取, 例如HttpUtils.java , ReadConfig.java

     1 package com.testng.utils;
     2 
     3 import org.apache.http.client.methods.HttpGet;
     4 import org.apache.http.client.methods.HttpPost;
     5 import org.apache.http.client.methods.HttpRequestBase;
     6 import org.apache.http.entity.StringEntity;
     7 import org.apache.http.impl.client.DefaultHttpClient;
     8 import org.apache.http.util.EntityUtils;
     9 
    10 import java.io.IOException;
    11 
    12 public class HttpUtils {
    13 
    14     private static DefaultHttpClient defaultHttpClient = null;
    15 
    16     public static String doGet(String url) throws IOException {
    17         String result;
    18         defaultHttpClient = new DefaultHttpClient();
    19         HttpGet get = new HttpGet(url);
    20         setHeader(get);
    21         result = EntityUtils.toString(defaultHttpClient.execute(get).getEntity(), "utf-8");
    22         return result;
    23     }
    24 
    25     public static String doPost(String url, String data) throws IOException {
    26         String result;
    27         HttpPost post = new HttpPost(url);
    28         setHeader(post);
    29         post.setEntity(new StringEntity(data, "utf-8"));
    30         defaultHttpClient = new DefaultHttpClient();
    31         result = EntityUtils.toString(defaultHttpClient.execute(post).getEntity(), "utf-8");
    32         return result;
    33     }
    34 
    35     private static void setHeader(HttpRequestBase httpRequestBase) {
    36         httpRequestBase.setHeader("Accept", ReadConfig.ACCEPT);
    37         httpRequestBase.setHeader("User-Agent", ReadConfig.USER_AGENT);
    38         httpRequestBase.setHeader("Content-Type", ReadConfig.CONTENT_TYPE);
    39         httpRequestBase.setHeader("Accept-Charset", ReadConfig.ACCEPT_CHARSET);
    40         httpRequestBase.setHeader("Cookie", ReadConfig.COOKIE);
    41     }
    42 
    43 }
     1 package com.testng.utils;
     2 
     3 import lombok.Data;
     4 
     5 import java.util.Locale;
     6 import java.util.ResourceBundle;
     7 
     8 @Data
     9 public class ReadConfig {
    10 
    11     private static ResourceBundle bundle = ResourceBundle.getBundle("application", Locale.CHINA);
    12 
    13     public static String ACCEPT = bundle.getString("header.accept");
    14     public static String USER_AGENT = bundle.getString("header.user.agent");
    15     public static String CONTENT_TYPE = bundle.getString("header.content.type");
    16     public static String ACCEPT_CHARSET = bundle.getString("header.accept.charset");
    17     public static String COOKIE = bundle.getString("header.cookie");
    18     public static String URI = bundle.getString("test.uri");
    19     public static String POST_PATH1 = bundle.getString("test.post.path1");
    20     public static String POST_PATH2 = bundle.getString("test.post.path2");
    21     public static String GET_PATH1 = bundle.getString("test.get.path1");
    22     public static String GET_PATH2 = bundle.getString("test.get.path2");
    23 
    24 
    25 }

    TestNG测试类

     1 package com.testng.cases;
     2 
     3 import com.alibaba.fastjson.JSONObject;
     4 import com.testng.utils.HttpUtils;
     5 import com.testng.utils.ReadConfig;
     6 
     7 import org.testng.Assert;
     8 import org.testng.Reporter;
     9 import org.testng.annotations.Test;
    10 
    11 import java.io.IOException;
    12 
    13 public class DoPostTest {
    14 
    15     @Test
    16     public void postTest() throws IOException {
    17         String url = ReadConfig.URI + ReadConfig.POST_PATH1;
    18         Reporter.log("请求地址" + url);
    19         JSONObject param = new JSONObject();
    20         param.put("name","zhangsan");
    21         param.put("password","123456");
    22         Reporter.log("请求体" + param.toString());
    23         String result = HttpUtils.doPost(url, param.toString());
    24         Reporter.log("请求结果" + result);
    25         Assert.assertEquals(result, "{"msg":"success","status":1011,"token":"ZZZZZZZZZZZZZZZZZZZZZZ"}");
    26     }
    27 
    28     @Test
    29     public void postTest2() throws IOException {
    30         String url = ReadConfig.URI + ReadConfig.POST_PATH2;
    31         Reporter.log("请求地址" + url);
    32         JSONObject param = new JSONObject();
    33         param.put("name","zhangsan");
    34         param.put("password","1234567");
    35         Reporter.log("请求体" + param.toString());
    36         String result = HttpUtils.doPost(url, param.toString());
    37         Reporter.log("请求结果" + result);
    38         Assert.assertEquals(result, "{"msg":"success","status":1011,"token":"YYYYYYYYYYYYYYYYYYYYYYYY"}");
    39     }
    40 
    41 }
     1 package com.testng.cases;
     2 
     3 import com.testng.utils.HttpUtils;
     4 import com.testng.utils.ReadConfig;
     5 import org.testng.Assert;
     6 import org.testng.Reporter;
     7 import org.testng.annotations.Test;
     8 
     9 import java.io.IOException;
    10 
    11 public class DoGetTest {
    12 
    13     @Test(description = "成功的案例")
    14     public void getTest() throws IOException {
    15         String uri = ReadConfig.URI + ReadConfig.GET_PATH1;
    16         String param = "name=zhangsan&password=123456";
    17         String url = uri + param;
    18         Reporter.log("请求地址" + url);
    19         String result = HttpUtils.doGet(url);
    20         Reporter.log("请求结果" + result);
    21         Assert.assertEquals(result, "{"msg":"success","status":1011,"token":"XXXXXXXXXXXXXXXX"}");
    22     }
    23 
    24     @Test(description = "失败的案例")
    25     public void getTest2() throws IOException {
    26         String uri = ReadConfig.URI + ReadConfig.GET_PATH2;
    27         String param = "name=zhangsan&password=1234567";
    28         String url = uri + param;
    29         Reporter.log("请求地址" + url);
    30         String result = HttpUtils.doGet(url);
    31         Reporter.log("请求结果" + result);
    32         Assert.assertEquals(result, "{"msg":"success","status":1011,"token":"AAAAAAAAAAAAAAAAAAAAAAAA"}");
    33     }
    34 }

    testng.xml

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <suite name="your suite name">
     3     <test name="your test name1">
     4         <classes>
     5             <class name="com.testng.cases.DoGetTest"/>
     6             <class name="com.testng.cases.DoPostTest"/>
     7         </classes>
     8     </test>
     9 
    10     <listeners>
    11         <listener class-name="com.testng.config.ExtentTestNGIReporterListener"/>
    12     </listeners>
    13 
    14 </suite>

    运行testng.xml 后得到测试报告

  • 相关阅读:
    OSG节点访问和遍历
    osg ifc数据渲染着色器
    osg qt kdchart 开发施工过程模拟软件
    KDChart example
    Qt kdChart 甘特图案例
    Qt KDChart编译
    osg 3ds模型加载与操作
    osg 三维模型加载与解析(fbx、3ds、ive、obj、osg)
    osg fbx 模型结构操作
    osg fbx 模型树结构
  • 原文地址:https://www.cnblogs.com/andrew209/p/9163797.html
Copyright © 2011-2022 走看看