preface

This article mainly explains the interface automation test of National Weather Service (Get request and result assertion) with examples. In order to achieve the purpose of automatic test introduction, in addition to the understanding of the first two articles, some JAVA knowledge (HTTP related) is required.

1 Description of the interface to be tested

1. National Weather Service Weather forecast interface

E.g. Weather in Beijing \

 

  1. The address of the interface: www.weather.com.cn/data/cityin…
  2. Request mode: GET
  3. Request result:

 

{” weatherinfo “: {” city” : “Beijing”, “cityid” : “101010100”, “temp1” : “15 ℃”, “temp2” : “5 ℃”, “weather” : “cloudy”, “img1” : “d1.gif”, “img2”: “n1.gif”, “ptime”: “08:00” } }

 

 

 

2. Test objectives

Request the corresponding cityID code and whether the returned city is the expected city.

This is my years of experience to sum up the need to send me a private message.  

2 Create a JAVA project

\

1. Engineering structure description

 

 

2. com mon. Java source code

 

package findyou.Interface; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; Public class Common {/** * Parse Json content ** @author Findyou * @version 1.0 2015/3/23 * @return JsonValue 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; }}

 

 

 

 

3. GetCityWeathe. Java source code

 

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 = (” www.weather.com.cn/data/cityin… + cityCode + “.html”); try { HttpURLConnection connection = URLConnection .getConnection(url); DataOutputStream out = null; // Set up the actual connection connection.connect(); out = new DataOutputStream(connection.getOutputStream()); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); while ((line = reader.readLine()) ! = null) { httpResults = httpResults + line.toString(); } reader.close(); // Disconnect the connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return httpResults; }}

 

 

 

4. URLConnection. Java source code

 

package findyou.Interface; import java.net.HttpURLConnection; import java.net.URL; public class URLConnection { public static HttpURLConnection getConnection(String url){ HttpURLConnection connection = null; Try {// Open the connection to the URL URL postUrl = new URL(URL); connection = (HttpURLConnection) postUrl.openConnection(); // Set the generic request property 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. Write test cases

1. Test cases (common “two”)

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=””; public static getCityWeather weather=new getCityWeather(); @test (groups = {“BaseCase”}) public void getShenZhen_Succ() throws IOException{exp_city=” sz “; cityCode=”101280601″; Reporter. Log (” [normal use case] : Get “+exp_city+” weather success!” ); httpResult=weather.getHttpRespone(cityCode); Reporter.log(” request address: “+weather.geturl()); Reporter. Log (” Return result: “+httpResult); weatherinfo=Common.getJsonValue(httpResult, “weatherinfo”); city=Common.getJsonValue(weatherinfo, “city”); Reporter. Log (” use case result: resultCode=>expected: “+ exp_city + “,actual: “+ city); Assert.assertEquals(city,exp_city); } @test (groups = {“BaseCase”}) public void getBeiJing_Succ() throws IOException{exp_city=” Beijing “; cityCode=”101010100″; Reporter. Log (” [normal use case] : Get “+exp_city+” weather success!” ); httpResult=weather.getHttpRespone(cityCode); Reporter.log(” request address: “+weather.geturl()); Reporter. Log (” Return result: “+httpResult); weatherinfo=Common.getJsonValue(httpResult, “weatherinfo”); city=Common.getJsonValue(weatherinfo, “city”); Reporter. Log (” use case result: resultCode=>expected: “+ exp_city + “,actual: “+ city); Assert.assertEquals(city,exp_city); } @test (groups = {“BaseCase”}) public void getShangHai_Succ() throws IOException{exp_city=” Shanghai “; cityCode=”101020100″; Reporter. Log (” [normal use case] : Get “+exp_city+” weather success!” ); httpResult=weather.getHttpRespone(cityCode); Reporter.log(” request address: “+weather.geturl()); Reporter. Log (” Return result: “+httpResult); weatherinfo=Common.getJsonValue(httpResult, “weatherinfo”); city=Common.getJsonValue(weatherinfo, “city”); Reporter. Log (” use case result: resultCode=>expected: “+ exp_city + “,actual: “+ city); Assert.assertEquals(city,exp_city); }}

 

 

2. Simplified use cases

 

How to return value format and request format fixed, use case optimization as follows

 

 

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=” sz “; cityCode=”101280601”; resultCheck(cityCode, exp_city); } @test (groups = {“BaseCase”}) public void getBeiJing_Succ() throws IOException{exp_city=” Beijing “; cityCode=”101010100”; resultCheck(cityCode, exp_city); } @test (groups = {“BaseCase”}) public void getShangHai_Succ() throws IOException{exp_city=” Shanghai “; cityCode=”101020100″; resultCheck(cityCode, exp_city); } public void resultCheck(String cityCode_str, String exp_city_str) throws IOException{reporter.log (” [normal use case] : Obtain “+exp_city_str+” Weather success!” ); httpResult=weather.getHttpRespone(cityCode_str); Reporter.log(” request address: “+weather.geturl()); Reporter. Log (” Return result: “+httpResult); weatherinfo=Common.getJsonValue(httpResult, “weatherinfo”); city=Common.getJsonValue(weatherinfo, “city”); Reporter. Log (” Use case result: resultCode=>expected: “+ exp_city_str + “,actual: “+ city); Assert.assertEquals(city,exp_city_str); }}

 

  

 

4 Execute test cases

 

 

 

 

  

TestNG automated test series examples, basically completed, Post method due to space problems, will not be posted, understand the above instance, Post method is not too big problem. Follow-up, if you have time to see mood on continuous integration, automation deployment, automation use case execution and test report output blog ~~~