zoukankan      html  css  js  c++  java
  • Java的get请求-----接口测试

    package findyou.Interface;
    
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class URLConnection {	
    	public static HttpURLConnection getConnection(String url){
    		HttpURLConnection connection = null;
    		try {
    			// 打开和URL之间的连接
    			URL postUrl = new URL(url);
    			connection = (HttpURLConnection) postUrl.openConnection();
    			 // 设置通用的请求属性
    			connection.setDoOutput(false);//posp请求改为TRUE
    			connection.setDoInput(true);
    			connection.setRequestMethod("GET");
    			connection.setUseCaches(false);
    			connection.setInstanceFollowRedirects(true);
    			connection.setRequestProperty("Content-Type", "application/json");
    			connection.setRequestProperty("Charset", "utf-8");
    			connection.setRequestProperty("Accept-Charset", "utf-8");
    		} catch (Exception e) {
    			e.printStackTrace();
    		} 
    		return connection;
    	}
    }
    

      

    package findyou.Interface;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    public class getCityWeather {
    
         private static String url="";
    
         
    
         public String geturl() {
    
             return url;
    
         }
    
    
    
      public String getHttpRespone(String cityCode) throws IOException {
    
             String line = "";
    
             String httpResults = "";
    
             url=("http://www.weather.com.cn/data/cityinfo/"
    
                     + cityCode + ".html");
    
             try {
     			//url=("http://www.weather.com.cn/data/cityinfo/"+ cityCode + ".html");
     			HttpURLConnection urlcon = URLConnection.getConnection(url);      
     			urlcon.setReadTimeout(5000);
     			urlcon.setConnectTimeout(5000);
     			urlcon.connect();
     			BufferedReader reader=new BufferedReader(new InputStreamReader(urlcon.getInputStream(),"utf-8"));
     			 //line=bf.readLine();
     			 while ((line = reader.readLine()) != null) {
                     httpResults = httpResults + line.toString();
                 }
     			
     		} catch (MalformedURLException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();
     		}
             return httpResults;
    
         }
    
        
    }
    

      

    package findyou.Interface;
    
    import org.codehaus.jettison.json.JSONException;
    import org.codehaus.jettison.json.JSONObject;
    
    public class Common {
    	/**
    	 * 解析Json内容
    	 * 
    	 * @author Findyou
    	 * @version 1.0 2016/3/23
    	 * @return JsonValue 返回JsonString中JsonId对应的Value
    	 **/
    	public static String getJsonValue(String JsonString, String JsonId) {
    		String JsonValue = "";
    		if (JsonString == null || JsonString.trim().length() < 1) {
    			return null;
    		}
    		try {
    			JSONObject obj1 = new JSONObject(JsonString);
    			JsonValue = (String) obj1.getString(JsonId);
    		} catch (JSONException e) {
    			e.printStackTrace();
    		}
    		return JsonValue;
    	}
    }
    

      下面的代码是用Java写的测试用例

    package findyou.testcase;
    
    import java.io.IOException;
    
    import org.testng.Assert;
    import org.testng.Reporter;
    import org.testng.annotations.Test;
    
    import findyou.Interface.Common;
    import findyou.Interface.getCityWeather;
    
    public class test {
    	public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
    	public static String cityCode="";	
    	getCityWeather weather=new getCityWeather();
    	
    	@Test(groups = { "BaseCase"})
    	public void getShenZhen_Succ() throws IOException{
    		exp_city="深圳";
    		cityCode="101280601";
    		Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getBeiJing_Succ() throws IOException{
    		exp_city="北京";
    		cityCode="101010100";
    		Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getShangHai_Succ() throws IOException{
    		exp_city="上海";
    		cityCode="101020100";
    		Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city);
    	}	
    }
    

      

    package findyou.testcase;
    
    import java.io.IOException;
    
    import org.testng.Assert;
    import org.testng.Reporter;
    import org.testng.annotations.Test;
    
    import findyou.Interface.Common;
    import findyou.Interface.getCityWeather;
    
    public class test1 {
    	public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
    	public static String cityCode="";
    	getCityWeather weather=new getCityWeather();
    	
    	
    	@Test(groups = { "BaseCase"})
    	public void getShenZhen_Succ() throws IOException{
    		exp_city="深圳";
    		cityCode="101280601";
    		resultCheck(cityCode, exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getBeiJing_Succ() throws IOException{
    		exp_city="北京";
    		cityCode="101010100";
    		resultCheck(cityCode, exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getShangHai_Succ() throws IOException{
    		exp_city="上海";
    		cityCode="101020100";
    		resultCheck(cityCode, exp_city);
    	}
    	
    	public void resultCheck(String cityCode_str, String exp_city_str) throws IOException{
    		Reporter.log("【正常用例】:获取"+exp_city_str+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode_str);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: City=>expected: " + exp_city_str + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city_str);		
    	}
    }
    

      

  • 相关阅读:
    切图常用快捷键
    统计网页访问量的代码
    jQuery $.each用法
    js数组去重的三种常用方法
    React中的context的用法和使用场景和发布-订阅模式
    工具函数
    前端安全
    面试题整理
    学习的一些链接
    工厂模式、构造函数模式、原型模式、构造函数模式+原型模式
  • 原文地址:https://www.cnblogs.com/wangxiaoqun/p/6721529.html
Copyright © 2011-2022 走看看