说明:

1.使用Exce作为数据存放地;

2.使用TestNG的Datarprovide 做数据供应;

3.不足的地方没有指定明确的result_code , error_code , ERROR_MSG ,如果知道明确规定error_code就可以直接用error_code来作为测试结果;

4.代码有许多需要改动的地方本次是第一版;

5.可以将整个小项目打成jar包执行,将excle的文件放入C盘根目录即可(毕竟每个电脑都有C盘,这样就不受机器的限制了)

6.关于本次用到的jar包有

TestNG+Excel+(HTTP+JSON) 简单接口测试

TestNG+Excel+(HTTP+JSON) 简单接口测试

 

一,读取EXCLE的相关代码

  1 package main.java;
  2 
  3 import org.apache.poi.ss.usermodel.*;
  4 
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.IOException;
  8 import java.util.ArrayList;
  9 import java.util.HashMap;
 10 import java.util.List;
 11 import java.util.Map;
 12 
 13 public class ExcelReader {
 14     private String filePath;
 15     private String sheetName;
 16     private Workbook workBook;
 17     private Sheet sheet;
 18     private List<String> columnHeaderList;
 19     private List<List<String>> listData;
 20     private List<Map<String, String>> mapData;
 21     private boolean flag;
 22     public Object[][] results;
 23 
 24     public ExcelReader(String filePath, String sheetName) {
 25         this.filePath = filePath;
 26         this.sheetName = sheetName;
 27         this.flag = false;
 28         this.load();
 29     }
 30 
 31     private void load() {
 32         FileInputStream inStream = null;
 33         try {
 34             inStream = new FileInputStream(new File(filePath));
 35             workBook = WorkbookFactory.create(inStream);
 36             sheet = workBook.getSheet(sheetName);
 37         } catch (Exception e) {
 38             e.printStackTrace();
 39         } finally {
 40             try {
 41                 if (inStream != null) {
 42                     inStream.close();
 43                 }
 44             } catch (IOException e) {
 45                 e.printStackTrace();
 46             }
 47         }
 48     }
 49 
 50     private String getCellValue(Cell cell) {
 51         String cellValue = "";
 52         DataFormatter formatter = new DataFormatter();
 53         if (cell != null) {
 54             switch (cell.getCellType()) {
 55                 case Cell.CELL_TYPE_NUMERIC:
 56                     if (DateUtil.isCellDateFormatted(cell)) {
 57                         cellValue = formatter.formatCellValue(cell);
 58                     } else {
 59                         double value = cell.getNumericCellValue();
 60                         int intValue = (int) value;
 61                         cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
 62                     }
 63                     break;
 64                 case Cell.CELL_TYPE_STRING:
 65                     cellValue = cell.getStringCellValue();
 66                     break;
 67                 case Cell.CELL_TYPE_BOOLEAN:
 68                     cellValue = String.valueOf(cell.getBooleanCellValue());
 69                     break;
 70                 case Cell.CELL_TYPE_FORMULA:
 71                     cellValue = String.valueOf(cell.getCellFormula());
 72                     break;
 73                 case Cell.CELL_TYPE_BLANK:
 74                     cellValue = "";
 75                     break;
 76                 case Cell.CELL_TYPE_ERROR:
 77                     cellValue = "";
 78                     break;
 79                 default:
 80                     cellValue = cell.toString().trim();
 81                     break;
 82             }
 83         }
 84         return cellValue.trim();
 85     }
 86 
 87     private void getSheetData() {
 88 
 89         listData = new ArrayList<>();
 90         mapData = new ArrayList<>();
 91         columnHeaderList = new ArrayList<>();
 92         int numOfRows = sheet.getLastRowNum() + 1;
 93         for (int i = 0; i < numOfRows; i++) {
 94             Row row = sheet.getRow(i);
 95             Map<String, String> map = new HashMap<>();
 96             List<String> list = new ArrayList<>();
 97 
 98             if (row != null) {
 99                 for (int j = 0; j < row.getLastCellNum(); j++) {
100                     Cell cell = row.getCell(j);
101                     if (i == 0) {
102                         columnHeaderList.add(getCellValue(cell));
103                     } else {
104 
105                         map.put(columnHeaderList.get(j), this.getCellValue(cell));
106 
107                     }
108                     list.add(this.getCellValue(cell));
109                 }
110             }
111             if (i > 0) {
112                 mapData.add(map);
113             }
114             listData.add(list);
115 
116 
117         }
118 
119         flag = true;
120 
121         for (int i = 0; i < listData.size(); i++) {
122             for (int j = 0; j < listData.get(i).size(); j++) {
123                 System.out.println(listData.get(i).get(j).toString());
124             }
125         }
126 
127     }
128 
129     public String getCellData(int row, int col) {
130         if (row <= 0 || col <= 0) {
131             return null;
132         }
133         if (!flag) {
134             this.getSheetData();
135         }
136         if (listData.size() >= row && listData.get(row - 1).size() >= col) {
137             return listData.get(row - 1).get(col - 1);
138         } else {
139             return null;
140         }
141     }
142 
143     public String getCellData(int row, String headerName) {
144         if (row <= 0) {
145             return null;
146         }
147         if (!flag) {
148             this.getSheetData();
149         }
150         if (mapData.size() >= row && mapData.get(row - 1).containsKey(headerName)) {
151             return mapData.get(row - 1).get(headerName);
152         } else {
153             return null;
154         }
155     }
156 
157 
158     public Object[][] getSheetData2() {
159 
160         List<Object[]> result = new ArrayList<>();
161         listData = new ArrayList<>();
162         mapData = new ArrayList<>();
163         columnHeaderList = new ArrayList<>();
164 
165         int numOfRows = sheet.getLastRowNum() + 1;
166         System.out.println("总共有 " + numOfRows + "行 !");
167         for (int i = 0; i < numOfRows; i++) {
168             Row row = sheet.getRow(i);
169             Map<String, String> map = new HashMap<>();
170             List<String> list = new ArrayList<>();
171             Object[] o1 = new Object[row.getLastCellNum()];
172 
173             if (row != null) {
174                 for (int j = 0; j < row.getLastCellNum(); j++) {
175                     //   System.out.println("第 "+i+" 行--- row.getLastCellNum()===="+row.getLastCellNum());
176                     Cell cell = row.getCell(j);
177                     if (i == 0) {
178                         o1[j] = this.getCellValue(cell);
179                         //       System.out.println(j+"------this.getCellValue(cell)="+this.getCellValue(cell));
180                         columnHeaderList.add(getCellValue(cell));
181                     } else {
182                         o1[j] = this.getCellValue(cell);
183                         // System.out.println(j+"------this.getCellValue(cell)="+this.getCellValue(cell));
184                         map.put(columnHeaderList.get(j), this.getCellValue(cell));
185 
186                     }
187                     list.add(this.getCellValue(cell));
188                 }
189             }
190             if (i > 0) {
191                 mapData.add(map);
192             }
193             result.add(o1);
194             listData.add(list);
195         }
196         // 测试数据excel数据用 ;
197       /*    for (int i = 0; i < result.size(); i++) {
198             for (int j = 0; j < result.get(i).length; j++) {
199                 System.out.print(result.get(i)[j]+" | ");
200             }
201             System.out.println();
202         }*/
203         results = new Object[result.size()][];
204 
205         for (int i = 0; i < result.size(); i++) {
206             results[i] = result.get(i);
207         }
208         flag = true;
209 
210         System.out.println("results.length==" + results.length);
211         return results;
212     }
213 
214     public static void main(String[] args) {
215  /*       Object[][] obj1;
216         ExcelReader eh = new ExcelReader("C:\\TEST.xlsx", "Sheet1");
217         Object[][] sheetData2 = eh.getSheetData2();
218         System.out.println(sheetData2.length + "------------");
219         for (int i = 1; i < sheetData2.length; i++) {
220             for (int j = 0; j < sheetData2[i].length; j++) {
221                 System.out.print(sheetData2[i][j] + " | ");
222             }
223             System.out.println();
224         }*/
225 
226 
227     }
228 }
View Code

相关文章:

  • 2022-01-01
  • 2021-11-17
  • 2021-11-27
  • 2022-02-03
  • 2022-12-23
猜你喜欢
  • 2022-02-21
  • 2022-12-23
  • 2021-10-14
  • 2021-08-24
  • 2021-05-02
  • 2022-12-23
  • 2022-02-09
相关资源
相似解决方案