App Interface Automation test java+testng (iii) HTTP interface Test Example

Source: Internet
Author: User
Tags testng

Reproduced from: http://www.cnblogs.com/findyou/p/5388853.html description, this article for the study reference, will be deleted after a period of time. Objective
After the first two basic knowledge, this article mainly explains the National Weather Control Interface Automation test (GET request and result assertion), in order to achieve the goal of automated testing, in addition to some of the first two of the understanding, need to have a certain Java knowledge (HTTP-related).

Directory

3.1 HTTP interface (GET) test instance
3.1.1 Description of the interface to be tested 3.1.2 new Java Project
1. Project Catalog description 2. Common.java Source code 3.getcityweathe.java source code 4.urlconnection.java
Source code 3.1.3 Write test Case
1. Test Case 2. Simplified use case
3.1.4 Execute test Case

The following examples are written again for this summary, such as reprint also please retain the source and author name findyou, thank you!

3.1.1 Description of the interface to be tested
1. National Weather Forecast Interface Example: Beijing weather

    1. Address of the interface: http://www.weather.com.cn/data/cityinfo/101010100.html
    2. Request Method: 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 target

Request the corresponding Cityid code, whether the returned city is the expected city.

3.1.2 New Java Project
Step above an article 2.2.2.1 new Java project, no longer repeat, if you do not understand Baidu Eclipse new project

1. Engineering Structure Description

2.common.java Source 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 returns the value corresponding to Jsonid in jsonstring
**/
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 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;
Establish an 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 Connection
Connection.disconnect ();
} catch (Exception e) {
E.printstacktrace ();
}
return httpresults;
}
}

4.urlconnection.java Source Package findyou. Interface;
Import java.net.HttpURLConnection;
Import Java.net.URL;
public class URLConnection {
public static httpurlconnection getconnection (String URL) {
HttpURLConnection connection = null;
try {
Opening and linking between URLs
URL posturl = new URL (URL);
Connection = (httpurlconnection) posturl.openconnection ();
To set common request properties
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 Writing test Cases
1. Test cases (common "two" general wording) 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= "Shenzhen";
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 the return value format is fixed with the request format, 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= "Shenzhen";
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": Get "+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);
}
}

Project: http://pan.baidu.com/s/1mhyTNny Password: 11ft

3.1.4 Executing test Cases

App Interface Automation test java+testng (iii) HTTP interface Test Example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.