import com.cxy_fanwe.common.test_fanwe_qiantai;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class fanwe_chongzhi {
private test_fanwe_qiantai login = new test_fanwe_qiantai();
private String url;
private ResourceBundle bundle;
@BeforeMethod
public void get_login_url(){
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString("fanwe.qiantai.url");
}

//读取Excel文件,封装在object[][]中
public Object[][] read_excel_login(String filePath,String sheetName) throws Exception {
//声明一个File对象
File file = new File(filePath);
//创建FileInputStream对象用来读取Excel文件
FileInputStream inputStream = new FileInputStream(file);
//声明Workbook对象
Workbook workbook= null;
//获取文件的参数名,判断是.xlsx还是.xls
String fileExtentsName = filePath.substring(filePath.indexOf("."));
//如果是.xlsx 使用XSSFWorkbook对象进行实例化
//如果是.xls 使用HSSFWorkbook对象进行实例化
if (fileExtentsName.equals(".xlsx")){
workbook = new XSSFWorkbook(inputStream);
}else if (fileExtentsName.equals(".xls")){
workbook = new HSSFWorkbook(inputStream);
}

//获取sheet对象
Sheet sheet = workbook.getSheet(sheetName);
//获取行数 最后一行的行号-第一行
int rowcount = sheet.getLastRowNum()-sheet.getFirstRowNum();
//创建list 对象存储Excel的数据
List<Object[]> records = new ArrayList<Object[]>();
//从1开始,去除首行标题行
for (int i = 1; i <rowcount+1 ; i++) {
Row row = sheet.getRow(i);
String fileds[] = new String[row.getLastCellNum()];
for (int j = 0; j <row.getLastCellNum(); j++) {

Cell cell = row.getCell(j);
fileds[j] = row.getCell(j).toString();

}
records.add(fileds);
}
//将records数组转成二维数组
Object[][] result = new Object[records.size()][];
for (int i = 0; i <records.size() ; i++) {
result[i] = records.get(i);
}
return result;
}

//调用读取Excel的方法,获得Object二维数组
@DataProvider(name="loginname")
public Object[][] get_excel_login() throws Exception {
Object[][] result = read_excel_login("src/main/com/cxy_fanwe/data/fanwe_username.xls","fanwe_username");
return result;
}
//数据驱动批量登录
@Test(dataProvider = "loginname")
public void fawe_login_qudong(String loginname) throws Exception {
String url = "http://192.168.232.138/fanwe/index.php?ctl=user&act=dologin&fhash=jhJhXBLeIeLhigZFcmspHeEqyoOGxRrlesHfEyfjHaZqIQgwIR";
String uri = bundle.getString("fanwe.qiantai.login");
String url_login =url+uri;
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url_login);
//设置请求头
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//添加请求参数
String param = "email="+loginname+"&user_pwd=ZFhKdXpMSE5WS3JPRmRrYmVDSlFVdlpPY2JxbXluR2RoRFFCZ3BWa1BEcG9JeU5pc00lMjV1NjVCOSUyNXU3RUY0Y3h5MTIzNDU2JTI1dThGNkYlMjV1NEVGNg==&ajax=1";
StringEntity entity = new StringEntity(param);
post.setEntity(entity);
//发送post请求
CloseableHttpResponse response = client.execute(post);
//获取cookie
String cookie_login = response.getFirstHeader("Set-Cookie").getValue();
String result = EntityUtils.toString(response.getEntity());
//将响应结果装换成json
JSONObject jsonObject =new JSONObject(result);
int status = (int) jsonObject.get("status");
String info = (String) jsonObject.get("info");
System.out.println("status: "+status+" info: "+info);
Assert.assertEquals(2,status);
Assert.assertEquals("本站需绑定第三方托管账户,是否马上去绑定",info);
}
}

使用TestNG,Apahcje POI和Excel文件进测试行数据驱动测试的更多相关文章

  1. Java Struts2 POI创建Excel文件并实现文件下载

    Java Struts2 POI创建Excel文件并实现文件下载2013-09-04 18:53 6059人阅读 评论(1) 收藏 举报 分类: Java EE(49) Struts(6) 版权声明: ...

  2. JAVA使用POI读取EXCEL文件的简单model

    一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...

  3. POI生成EXCEL文件

    POI生成EXCEL文件 一.背景 根据指定格式的JSON文件生成对应的excel文件,需求如下 支持多sheet 支持单元格合并 支持插入图片 支持单元格样式可定制 需要 标题(title),表头( ...

  4. java使用jxl,poi解析excel文件

    public interface JavaExcel { /** * 使用jxl写excel文件 */ public void writeJxlExcel(); /** * 使用jxl读excel文件 ...

  5. 使用poi读写excel文件

    使用poi库测试了一下读取excel文件,效果不错,跟大家分享一下. 第一列是数值型,第二列是字符型,代码如下: package poi; import java.io.FileInputStream ...

  6. java通过poi编写excel文件

    public String writeExcel(List<MedicalWhiteList> MedicalWhiteList) { if(MedicalWhiteList == nul ...

  7. java使用POI实现excel文件的读取,兼容后缀名xls和xlsx

    需要用的jar包如下: 如果是maven管理的项目,添加依赖如下: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --&g ...

  8. poi解析Excel文件版本问题

    poi解析Excel文件时有两种格式: HSSFWorkbook格式用来解析Excel2003(xls)的文件 XSSFWorkbook格式用来解析Excel2007(xlsx)的文件 如果用HSSF ...

  9. Java使用POI操作Excel文件

    1.简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式文件读和写的功能. 2.依赖的jar包 <!-- ex ...

随机推荐

  1. SpringCloud:扩展zuul配置路由访问

    继续上次整合SpringCloud的demo进行扩展zuul:https://www.cnblogs.com/nhdlb/p/12555968.html  这里我把zuul划分出一个模块单独启动 创建 ...

  2. <clinit>() 和 <init>()

    原文:https://www.cnblogs.com/aspirant/p/7200523.html <clinit>() 类构造器方法 在 类初始化阶段 被执行 由编译器自动收集类中的所 ...

  3. c语言格式字符

    格式说明由"%"和格式字符组成,如%d%f等.它的作用是将输出的数据转换为指定的格式输出.格式说明总是由"%"字符开始的. 格式字符有d,o,x,u,c,s,f ...

  4. 「CF527E」 Data Center Drama

    「CF527E」 Data Center Drama 传送门 显然一个环肯定满足题目条件. 然后我就开始想:先整一棵 \(\texttt{DFS}\) 树,然后非树边从深度深的节点向深度浅的节点连边, ...

  5. 「CF1208G」 Polygons

    「CF1208G」 Polygons 似乎我校神犇在很久以前和我提过这题? 首先有一点显而易见:这 \(k\) 个多边形肯定至少有一个公共的顶点.假设我们将此点定义为起点. 那么对于一个正 \(n\) ...

  6. C语言:for语句原理及具体执行过程

    #include <stdio.h> int main() { int i,s=0; for(i=1;;i*=2) { s=s+i; if(i%3==0) break; i=i+2; pr ...

  7. pytorch中网络特征图(feture map)、卷积核权重、卷积核最匹配样本、类别激活图(Class Activation Map/CAM)、网络结构的可视化方法

    目录 0,可视化的重要性: 1,特征图(feture map) 2,卷积核权重 3,卷积核最匹配样本 4,类别激活图(Class Activation Map/CAM) 5,网络结构的可视化 0,可视 ...

  8. ES6 箭头函数及this

    ES6 箭头函数及this 1.箭头函数 <script type="text/javascript"> //以前定义函数 let fun=function () { ...

  9. 使用vue-preview报错Cannot read property 'open' of undefined

    最近在做一个vue项目中时,需要使用vue-preview插件制作缩略图,首先在终端使用npm i vue-preview -S指令安装了vue-preview插件,然后在main.js中,导入并引用 ...

  10. 微信小程序云开发-云存储-上传单张照片到云存储并显示到页面上

    一.wxml文件 页面上写上传图片的按钮,按钮绑定chooseImg. <button bindtap="chooseImg" type="primary" ...