zoukankan      html  css  js  c++  java
  • 第二版_TestNG+Excel+(HTTP+JSON) 简单接口测试

    -----------------------------------------------------------------------------------------------------------------------------------------

    说明:

    >1.首先必须在指定目录创建 指定文件 ; c:/c/TEST1.xlsx

    >2.ExcleUtil类只读取了TEST1.xlsx 前4列的值 ;

    >3.ExcleUtil类将接口的返回的json值写入到excle中 ;

    >4.HttpInterfaceTest 类中的testSendPost()方法使用Object ... obj 进行参数接收 ;

    ------------------------------------------------------------------------------------------------------------------------------------------

    1.ExcelReader  --读取*.xls,*.xlsx 格式的 excle ;

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

    1.1 20180131——setCellData2()方法的更新 ;

     1     public static void setCellData2(int rownum, int colnum, String result) {
     2         FileOutputStream out = null;
     3         if (null == sheet.getRow(rownum)) {
     4             Row row1 = sheet.createRow(rownum);
     5             if (null == row1.getCell(colnum)) {
     6                 row1.createCell(colnum).setCellValue(result);
     7             } else {
     8                 row.getCell(colnum).setCellValue(result);
     9             }
    10         } else {
    11             sheet.getRow(rownum).createCell(colnum).setCellValue(result);
    12 
    13         }
    14 
    15         try {
    16             out = new FileOutputStream(ExcelFilePath);
    17             workBook.write(out);
    18             out.flush();
    19 
    20         } catch (IOException e) {
    21             e.printStackTrace();
    22         }finally {
    23             try {
    24                 out.close();
    25                 System.out.println("-----写入成功!------");
    26             } catch (Exception e) {
    27                 e.printStackTrace();
    28             }
    29         }
    30 
    31     }
    View Code


    2.ExcleUtil --读取和设置单元格的值 ;

      1 package main.java;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 
      9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     10 import org.apache.poi.ss.usermodel.Row;
     11 import org.apache.poi.ss.usermodel.Sheet;
     12 import org.apache.poi.ss.usermodel.Workbook;
     13 import org.apache.poi.xssf.usermodel.XSSFCell;
     14 import org.apache.poi.xssf.usermodel.XSSFRow;
     15 import org.apache.poi.xssf.usermodel.XSSFSheet;
     16 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     17 
     18 
     19 public class ExcleUtil {
     20     private static XSSFSheet ExcelWSheet;
     21     private static XSSFWorkbook ExcelWBook;
     22     private static XSSFCell Cell;
     23     private static XSSFRow Row;
     24     private static String ExcelFilePath="C:\C\TEST1.xlsx";
     25 
     26     // 设定要设置的Excel的文件路径和Excel 中Sheet名;
     27     // 在读/写Excel 的时候先要调用此方法
     28     public static void setExcleFile(String FilePath, String sheetName) throws Exception {
     29         FileInputStream ExcleFile;
     30         try {
     31             // 实例化Excle文件的FileInputStream 对象;
     32             ExcleFile = new FileInputStream(FilePath);
     33             // 实例化Excle文件的XSSFWorkbook 对象;
     34             ExcelWBook = new XSSFWorkbook(ExcleFile);
     35             /*
     36              * 实例化XSSFSheet 对象,指定ExcelFile中的sheet名称,用于后续对sheet中行和列的操作;
     37              * 
     38              */
     39             ExcelWSheet = ExcelWBook.getSheet(sheetName);
     40 
     41         } catch (Exception e) {
     42             e.getStackTrace();
     43         }
     44 
     45     }
     46     /*
     47      * 读取excle文件指定单元格的函数 ;
     48      * 
     49      */
     50 
     51     public static String getCell(int row, int col) throws Exception {
     52 
     53         try {
     54             // 通过函数参数指定单元格的行号和列,获取指定单元格的对象;
     55             Cell = ExcelWSheet.getRow(row).getCell(col);
     56             /*
     57              * 1.如果单元格的类型为字符串类型,使用getStringCellValue();来获取单元格的内容;
     58              * 2.如果单元格的类型为数字类型,使用getNumberricCellValue();来获取单元格的内容;
     59              * 注意:getNumberricCellValue();返回的值为double类型,转为为字符串类型,必须在
     60              * getNumberricCellValue();前面加上(" ")双引号,用于强制转换为String类型,不加双引号
     61              * 则会抛错;double类型无法转换为String类型的异常;
     62              * 
     63              */
     64             String CellData = Cell.getCellType() == XSSFCell.CELL_TYPE_STRING ? Cell.getStringCellValue() + ""
     65                     : String.valueOf(Math.round(Cell.getNumericCellValue()));
     66             return CellData;
     67         } catch (Exception e) {
     68             e.getStackTrace();
     69             return "";
     70         }
     71 
     72     }
     73     /*
     74      * 在Excle中执行单元格写入数据;
     75      * 
     76      * 
     77      */
     78 
     79     public static void setCellData(int rownum, int colnum, String Result) throws Exception {
     80 
     81         try {
     82             // 获取excle文件的中行对象;
     83             Row = ExcelWSheet.getRow(rownum);
     84             // 如果单元格为空则返回null;
     85             Cell = Row.getCell(colnum, Row.RETURN_BLANK_AS_NULL);
     86             if (Cell == null) {
     87                 // 当单元格为空是则创建单元格
     88                 // 如果单元格为空无法调用单元格对象的setCellValue方法设定单元格的值 ;
     89                 Cell = Row.createCell(colnum);
     90                 // 创建单元格和后可以通过调用单元格对象的setCellValue方法设置单元格的值了;
     91                 Cell.setCellValue(Result);
     92             } else {
     93                 // 单元格中有内容,则可以直接调用单元格对象的 setCellValue 方法来设置单元格的值;
     94                 Cell.setCellValue(Result);
     95             }
     96             FileOutputStream fileout = new FileOutputStream(ExcelFilePath);
     97             // 将内容写到Excel文件中 ;
     98             ExcelWBook.write(fileout);
     99             // j调用flush方法强制刷新写入文件;
    100             fileout.flush();
    101             fileout.close();
    102             System.out.println("-----写入成功!------");
    103         } catch (Exception e) {
    104             System.out.println(e.getMessage() + e.getStackTrace());
    105             throw (e);
    106         }
    107 
    108     }
    109 
    110     public static void TangsetCellData(int RowNum, int ColNum, String Result) {
    111         try {
    112             // 获取行对象
    113             Row = ExcelWSheet.getRow(RowNum);
    114             // 如果单元格为空,则返回null
    115             Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
    116             if (Cell == null) {
    117                 // 当单元格对象是Null时,则创建单元格
    118                 // 如果单元格为空,无法直接调用单元格的setCellValue方法设定单元格的值
    119                 Cell = Row.createCell(RowNum);
    120                 // 调用setCellValue方法设定单元格的值
    121                 Cell.setCellValue(Result);
    122             } else {
    123                 // 单元格中有内容,则可以直接调用seCellValue方法设定单元格的值
    124                 Cell.setCellValue(Result);
    125             }
    126             // 实例化写入Excel文件的文件输出流对象
    127             FileOutputStream fileOut = new FileOutputStream(ExcelFilePath);
    128             // 将内容写入Excel中
    129             ExcelWBook.write(fileOut);
    130             fileOut.flush();
    131             fileOut.close();
    132         } catch (Exception e) {
    133             // TODO: handle exception
    134             e.printStackTrace();
    135         }
    136     }
    137 
    138     // 从excel 文件中获取测试数据的静态方法;
    139     public static Object[][] getTestData(String excelFilePath, String sheetName) throws Exception {
    140         // 根据参数传入的数据文件路径和文件名称,组合出Excel 数据文件的绝对路径
    141         // 声明一个文件;
    142         File file = new File(excelFilePath);
    143         // 创建FileInputStream 来读取Excel文件内容;
    144         FileInputStream inputStream = new FileInputStream(file);
    145         // 声明Workbook 对象;
    146         Workbook workbook = null;
    147         // 获取文件名参数的扩展名,判断是“.xlsx” 还是 “.xls” ;
    148         String fileExtensionName = excelFilePath.substring(excelFilePath.indexOf('.'));
    149         if (fileExtensionName.equals(".xlsx")) {
    150             workbook = new XSSFWorkbook(inputStream);
    151 
    152         } else if (fileExtensionName.equals(".xls")) {
    153             workbook = new HSSFWorkbook(inputStream);
    154 
    155         }
    156         Sheet sheet = workbook.getSheet(sheetName);
    157         // 获取Excel 数据文件Sheet1 中数据的行数,getLastRowNum 方法获取数据的最后一行的行号,
    158         // getFistRowNum 获取第一行 最后一行减去第一行就是总行数了
    159         // 注意excle 的行和列都是从0开始的;
    160         int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
    161         // 创建名为records 的List对象来存储从Excel文件中读取的数据;
    162         List<Object[]> records = new ArrayList<Object[]>();
    163         // 使用for循环遍历Excel 数据文件的所有数据(除了第一行,第一行为标题行),所以i从1开始而不是从0开始;
    164 
    165         for (int i = 1; i < rowCount + 1; i++) {
    166             // 使用getRow来获取行对象;
    167             Row row = sheet.getRow(i);
    168             /*
    169              * 声明一个数据,用来存储Excel数据文件每行中的测试用例和数据,数据的大小用getLastCellNum-2
    170              * 来进行动态声明,实现测试数据个数和数组大小一致,
    171              * 因为Excel数据文件中的测试数据行的最后一个单元格是测试执行结果,倒数第二个单元格为此测试数据行是否运行的状态位,
    172              * 所以最后俩列的单元格数据并
    173              * 不需要传入测试方法中,所以是用getLastCellNum-2的方式去掉每行中的最后俩个单元格数据,计算出需要存储的测试数据个数,
    174              * 并作为测试数据数组的初始化大小
    175              * 
    176              */
    177             String fields[] = new String[row.getLastCellNum() - 2];
    178 
    179             /*
    180              * 判断数据行是否要参与测试的执行,Excel 文件的倒数第二列为数据行的状态位, 标记为“y”
    181              * 表示此数据行要被测试脚本执行,标记为非“y”的数据行均被认为不会参数测试脚本执行,会被跳过;
    182              */
    183 
    184             if (row.getCell(row.getLastCellNum() - 2).getStringCellValue().equals("y")) {
    185                 for (int j = 0; j < row.getLastCellNum() - 2; j++) {
    186                     /*
    187                      * 判断Excel 单元格的内容是数字还是字符, 字符格式调用:
    188                      * row.getCell(j).getStringCellValue();
    189                      * 数字格式调用:row.getCell(j).getNumericCellValue();
    190                      */
    191                     fields[j] = (String) (row.getCell(j).getCellType() == XSSFCell.CELL_TYPE_STRING
    192                             ? row.getCell(j).getStringCellValue() : "" + row.getCell(j).getNumericCellValue());
    193 
    194                 }
    195                 // fields 存储到数组当中;
    196                 records.add(fields);
    197 
    198             }
    199         }
    200 
    201         /*
    202          * 定义函数的返回值,即Object[] [] 将存储测试数据的list 转换为一个Object 的二维数组;
    203          */
    204         Object[][] results = new Object[records.size()][];
    205         for (int i = 0; i < records.size(); i++) {
    206             results[i] = records.get(i);
    207         }
    208 
    209         return results;
    210 
    211     }
    212 
    213     public static int getLastColumnNum() {
    214 
    215         return ExcelWSheet.getRow(0).getLastCellNum() - 1;
    216     }
    217 
    218     public static void main(String[] args) throws Exception {
    219 
    220         //ExcleUtil.setCellData(3,3,"hellojava!");
    221         //System.out.println(ExcleUtil.getCell(3,1) );
    222                ;
    223 
    224 
    225 
    226     }
    227 
    228 
    229 }
    View Code

    3.HttpInterfaceTest --接口 get 和 post 的方法 ;

      1 package main.java;
      2 import net.sf.json.JSONObject;
      3 import java.io.BufferedReader;
      4 import java.io.IOException;
      5 import java.io.InputStreamReader;
      6 import java.io.PrintWriter;
      7 import java.net.URL;
      8 import java.net.URLConnection;
      9 import java.util.List;
     10 import java.util.Map;
     11 
     12 /**
     13  * Created by ty on 2017/8/17.
     14  */
     15 public class HttpInterfaceTest {
     16 
     17 
     18     public String sendGet(String url, String param) {
     19         String result = "";
     20         BufferedReader in = null;
     21         try {
     22             String urlName = url + "?" + param;
     23             System.out.println("Get请求接口:" + urlName);
     24             URL realUrl = new URL(urlName);
     25             // 打开和URL之间的连接
     26             URLConnection conn = realUrl.openConnection();
     27             // 设置通用的请求属性
     28             conn.setRequestProperty("accept", "*/*");
     29             conn.setRequestProperty("connection", "Keep-Alive");
     30             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
     31             // 建立实际的连接
     32             conn.connect();
     33             // 获取所有响应头字段
     34             Map<String, List<String>> map = conn.getHeaderFields();
     35             // 遍历所有的响应头字段
     36             for (String key : map.keySet()) {
     37                 System.out.println(key + "--->" + map.get(key));
     38             }
     39             // 定义BufferedReader输入流来读取URL的响应
     40             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
     41             String line;
     42             while ((line = in.readLine()) != null) {
     43                 result += "
    " + line;
     44             }
     45         } catch (Exception e) {
     46          //   System.out.println("发送GET请求出现异常!" + e);
     47             e.printStackTrace();
     48         }
     49         // 使用finally块来关闭输入流
     50         finally {
     51             try {
     52                 if (in != null) {
     53                     in.close();
     54                 }
     55             } catch (IOException ex) {
     56                 ex.printStackTrace();
     57             }
     58         }
     59         return result;
     60     }
     61 
     62     /**
     63      * 向指定URL发送POST方法的请求
     64      *
     65      * @param url   发送请求的URL
     66      * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式或者是json。
     67      * @return URL所代表远程资源的响应
     68      */
     69     public String sendPost(String url, String param) {
     70         PrintWriter out = null;
     71         BufferedReader in = null;
     72         String result = "";
     73         String jsonName="Content=";
     74         try {
     75             URL realUrl = new URL(url);
     76             // 打开和URL之间的连接
     77             URLConnection conn = realUrl.openConnection();
     78             // 设置通用的请求属性
     79             conn.setRequestProperty("accept", "*/*");
     80             conn.setRequestProperty("connection", "Keep-Alive");
     81          conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)") ;
     82             //Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
     83            // conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
     84             conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
     85            // application/x-www-form-urlencoded
     86             // 发送POST请求必须设置如下两行
     87             conn.setDoOutput(true);
     88             conn.setDoInput(true);
     89 
     90             // 获取URLConnection对象对应的输出流
     91             out = new PrintWriter(conn.getOutputStream());
     92             // 发送请求参数
     93             out.print(jsonName+param);
     94             System.out.println("jsonName+param="+jsonName+param);
     95             // flush输出流的缓冲
     96             out.flush();
     97             // 定义BufferedReader输入流来读取URL的响应
     98             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
     99             String line;
    100             while ((line = in.readLine()) != null) {
    101                 result += "
    " + line;
    102             }
    103         } catch (Exception e) {
    104             System.out.println("Send Post Excepion :" + e);
    105             e.printStackTrace();
    106         }
    107         // 使用finally块来关闭输出流、输入流
    108         finally {
    109             try {
    110                 if (out != null) {
    111                     out.close();
    112                 }
    113                 if (in != null) {
    114                     in.close();
    115                 }
    116             } catch (IOException ex) {
    117                 ex.printStackTrace();
    118             }
    119         }
    120         return result;
    121     }
    122 
    123 
    124   /*  public static void main(String[] args) {
    125 
    126         HttpInterfaceTest httpInterfaceTest = new HttpInterfaceTest();
    127 
    128         System.out.println("----------------");
    129          String cissUrl="http://192.168.1.110:7001/CISS/MSG/callTrxMsgManager.action";
    130         String PostValue = "{MESSAGEID:'ZTS00120170914110859000043',
    " +
    131                 "INTERFACETYPE:'S001',
    " +
    132                 "UNITID:'10001',
    " +
    133                 "CNFULLNAME:'测试机构12222',
    " +
    134                 "CNABBRNAME:'测试机构12222',
    " +
    135                 "PINYIN:'CSJG1222',
    " +
    136                 "ENFULLNAME:'CESHIJIGOU',
    " +
    137                 "ENABBRNAME:'CESHIJIGOU'
    " +
    138                 "}";
    139 
    140         JSONObject CissjsonPostParams = JSONObject.fromObject(PostValue);
    141         String postResult = httpInterfaceTest.sendPost(cissUrl, CissjsonPostParams.toString());
    142         System.out.println("POST请求参数二:" + CissjsonPostParams);
    143         System.out.println( postResult);
    144     }*/
    145 }
    View Code

    4.TestHttpInterfaceTest ---1 。2 。3 联合测试接口;

      1 package main.test;
      2 
      3 import main.java.DateUtil;
      4 import main.java.ExcelReader;
      5 import main.java.ExcleUtil;
      6 import main.java.HttpInterfaceTest;
      7 import net.sf.json.JSONException;
      8 import net.sf.json.JSONObject;
      9 import org.testng.annotations.BeforeTest;
     10 import org.testng.annotations.DataProvider;
     11 import org.testng.annotations.Test;
     12 
     13 import java.text.SimpleDateFormat;
     14 import java.util.Date;
     15 
     16 /**
     17  * Created by  on 2017/9/5.
     18  */
     19 public class TestHttpInterfaceTest {
     20     public static HttpInterfaceTest ht;
     21     static ExcleUtil excleUtil;
     22     ExcelReader ex;
     23     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     24     ;
     25     @BeforeTest
     26     public void init() {
     27         String ExcelFilePath = "C:\C\TEST1.xlsx";
     28         String sheetName = "Sheet1";
     29         ht = new HttpInterfaceTest();
     30         ex = new ExcelReader(ExcelFilePath, sheetName);
     31         try {
     32             excleUtil.setExcleFile(ExcelFilePath, sheetName);
     33         } catch (Exception e) {
     34             e.printStackTrace();
     35         }
     36     }
     37 
     38     @Test(dataProvider = "dp")
     39     public void testSendPost(Object... obj) throws Exception {
     40         String rowNum = "";
     41         String Url = "";
     42         String effect = "";
     43         String paras = "";
     44         String Contents = "";
     45         String times = "";
     46         for (int i = 0; i < obj.length; i++) {
     47             rowNum = (String) obj[0];
     48             Url = (String) obj[1];
     49             effect = (String) obj[2];
     50             paras = (String) obj[3];
     51 
     52         }
     53         //String rowNum, String Url, String effect, String paras,String Contents ,String times
     54         System.out.println("rowNum=" + rowNum + ";  URL=" + Url + " ;" + effect + "   paras=" + paras + Contents + times);
     55         Integer it = new Integer(rowNum);
     56         int row = it.intValue();
     57         if (paras.contains("&")) {
     58             String s1 = ht.sendPost(Url, paras);
     59             String date = DateUtil.format();
     60             excleUtil.setCellData(row, 3, s1);
     61             excleUtil.setCellData(row, 4, date);
     62             System.out.println(s1);
     63         } else {
     64             try {
     65                /* JSONObject jsonObject = JSONObject.fromObject(paras);
     66                 String s  =  ht.sendPost(Url, jsonObject.toString());*/
     67                 String testTimes =df.format(new Date());
     68                 String s = ht.sendPost(Url, paras);
     69                 JSONObject jsonObject = JSONObject.fromObject(s);
     70                 String MESSAGEID = jsonObject.getString("MESSAGEID");
     71                 String RESULTCODE = jsonObject.getString("RESULTCODE");
     72                 if (RESULTCODE.equals("0")) {
     73                     excleUtil.setCellData(row, 4, RESULTCODE + ":ACK成功");
     74                     excleUtil.setCellData(row, 5, df.format(new Date()));
     75                 } else if (RESULTCODE.equals("E000")) {
     76                     excleUtil.setCellData(row, 4, RESULTCODE + "其他");
     77                     excleUtil.setCellData(row, 5, df.format(new Date()));
     78                 } else if (RESULTCODE.equals("E001")) {
     79                     excleUtil.setCellData(row, 4, RESULTCODE + "必填项数据为空值");
     80                     excleUtil.setCellData(row, 5, df.format(new Date()));
     81                 } else if (RESULTCODE.equals("E002")) {
     82                     excleUtil.setCellData(row, 4, RESULTCODE + "必填项在数据库中没有匹配数据");
     83                     excleUtil.setCellData(row, 5, df.format(new Date()));
     84                 } else if (RESULTCODE.equals("E003")) {
     85                     excleUtil.setCellData(row, 4, RESULTCODE + "唯一项在数据库中重复");
     86                     excleUtil.setCellData(row, 5, df.format(new Date()));
     87                 } else if (RESULTCODE.equals("E004")) {
     88                     excleUtil.setCellData(row, 4, RESULTCODE + "传过来的关键字段不同属于一条记录;");
     89                     excleUtil.setCellData(row, 5, df.format(new Date()));
     90                 } else if (RESULTCODE.equals("E005")) {
     91                     excleUtil.setCellData(row, 4, RESULTCODE + "没有满足前提条件。如:当前日期范围内还没有维护汇率信息,请先行维护!");
     92                     excleUtil.setCellData(row, 5, df.format(new Date()));
     93                 } else if (RESULTCODE.equals("E006")) {
     94                     excleUtil.setCellData(row, 4, RESULTCODE + "类型转化异常,");
     95                     excleUtil.setCellData(row, 5, df.format(new Date()));
     96                 } else if (RESULTCODE.equals("E007")) {
     97                     excleUtil.setCellData(row, 4, RESULTCODE + " 所填数据不在给定范围内 ");
     98                     excleUtil.setCellData(row, 5, df.format(new Date()));
     99                 } else if (RESULTCODE.equals("E008")) {
    100                     excleUtil.setCellData(row, 4, RESULTCODE + " 数据格式不正确 ");
    101                     excleUtil.setCellData(row, 5, df.format(new Date()));
    102                 } else if (RESULTCODE.equals("E009")) {
    103                     excleUtil.setCellData(row, 4, RESULTCODE + "在当前数据状态下不能进行操作");
    104                     excleUtil.setCellData(row, 5, df.format(new Date()));
    105                 } else if (RESULTCODE.equals("E010")) {
    106                     excleUtil.setCellData(row, 4, RESULTCODE + " 该经纪人已在清算系统中存在,状态:正常 ");
    107                     excleUtil.setCellData(row, 5, df.format(new Date()));
    108                 } else if (RESULTCODE.equals("E011")) {
    109                     excleUtil.setCellData(row, 4, RESULTCODE + " 该经纪人已在清算系统中存在,状态:禁用");
    110                     excleUtil.setCellData(row, 5, df.format(new Date()));
    111                 }
    112 
    113                 System.out.println(s);
    114             } catch (JSONException jsonException) {
    115                 excleUtil.setCellData(row, 4, " 响应结果 ");
    116                 excleUtil.setCellData(row, 5, " 响应时间 ");
    117                 System.out.println("标题行不能进行转换!");
    118             } catch (Exception e) {
    119                 excleUtil.setCellData(row, 4, " Exception  ");
    120                 excleUtil.setCellData(row, 5, "  Exception");
    121                 e.printStackTrace();
    122                 System.out.println("标题行不能进行转换!");
    123             }
    124 
    125         }
    126 
    127 
    128     }
    129 
    130     @DataProvider
    131     public Object[][] dp() {
    132         Object[][] sheetData2 = ex.getSheetData2();
    133         System.out.println(sheetData2.length + "------------");
    134         for (int i = 1; i < sheetData2.length; i++) {
    135             for (int j = 0; j < sheetData2[i].length - 1; j++) {
    136                 System.out.print(sheetData2[i][j] + " | ");
    137             }
    138             System.out.println();
    139         }
    140 
    141         return sheetData2;
    142     }
    143 }
    View Code
  • 相关阅读:
    Windows如何安装苹果系统
    python生成.h和.m文件
    python脚本为已创建的ios .h和.m文件添加费代码
    (转)xcode报错:Undefined symbols for architecture x86_64
    (转)iOS内购(iap)总结
    (转)iOS的静态库和动态库(.a、.framework)、Undefined symbols for architecture x86_64、Invalid bitcode signature错误的...
    (转)iOS开发之内购-AppStore
    (转)Cocos 2d-X Lua 游戏添加苹果内购(二) OC和Lua交互代码详解
    关于git服务器的搭建
    关于计算机(概念漫谈)
  • 原文地址:https://www.cnblogs.com/linbo3168/p/7607673.html
Copyright © 2011-2022 走看看