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);
}
}

最新文章

  1. MVC5 网站开发之七 用户功能 1、角色的后台管理
  2. 以application/json 方式提交 然后用在php中读取原始数据流的方式获取 在json_encode
  3. (六)文件操作的主要接口API
  4. 如何优化C语言代码(程序员必读)
  5. git会议分享
  6. Java知识点:javac命令
  7. Code (组合数)
  8. C语言基础学习学习前的准备-1
  9. 未能载入文件或程序集“DAL”或它的某一个依赖项。系统找不到指定的文件。
  10. js中取session的值
  11. UVA 10152-ShellSort(映射+栈)
  12. asp.net core 中灵活的配置方式
  13. java多线程 生产者和消费者 lock
  14. P4098 [HEOI2013]ALO
  15. IDEA修改JDK(全)
  16. 使用SVN提交代码,快速简单的换地址方法
  17. Jmeter连接MySQL和SQL Server配置(转载)
  18. 洛谷P2619 Tree I
  19. Django - Python3 配置 MySQL
  20. redis集群服务启动

热门文章

  1. 免费版:Xshell和Xftp下载路径
  2. Raspberry Pi:树莓派安装基础系统 Raspberry Pi(树莓派系统)
  3. Hibernate框架(二)POJO对象的操作
  4. MySQL | 使用Xtrabackup进行备份和备份恢复
  5. P5816 [CQOI2010]内部白点 题解
  6. JAVA基础之JDK、JRE、JVM关系
  7. Java基础00-基础知识练习12
  8. [刘阳Java]_Spring入门_第1讲
  9. LeetCode 780. Reaching Points
  10. awk对某个字段分割处理