标签:dex 使用 数字 none work password repo puts fast
1、在pom文件添加POI依赖
<!--操作Excel数据-->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
2、新建一个Class命名为ExcelUtils
package com.test.excel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lh
* @date 2020/7/3
* @description
*/
public class ExcelUtils {
/**
* 读取Excel数据
* @return List
* @throws Exception
*/
public static List<Map<String ,Object>> readExcle() throws Exception{
//用流的方式读取Excel文件
FileInputStream fis = new FileInputStream("C:\\Users\\Think\\Desktop\\test.xlsx");
//获取Excel工作簿
XSSFWorkbook xf = new XSSFWorkbook(fis);
//获取第一个sheet
XSSFSheet sheet = xf.getSheetAt(0);
//获取第一行
int firstRow = sheet.getFirstRowNum();
//获取最后一行
int lastRow = sheet.getLastRowNum();
// System.out.println(lastRow);
//用于map设置key值,自定义
String columns[] = {"username", "password"};
List< Map<String ,Object>> list = new ArrayList<>();
for (int i = 0;i<=lastRow;i++){
XSSFRow row = sheet.getRow(i);
if (row!=null){
//获取第一行,第一列
int firstcell = row.getFirstCellNum();
//获取最后一行,最后一列
int lastcell = row.getLastCellNum();
Map<String ,Object> map = new HashMap<>();
for (int j = 0;j<columns.length;j++){
XSSFCell cell = row.getCell(j);
if (cell == null){
continue;
}
//封装成map
map.put(columns[j], getCellFormatValue(cell));
}
//System.out.println(map);
//将map放入List
list.add(map);
}
fis.close();
xf.close();
}
return list;
}
//格式化数值类型
public static Object getCellFormatValue(Cell cell){
Object cellValue = null;
/**
* getCellTypeEnum()方法是枚举类型,用于判断单元格值类型,有以下五种格式:
* _NONE(-1),
* NUMERIC(0),
* STRING(1),
* FORMULA(2),
* BLANK(3),
* BOOLEAN(4),
* ERROR(5);
*/
switch(cell.getCellTypeEnum()){
case STRING:
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
/**如果是数字类型转换成数字类型,但是初始化数字加的.0,因此可以转换成int类型去掉.0
* cell.getNumericCellValue()
*/
cellValue = new Double(cell.getNumericCellValue()).intValue();
break;
case BOOLEAN:
break;
case FORMULA:
break;
}
return cellValue;
}
//测试
public static void main(String[] arg) throws Exception{
ExcelUtils excelTest = new ExcelUtils();
excelTest.readExcle();
}
}
package com.test.httpclient;
import com.alibaba.fastjson.JSONObject;
import com.test.excel.ExcelUtils;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.List;
import java.util.Map;
/**
* @author lh
* @date 2020/6/8
* @description
*/
@Test
public class LoginCase {
HttpClientPost httpClientPost = new HttpClientPost();
@DataProvider
public Object[][] getDataMethod() throws Exception{
//读取Excel中的数据
List<Map<String,Object>> result = ExcelUtils.readExcle();
Object[][] params = new Object[result.size()][];
for (int i = 0; i < result.size(); i++) {
params[i] = new Object[]{result.get(i)};
}
return params;
}
@BeforeTest
public void setup(){
System.out.println("用例执行前执行");
}
//一个测试方法
@Test(dataProvider = "getDataMethod")
public void test1(Map<String, Object> param) throws Exception {
JSONObject result = httpClientPost.LoginPost(param);
Assert.assertEquals(result.get("code"),200);
}
@AfterTest
public void teardown(){
System.out.println("用例执行完后执行");
}
}
四、查看运行结果
标签:dex 使用 数字 none work password repo puts fast
原文地址:https://www.cnblogs.com/liho/p/13260638.html