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);		
    	}
    }
    

      

  • 相关阅读:
    南京师范大学2021年高等代数考研试卷
    有限阶全图边图两种颜色后同色三角形数量最少为?(2019年清华大学丘成桐数学英才班)
    关于三个变元的正整数解(2019年清华大学丘成桐数学英才班)
    [Oracle工程师手记]归档日志产生量太大时的简易分析手段
    [Oracle 工程师手记] 如何查看 FRA 的使用率
    [Oracle数据库工程师手记] Data Guard broker 与 ORA-32701
    [Oracle工程师手记]CRSD 进程与 password 文件
    [oracle 工程师手记]RMAN duplicate 发生ORA-19504、ORA-17502、ORA-15001、ORA-27140 错误的解决过程
    [Oracle工程师手记] 备份恢复双城记(三)
    [Oracle工程师手记] 备份恢复双城记(二)
  • 原文地址:https://www.cnblogs.com/wangxiaoqun/p/6721529.html
Copyright © 2011-2022 走看看