一、maven中引入httpclient、testng、poi依赖包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.lemon</groupId>
<artifactId>interfaceDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>interfaceDemo</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
</dependencies>
</project>

二、准备测试数据

三、poi读取Excel文件

package com.lemon;

import java.io.File;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelUtil { public static Object[][] read(int startRow,int endRow,int startCell,int endCell){ Object [][] datas = new Object [endRow-startRow+1][endCell-startCell+1];
try {
//获取WorkBook对象
Workbook workbook = WorkbookFactory.create(new File("src/test/java/test.xlsx"));
//获取sheet,0表示第一个
Sheet sheet = workbook.getSheetAt(0);
for(int i = startRow; i <= endRow;i++){
//取出每一行
Row row = sheet.getRow(i-1);
for (int j = startRow; j <= endCell;j++){
//取出每一列,先指定不会返回空对象,防止单元格为空时,报空指针异常
Cell cell = row.getCell(j-1,MissingCellPolicy.CREATE_NULL_AS_BLANK);
//把每列当字符串处理,并取出字符串的值
cell.setCellType(CellType.STRING);
String value = cell.getStringCellValue();
datas[i-startRow][j-startCell] = value;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return datas;
}
//测试
public static void main(String[] args) throws Exception {
Object[][] datas = read(2, 7, 2, 5);
for(Object[] objects:datas){
for(Object object:objects){
System.out.print("【"+object+"】");
}
System.out.println();
}
}
}

四、编写接口自动化脚本

package com.lemon;

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; public class Demo { @Test(dataProvider="datas")
public static void test(String url,String mobileCode,String userID,String type,String response) throws Exception { System.out.println("url:"+url+",mobileCode:"+mobileCode+",userID:"+userID+",type:"+type);
if("post".equalsIgnoreCase(type)){
String resp = doPost(url,mobileCode,userID);
Assert.assertEquals(resp, response);
}else {
String resp = doGet(url,mobileCode,userID);
Assert.assertEquals(resp, response);
}
} @DataProvider
public static Object [][] datas(){ /* Object [][] datas = {
{"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo","15578581","","post"},
{"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo","18381485","","get"},
{"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo","15084258","","post"}
}; */ Object[][] datas = ExcelUtil.read(2, 5, 2, 6);
return datas;
} /*
* 实现get类型接口的调用
*/
private static String doGet(String url,String mobileCode,String userID) throws Exception {
//准备参数
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
BasicNameValuePair mobile = new BasicNameValuePair("mobileCode",mobileCode);
BasicNameValuePair ID = new BasicNameValuePair("userID",userID);
params.add(mobile);
params.add(ID);
String paramsString = URLEncodedUtils.format(params, "UTF-8");
url += "?" + paramsString;
//创建get对象
HttpGet get = new HttpGet(url);
//创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//提交请求
CloseableHttpResponse response = null;
try {
response = httpclient.execute(get);
//获取状态码及响应数据
int status = response.getStatusLine().getStatusCode();
System.out.println("状态码为:" + status);
String result = EntityUtils.toString(response.getEntity());
System.out.println("响应数据为:" + result);
//创建Pattern对象
Pattern pat = Pattern.compile(">(.*)</");
//创建matcher对象
Matcher m = pat.matcher(result);
if (m.find( )){
return m.group(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (response != null) {
response.close();
}
//相当于关闭浏览器
httpclient.close();
}
return null;
}
/*
* 实现post类型接口的调用
*/
private static String doPost(String url,String mobileCode,String userID) throws Exception {
//创建post对象
HttpPost post = new HttpPost(url);
//准备参数
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
BasicNameValuePair mobile = new BasicNameValuePair("mobileCode",mobileCode);
BasicNameValuePair ID = new BasicNameValuePair("userID",userID);
params.add(mobile);
params.add(ID);
//将参数封装到请求体当中
post.setEntity(new UrlEncodedFormEntity(params));
//创建httpclient对象发送请求
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try { response = httpclient.execute(post);
//获取状态码及响应数据
int status = response.getStatusLine().getStatusCode();
System.out.println("状态码为:" + status);
String result = EntityUtils.toString(response.getEntity());
System.out.println("响应数据为:" + result);
// 创建 Pattern对象
Pattern pat = Pattern.compile(">(.*)</");
// 现在创建 matcher对象
Matcher m = pat.matcher(result);
if (m.find( )) {
return m.group(1);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}finally {
if (response != null) {
response.close();
}
//相当于关闭浏览器
httpclient.close();
}
return null;
}
}

五、执行测试套

六:执行结果

最新文章

  1. sql server2008 代码折叠
  2. ceph与openstack对接
  3. Spring——(一)IoC
  4. Spark standlone安装与配置
  5. 【ASP.NET Web API教程】6.3 内容协商
  6. compiled python files
  7. QT 焦点事件(4种方式的解释,还有委托焦点)
  8. hdu 1576 A/B 拓展欧几里得算法
  9. hdu 2222 Keywords Search ac自动机模板
  10. jq的合成事件
  11. Android开发之如何保证Service不被杀掉(broadcast+system/app)
  12. 用openssl库RSA加密解密
  13. 解决jenkins shell执行sonar-scanner提示命令存在的问题
  14. TypeScript语法学习--基本类型
  15. 转载:指针delete后要设置为NULL
  16. C#将图片存放到SQL SERVER数据库中的方法
  17. 如何合理的规划jvm性能调优
  18. 《设计模式》-原则四:接口隔离原则(ISP)
  19. LeetCode解题报告—— Linked List Cycle II &amp; Reverse Words in a String &amp; Fraction to Recurring Decimal
  20. Glassfish Password Alias

热门文章

  1. JPA 相关API (一)
  2. 物流一站式单号查询之快递鸟API接口(附Demo源码)
  3. Python使用进程制作爬虫
  4. 对于RBAC与shiro的一些思考
  5. SpringBoot中的classpath
  6. Spring Cloud Alibaba 基础
  7. leetcode17gas-station
  8. Python列表排序方法汇总,超详细!
  9. django环境安装操作整理!
  10. php xml转数组