• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yxchun

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

testng 接口测试,读取Excel表格数据,做数据驱动2(读取某些固定列数据)

testng

public class TestRegister {


    @Test(dataProvider="datas")
    public void test1(String,url,String username,String pwd){
    
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", username);
        params.put("pwd", pwd);
        System.out.println(HttpUtils.doPost(params,url));
    }

    @DataProvider
    public Object[][] datas(){
        //取出固定列的数据
        int[] rows = {1,2,3,4,5,6};
        int[] cols = {2,3,4};
        Object[][] datas = ExcelUtil.readExcel2("C:\\Users\\Administrator\\Desktop\\register.xls",rows,cols);
        return datas;
    }

}
ExcelUtil.readExcel2
    public static Object[] [] readExcel2(String  url,int[] rows,int[] cols){

        Object[] [] datas = new Object[rows.length][cols.length];
        //获取Workbook对象
        try {
            File file = new File(url);
            Workbook workBook = WorkbookFactory.create(file);
            //获取sheet 对象
            Sheet sheet =  workBook.getSheet("Sheet1");
            DataFormatter  formatter = new DataFormatter();
            //获取行
            for(int i = 0;i < rows.length ;i++){
                Row row = sheet.getRow(rows[i]);
                for(int j = 0;j < cols.length;j++){
                    Cell cell = row.getCell(cols[j], Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    String value = formatter.formatCellValue(cell);
                    datas[i][j] = value;
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return datas;
    }

表格

HttpUtils.doPost以及所需要jar包参考:https://www.cnblogs.com/ychun/p/15612428.html

posted on 2021-11-28 10:16  yxchun  阅读(108)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3