zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    我与计算机简述
    第十五周计应151班第四组排球计分程序
    第十五周排球积分查询程序
    本周总结
    计应1班第4小组第一次产品计划会议
    本周总结
    计应1班第4小组“排球比赛计分程序”的典型用户、创立场景、用户故事
    排球计分规则功能说明书
    我与计算机
    5th 各组作品alpha发布体会
  • 原文地址:https://www.cnblogs.com/ychun/p/15614407.html
Copyright © 2011-2022 走看看