• 如何把excel 数据做dataprovide


    1.  新建一个类,实现接口Iterator

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    
    import org.testng.Assert;
    
    
     public class ExcelDataProvider implements Iterator<Object[]> {
    
        private Workbook book = null;
        private Sheet sheet = null;
        private int rowNum = 0;
        private int currentRowNo = 0;
        private int columnNum = 0;
        private String[] columnnName;
        private String path = null;
        private InputStream inputStream = null;
        public ExcelDataProvider(String fileName,String moduleName) {
    
            try {
                path = "TestData/" + fileName + ".xls";
                inputStream = new FileInputStream(path);
                book = Workbook.getWorkbook(inputStream);
                sheet = book.getSheet(moduleName);
                rowNum = sheet.getRows(); 
                Cell[] cell = sheet.getRow(0);
                columnNum = cell.length; 
                columnnName = new String[cell.length];
                for (int i = 0; i < cell.length; i++) {
                    columnnName[i] = cell[i].getContents().toString();
                }
                this.currentRowNo++;
            } catch (FileNotFoundException e) {
                Assert.fail("文件路径错误" + "[" + path + "]");
            } catch (Exception e) {
                Assert.fail("数据文件错误" + path + "]");
            }
        }
       public boolean hasNext() {
            if (this.rowNum == 0 || this.currentRowNo >= this.rowNum) {
    
                try {
                    inputStream.close();
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            } else {
                if ((sheet.getRow(currentRowNo))[0].getContents().equals(""))
                    return false;
                return true;
            }
        }
        public Object[] next() {
            Cell[] c = sheet.getRow(this.currentRowNo);
            Map<String, String> data = new HashMap<String, String>();
            for (int i = 0; i < this.columnNum; i++) {
                String temp = "";
                try {
                    temp = c[i].getContents().toString();
                } catch (ArrayIndexOutOfBoundsException ex) {
                    temp = "";
                }
                data.put(this.columnnName[i], temp);
            }
            Object object[] = new Object[1];
            object[0] = data;
            this.currentRowNo++;
            return object;
        }
    
        public void remove() {
            throw new UnsupportedOperationException("remove unsupported.");
        }
    }

    2 . 在testcase 中写个方法,获取数据

    @DataProvider(name="excelData")
    protected Iterator<Object[]> excel(Method method){
    String fileName = this.getClass().getSimpleName();
    String moduleName = method.getName();
    return new ExcelDataProvider(fileName,moduleName);
    }

     
  • 相关阅读:
    jQuery1.3.2 源码学习8 index 函数
    转发:在 IE 和 FireFox 中 Javascript 代码的调试视频
    关于 Fiddler 使用的两个常见问题的解决视频
    jQuery1.3.2 源码学习7 setArray,each 函数
    一个 Free 的 Web Server
    服务器端编程的10大性能问题
    Windows Socket五种I/O模型——代码全攻略
    几种winsock I/O模型的分析
    小谈Onlinegame服务器端设计(3)
    [转载]理解 I/O Completion Port (IOCP完成端口)
  • 原文地址:https://www.cnblogs.com/lgm1999/p/6233063.html
Copyright © 2020-2023  润新知