51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1913|回复: 0

[原创] http接口测试—参数化

[复制链接]

该用户从未签到

发表于 2019-1-22 16:15:28 | 显示全部楼层 |阅读模式
1、传入参数对象化
  1. public class Param {
  2.        
  3.         String no;//编号
  4.         String name;//姓名
  5.         String age;//年龄
  6.         String sex;//性别
  7.         String expResu;//期望结果
  8.         String actResu;//实际结果
  9.         String pass;//是否通过
  10.         String desc;//描述
  11.         .......
  12. }
复制代码

2、使用Excel储存数据,表格如下




3、用poi读取和写入数据,这里我们封装一个ExcelUtil类,代码如下
  1. public class ExcelUtil {
  2.        
  3.         //读取Excel中数据
  4.         public static List<Param> read() throws Exception{
  5.                  HSSFWorkbook wb = new HSSFWorkbook();
  6.                  HSSFSheet s = wb.createSheet();
  7.                  HSSFRow row = s.createRow(0);
  8.                  HSSFCell cell = row.createCell((int)0,0);

  9.                  //------------从xls读出数据
  10.                  wb = new HSSFWorkbook(new FileInputStream("D:\\learn\\test.xls"));
  11.                  s = wb.getSheetAt(0);
  12.                  
  13.                  //获得EXCEL行数
  14.                  int rowNums=s.getLastRowNum();
  15.                  //获得Excell列数
  16.                  //int columnNum=r.getPhysicalNumberOfCells();
  17.                  
  18.                  List<Param> params=new ArrayList<Param>();
  19.                  for(int i=1;i<=rowNums;i++){
  20.                          HSSFRow r = s.getRow(i);
  21.                          cell=r.getCell(0);
  22.                          Param param= new Param();
  23.                          param.setNo(r.getCell(0).getStringCellValue());
  24.                          param.setName(r.getCell(1).getStringCellValue());
  25.                          param.setAge(r.getCell(2).getStringCellValue());
  26.                          param.setSex(r.getCell(3).getStringCellValue());
  27.                          param.setExpResu(r.getCell(4).getStringCellValue());
  28. //                         System.out.println(cell.getRichStringCellValue());
  29.                          params.add(param);
  30.                  }
  31.                  return params;

  32.         }

  33.         /**
  34.           * 写入Excel,在任意坐标处写入数据。
  35.           * String value:你要输入的内容
  36.           * int x :行坐标,Excel从 0 算起
  37.           * int y   :列坐标,Excel从 0 算起
  38.           */
  39.                 public static void writeCell(String filePath,int x,int y,String value) {
  40.                         try {
  41.                                 // 创建Excel的工作书册 Workbook,对应到一个excel文档
  42.                                 HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
  43.                                 HSSFSheet sheet=wb.getSheetAt(0);
  44.                                 HSSFRow row=sheet.getRow(x);
  45.                                 HSSFCell cell=row.getCell((short) y);
  46.                                 cell.setCellValue(value);
  47.                                 FileOutputStream os;
  48.                                 os = new FileOutputStream(filePath);
  49.                                 wb.write(os);
  50.                                 os.close();
  51.                         } catch (Exception e) {
  52.                                 e.printStackTrace();
  53.                         }

  54.                 }
  55. }
复制代码

4、客户端代码如下:

  1. public class TestClient {
  2.        
  3.         public static void main(String[]agrs){
  4.                 TestClient a=new TestClient();
  5.                 try {
  6.                         a.client();
  7.                 } catch (Exception e) {
  8.                         // TODO Auto-generated catch block
  9.                         e.printStackTrace();
  10.                 }
  11.         }
  12.         public void client() throws Exception{
  13.                
  14.                 List<Param> params = ExcelUtil.read();
  15.                 for(Param pa:params){
  16.                         try {
  17.                                 // 接报文的地址
  18.                                 String param= new JsonsUtil().BuildJson(pa);
  19.                                 URL serverUrl= new URL("http://localhost:8090/lctest/TestServer");                       
  20.                                 URLConnection uct= serverUrl.openConnection();
  21.                                 HttpURLConnection hutc=(HttpURLConnection)uct;                               
  22.                                 // 设置报文参数
  23.                                 hutc.setRequestMethod("POST");
  24.                                
  25.                                 // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true, 默认情况下是false;
  26.                                 hutc.setDoOutput(true);
  27.                                
  28.                                 // 设置是否从httpUrlConnection读入,默认情况下是true
  29.                                 hutc.setDoInput(true);       
  30. //                                hutc.setAllowUserInteraction(true);
  31.                                
  32.                                 // 开启流,写入数据data
  33.                                 OutputStream out=hutc.getOutputStream();
  34.                                
  35.                                 out.write(param.getBytes("UTF-8"));
  36.                                 out.flush();
  37.                                 out.close();
  38.                                
  39.                                 // 获取返回的数据       
  40.                                 StringBuffer buffer=new StringBuffer();
  41.                                 BufferedReader reader = null;
  42.                                 InputStream ins=hutc.getInputStream();
  43.                                 reader = new BufferedReader(new InputStreamReader(ins,"UTF-8"));
  44.                                 String sg=reader.readLine();
  45.                                 if (sg!= null){
  46.                                    buffer.append(sg);
  47.                              }
  48.                                 System.out.println("接收返回值:" + buffer);
  49.        
  50.                         } catch (Exception e) {
  51.                                 // TODO Auto-generated catch block
  52.                                 e.printStackTrace();
  53.                         }
  54.                 }
  55.                
  56.        
  57.         }
  58. }
复制代码

5、服务端代码如下:

  1. public class TestServer extends HttpServlet {
  2.         private static final long serialVersionUID = 1L;
  3.          private static JSONArray ja;
  4.       
  5.     /**
  6.      * @see HttpServlet#HttpServlet()
  7.      */
  8.     public TestServer() {
  9.         super();
  10.         // TODO Auto-generated constructor stub
  11.     }

  12.         /**
  13.          * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  14.          */
  15.         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.                 // TODO Auto-generated method stub
  17.                 try {
  18.                         this.excute(request, response);
  19.                 } catch (Exception e) {
  20.                         // TODO Auto-generated catch block
  21.                         e.printStackTrace();
  22.                 }
  23.         }

  24.         /**
  25.          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  26.          */
  27.         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28.                 // TODO Auto-generated method stub
  29.                 try {
  30.                         this.excute(request, response);
  31.                 } catch (Exception e) {
  32.                         // TODO Auto-generated catch block
  33.                         e.printStackTrace();
  34.                 }
  35.         }
  36.        
  37.         public void excute(HttpServletRequest request,HttpServletResponse response) throws Exception{
  38.                 request.setCharacterEncoding("utf-8");
  39.                 response.setCharacterEncoding("UTF-8");
  40.                 response.setContentType("text/xml");
  41.                 String method=request.getMethod();
  42.                 String url=request.getRequestURI();
  43.                 String param;
  44.                // 获取收到的报文
  45.         BufferedReader reader = request.getReader();
  46.         String line = "";
  47.         line = reader.readLine();
  48.         ja=new JsonsTest().ParseJson(line);               
  49.                 StringBuffer resultBuffer=new StringBuffer();
  50.                 resultBuffer.append("访问方式"+method+"访问成功");
  51.                 resultBuffer.append("接收到的数据:"+line+"name:     "+ja.getJSONObject(0).getString("name"));
  52.                 PrintWriter out =response.getWriter();
  53.                 out.println(resultBuffer.toString());
  54.                 out.flush();
  55.                 out.close();
  56.                
  57.         }
  58. }
复制代码

6、客户端调取服务端,运行结果如下:




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-29 09:29 , Processed in 0.073117 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表