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

  

Java的get请求-----接口测试的更多相关文章

  1. Java的post请求-----接口测试

    本次主要是对登陆的接口测试post请求,希望记录在博客里面,一点一点的成长. package com.ju.Login; import java.io.BufferedReader; import j ...

  2. java自动化测试-http请求结合抓包工具实际应用

    继上文我编写了java的get请求与post请求之后,我现在开始写一下实际操作 很多人有疑问,接口测试的代码是哪里来的,怎么来的呢?看得见吗?我来做一个简单的演示 我们这里简单介绍一下抓包工具,对于一 ...

  3. Java发送Http请求并获取状态码

    通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...

  4. java 实现https请求

    java 实现https请求 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从 ...

  5. 第三篇 :微信公众平台开发实战Java版之请求消息,响应消息以及事件消息类的封装

    微信服务器和第三方服务器之间究竟是通过什么方式进行对话的? 下面,我们先看下图: 其实我们可以简单的理解: (1)首先,用户向微信服务器发送消息: (2)微信服务器接收到用户的消息处理之后,通过开发者 ...

  6. 通过java发送http请求

    通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...

  7. 深入浅出Java 重定向和请求转发的区别

    深入浅出Java 重定向和请求转发的区别 <span style="font-family:FangSong_GB2312;font-size:18px;">impor ...

  8. 上curl java 模拟http请求

    最近,我的项目要求java模拟http请求,获得dns解决 tcp处理过的信息特定的连接. java api提供urlConnection apache提供的httpClient都不能胜任该需求,二次 ...

  9. 使用SoapUI工具做get请求和post请求接口测试

    祝大家节日快乐啦. 之前写过的一篇帖子已经介绍了SoapUI工具的基本使用,所以在此不再重复讲解关于建工程.建测试套件.添加用例等操作,可查看该篇文章详解:http://www.cnblogs.com ...

随机推荐

  1. NOIp2018集训test-10-24(am&pm)

    李巨连续AK三场了,我跟南瓜打赌李巨连续AK七场,南瓜赌李巨连续AK五场. DAY1 T1 qu 按题意拿stack,queue和priority_que模拟即可.特判没有元素却要取出的情况. T2 ...

  2. java 实现插入排序

    sorted数组第0个位置没有放数据 从sorted数组的第二个数据开始处理 package com.learn; public class InsertionSort { public static ...

  3. Visualforce入门第六篇_2017.3.1

    Visualforce实现过滤.数据列表显示.分页功能 可以参考salesforce官网开发文档:https://trailhead.salesforce.com/modules/visualforc ...

  4. Azure CLI脚本查看未挂载的ManagedDisk

    本文介绍如何用Azure CLI的脚本查看未挂载的Managed Disk,以及Managed Disk挂载到哪些资源. 具体的脚本如下: #!/bin/bash rm -rf noownerdisk ...

  5. Python使用类

    #coding:utf8 from selenium import webdriverfrom time import sleep class urlpage(object): #创建浏览器对象 de ...

  6. mycat sequence

    数据库方式原理在数据库中建立一张表,存放sequence名称(name),sequence当前值(current_value),步长(increment int类型每次读取多少个sequence,假设 ...

  7. linux uid/euid/suid

    Each UNIX process has 3 UIDs associated to it. Superuser/root is UID=0. UID Read UID. It is of the u ...

  8. DDD学习笔录——简介DDD的战略模式如何塑造应用程序的架构

    前一篇,简单介绍了DDD战略模式的提炼问题域,这篇简单介绍它如何塑造应用程序的架构. 1.创建一个模型以解决领域问题 为每一个子域构建一个软件模型以处理领域问题并让软件与业务保持一致. 这个模型并非现 ...

  9. leetcode860

    使用C++进行编码: bool lemonadeChange(vector<int>& bills) { ; ; ; int N = bills.size(); ; i < ...

  10. 用命令查看端口占用情况 netstat -ano

    查看所有端口 netstat -ano 可以看到进程ID 参考某个具体端口,第五列就是PID进程ID了. netstat -aon|findstr "80"