(JAVA+TESTNG 三)Eclipse+TestNG搭建接口自动化测试框架

上一篇 / 下一篇  2017-09-04 17:24:08

转载http://www.cnblogs.com/findyou/p/5388853.html


1、接口说明

例:北京市天气

接口的址:http://www.weather.com.cn/data/cityinfo/101010100.html

  1. 请求方式:GET
  2. 请求结果:
[java] view plain copy
  1. {  
  2.     "weatherinfo": {  
  3.         "city""北京",  
  4.         "cityid""101010100",  
  5.         "temp1""15℃",  
  6.         "temp2""5℃",  
  7.         "weather""多云",  
  8.         "img1""d1.gif",  
  9.         "img2""n1.gif",  
  10.         "ptime""08:00"  
  11.     }  
  12. }  
问题:通过浏览器用get方法访问接口地址,请求结果中文显示乱码。

解决方法:firefox,查看-文字编码-Unicode

2、测试目的

请求对应cityid代码,返回的城市是否是预期城市

3、Java工程

3.1.2 新建JAVA工程

步骤同上一篇2.2.2.1新建JAVA工程,不再复述,如不懂请百度Eclipse新建工程

 

1.工程结构说明

 

 

2.Common.java源码

[java] view plain copy
  1. package findyou.Interface;  
  2. import org.codehaus.jettison.json.JSONException;  
  3. import org.codehaus.jettison.json.JSONObject;  
  4.   
  5. public class Common {  
  6.     /** 
  7.      * 解析Json内容 
  8.      *  
  9.      * @author Findyou 
  10.      * @version 1.0 2015/3/23 
  11.      * @return JsonValue 返回JsonString中JsonId对应的Value 
  12.      **/  
  13.   
  14.     public static String getJsonValue(String JsonString, String JsonId) {  
  15.         String JsonValue = "";  
  16.         if (JsonString == null || JsonString.trim().length() < 1) {  
  17.             return null;  
  18.         }  
  19.   
  20.         try {  
  21.             JSONObject obj1 = new JSONObject(JsonString);  
  22.             JsonValue = (String) obj1.getString(JsonId);  
  23.         } catch (JSONException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return JsonValue;  
  27.     }  
  28. }  

JSONObject.getString()// 根据key返回一个字符串

JSONObject.getInt()// 根据key返回一个整形数据

trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了

Android中的JSON详细总结 http://www.jb51.NET/article/33414.htm

JSONObject与JSONArray的使用http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html

JSON for java入门总结 http://blog.csdn.net/xiazdong/article/details/7059573

3.getCityWeathe.java源码

[java] view plain copy
  1. package findyou.Interface;  
  2. import java.io.BufferedReader;  
  3. import java.io.DataOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.net.HttpURLConnection;  
  7. public class getCityWeather {  
  8.     private String url="";      
  9.     public String geturl() {  
  10.         return url;  
  11.     }  
  12.   
  13.     public String getHttpRespone(String cityCode) throws IOException {  
  14.         String line = "";  
  15.         String httpResults = "";  
  16.         url=("http://www.weather.com.cn/data/cityinfo/"  
  17.                 + cityCode + ".html");  
  18.         try {  
  19.             HttpURLConnection connection = URLConnection  
  20.                     .getConnection(url);  
  21.             DataOutputStream out = null;  
  22.             // 建立实际的连接  
  23.             connection.connect();  
  24.             out = new DataOutputStream(connection.getOutputStream());//创建一个新的数据输出流,将数据写入指定基础输出流。  
  25.             out.flush();//清空此数据输出流,<span style="color:#0800;margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; line-height:1.5;">刷新对象输出流,将任何字节都写入潜在的流中</span>out  
  26.             out.close();//  
  27.             BufferedReader reader = new BufferedReader(new InputStreamReader(  
  28.                     connection.getInputStream()));  
  29.             while ((line = reader.readLine()) != null) {  
  30.                httpResults = httpResults + line.toString();  
  31.             }  
  32.             reader.close();//关闭该流并释放与之关联的所有资源  
  33.             // 断开连接  
  34.             connection.disconnect();  
  35.         } 

TAG:

 

评分:0

我来说两句

Open Toolbar