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);
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章