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

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


1、接口说明

例:北京市天气

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

  1. 请求方式:GET
  2. 请求结果:
{
    "weatherinfo": {
        "city": "北京",
        "cityid": "101010100",
        "temp1": "15℃",
        "temp2": "5℃",
        "weather": "多云",
        "img1": "d1.gif",
        "img2": "n1.gif",
        "ptime": "08:00"
    }
}
问题:通过浏览器用get方法访问接口地址,请求结果中文显示乱码。

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

2、测试目的

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

3、JAVA工程

3.1.2 新建JAVA工程

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

 

1.工程结构说明

 (JAVA+TESTNG 三)Eclipse+TestNG搭建接口自动化测试框架_第1张图片

 

2.Common.java源码

package findyou.Interface;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class Common {
    /**
     * 解析Json内容
     * 
     * @author Findyou
     * @version 1.0 2015/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;
    }
}

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源码

package findyou.Interface;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
public class getCityWeather {
    private 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 {
            HttpURLConnection connection = URLConnection
                    .getConnection(url);
            DataOutputStream out = null;
            // 建立实际的连接
            connection.connect();
            out = new DataOutputStream(connection.getOutputStream());//创建一个新的数据输出流,将数据写入指定基础输出流。
            out.flush();//清空此数据输出流,刷新对象输出流,将任何字节都写入潜在的流中out
            out.close();//
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            while ((line = reader.readLine()) != null) {
               httpResults = httpResults + line.toString();
            }
            reader.close();//关闭该流并释放与之关联的所有资源
            // 断开连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return httpResults;
    }
}

HttpURLConnection用法详解 http://blog.csdn.net/x1617044578/article/details/8668632

详解HttpURLConnection http://blog.csdn.net/woxueliuyun/article/details/43267365

java.io类 DataOutputStream

DataOutputStream(OutputStream out)

          创建一个新的数据输出流,将数据写入指定基础输出流。

URLConnection类

getOutputStream()
          返回写入到此连接的输出流。

getInputStream()
          返回从此打开的连接读取的输入流。


InputStreamReader 是字节流通向字符流的桥梁

BufferedReader(Reader in)
创建一个使用默认大小输入缓冲区的缓冲字符输入流


readLine()
          读取一个文本行,(读取时自动换行)


4.URLConnection.java源码

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

3.1.3 编写测试用例


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";

        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("用例结果: resultCode=>expected: " + exp_city_str + " ,actual: "+ city);

        Assert.assertEquals(city,exp_city_str);        

    }

}





你可能感兴趣的:((JAVA+TESTNG 三)Eclipse+TestNG搭建接口自动化测试框架)