51Testing软件测试论坛

标题: selenium 入门学习 (四) [打印本页]

作者: 海鸥一飞    时间: 2018-2-7 15:16
标题: selenium 入门学习 (四)
     通过Excel对用例参数进行配置
     pom.xm依赖:
  1. <dependency>
  2.     <groupId>net.sourceforge.jexcelapi</groupId>
  3.     <artifactId>jxl</artifactId>
  4.     <version>2.6.12</version>
  5. </dependency>
复制代码
    Excel读取类:通过迭代器实现,这样比较方便封装,实现Excel读取类型的多态性;这里用的是
Map实现;
  1. public class ExcelIterator implements Iterator<Object[]> {
  2. private Workbook book = null;
  3. private Sheet sheet = null;

  4. private int rowNum = 0;
  5. private int curRowNo = 0;
  6. private int columnNum = 0;
  7. private String[] columnName;
  8. private int singleline = 0;

  9. public ExcelIterator(String filename, String testMethodname) {
  10.     try {
  11.         FileInputStream fs = new FileInputStream(filename);
  12.         this.book = Workbook.getWorkbook(fs);
  13.         this.sheet = book.getSheet(testMethodname);
  14.         this.rowNum = sheet.getRows();
  15.         Cell[] c = sheet.getRow(0);
  16.         this.columnNum = c.length;
  17.         columnName = new String[c.length];
  18.         for (int i = 0; i < c.length; i++) {
  19.             columnName[i] = c[i].getContents().toString();
  20.         }

  21.         if (this.singleline > 0) {
  22.             this.curRowNo = this.singleline;
  23.         } else {
  24.             this.curRowNo++;
  25.         }

  26.     } catch (FileNotFoundException e) {
  27.         System.out.println("文件IO异常");
  28.     } catch (BiffException e) {
  29.         e.printStackTrace();
  30.     } catch (IOException e) {
  31.         e.printStackTrace();
  32.     }
  33. }

  34. @Override
  35. public boolean hasNext() {
  36.     if (this.rowNum == 0 || this.curRowNo >= this.rowNum) {
  37.         book.close();
  38.         return false;
  39.     }
  40.     if (this.singleline > 0 && this.curRowNo > this.singleline) {
  41.         book.close();
  42.         return false;
  43.     }
  44.     return true;
  45. }

  46. @Override
  47. public Object[] next() {
  48.     Cell[] c = sheet.getRow(this.curRowNo);
  49.     Map<String, Object> s = new LinkedHashMap<String, Object>();

  50.     for (int i = 0; i < this.columnNum; i++) {
  51.         String data = c[i].getContents().toString();
  52.         s.put(this.columnName[i], data);
  53.     }
  54.     this.curRowNo++;
  55.     return new Object[] { s };
  56. }

  57. @Override
  58. public void remove() {
  59.     // TODO Auto-generated method stub
  60. }
  61. }
复制代码
    读取Excle用例示例:
  1. public class TestDriverData {
  2.     @Test(dataProvider = "test_selenium1")
  3.     public void f(Map<String, String> data) {
  4.         System.out.println("用例标题:" + data.get("comment"));
  5.   }

  6.     @DataProvider(name = "test_selenium1")
  7.     public Iterator<Object[]> dataProvider() {
  8.         return new ExcelIterator("D:\\data\\ErShouFangWebDetailTest.xls", "testEsfWebDetail");
  9.     }
  10. }
复制代码
    Excel展示:
[attach]110689[/attach]
     Excel读取执行用例结果:
[attach]110690[/attach]



作者: 梦想家    时间: 2018-5-14 16:53





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2