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);
}
}
Java的get请求-----接口测试的更多相关文章
- Java的post请求-----接口测试
本次主要是对登陆的接口测试post请求,希望记录在博客里面,一点一点的成长. package com.ju.Login; import java.io.BufferedReader; import j ...
- java自动化测试-http请求结合抓包工具实际应用
继上文我编写了java的get请求与post请求之后,我现在开始写一下实际操作 很多人有疑问,接口测试的代码是哪里来的,怎么来的呢?看得见吗?我来做一个简单的演示 我们这里简单介绍一下抓包工具,对于一 ...
- Java发送Http请求并获取状态码
通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...
- java 实现https请求
java 实现https请求 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从 ...
- 第三篇 :微信公众平台开发实战Java版之请求消息,响应消息以及事件消息类的封装
微信服务器和第三方服务器之间究竟是通过什么方式进行对话的? 下面,我们先看下图: 其实我们可以简单的理解: (1)首先,用户向微信服务器发送消息: (2)微信服务器接收到用户的消息处理之后,通过开发者 ...
- 通过java发送http请求
通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...
- 深入浅出Java 重定向和请求转发的区别
深入浅出Java 重定向和请求转发的区别 <span style="font-family:FangSong_GB2312;font-size:18px;">impor ...
- 上curl java 模拟http请求
最近,我的项目要求java模拟http请求,获得dns解决 tcp处理过的信息特定的连接. java api提供urlConnection apache提供的httpClient都不能胜任该需求,二次 ...
- 使用SoapUI工具做get请求和post请求接口测试
祝大家节日快乐啦. 之前写过的一篇帖子已经介绍了SoapUI工具的基本使用,所以在此不再重复讲解关于建工程.建测试套件.添加用例等操作,可查看该篇文章详解:http://www.cnblogs.com ...
随机推荐
- C#进阶之路(六):表达式进行类的赋值
好久没更新这个系列了,最近看.NET CORE源码的时候,发现他的依赖注入模块的很多地方用了表达式拼接实现的.比如如下代码 private Expression<Func<ServiceP ...
- LeetCode Student Attendance Record I
原题链接在这里:https://leetcode.com/problems/student-attendance-record-i/description/ 题目: You are given a s ...
- 数字排列(n,m)(搜索与回溯)
题目描述: 设有n个整数的集合{1,2,…,n},从中取出任意r个数进行排列(r<n),试列出所有的排列. 代码如下: #include<iostream>#include<c ...
- 类和对象(12)—— this指针
首先,我们都知道类的成员函数可以访问类的数据(限定符只是限定于类外的一些操作,类内的一切对于成员函数来说都是透明的),那么成员函数如何知道哪个对象的数据成员要被操作呢,原因在于每个对象都拥有一个指针: ...
- vue 链接
https://www.jianshu.com/p/5d9b341d650f vue-cli入门(三)——人员管理实例 ps:如何创建vue-cli项目 https://www.v2ex.com/t/ ...
- 难道调用ThreadPool.QueueUserWorkItem()的时候,真是必须调用Thread.Sleep(N)吗?
开门见山,下面的例子中通过调用ThreadPool.QueueUserWorkItem(WaitCallback callBack, object state)的方式实现异步调用: 1: class ...
- git公钥生成以及与coding等联合
更好用的 https://segmentfault.com/a/1190000002645623 其中 $ ssh -T git@github.comgitbub $ ssh -T git@git.c ...
- Angular5学习笔记 - 集成Bootstrap、Jquery、Tether(三)
一.添加配置 cnpm i bootstrap jquery tether --save 添加后效果 二.配置添加样式和js的引用 打开.angular-cli.json文件,在styles和scri ...
- springboot或者jetty等启动服务器后,如何去停止这个服务
首先在win7下找到运行,但是win7的运行不像XP那么好找,win7运行的位置在:开始→所有程序→附件→运行. 然后在对话框中,输入cmd(大小写均可). 然后是如何查看80端口的方法,一般 ...
- 修改crushmap实验
标签(空格分隔): ceph,ceph实验,crushmap CRUSH的全称是Controlled Replication Under Scalable Hashing,是ceph数据存储的分布式选 ...