使用TestNG,Apahcje POI和Excel檔案進測試行資料驅動測試
阿新 • • 發佈:2021-08-16
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);
}
}