本次主要是对登陆的接口测试post请求,希望记录在博客里面,一点一点的成长。
package com.ju.Login; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; public class URLConnection { public static String GetResponse(String Info) throws IOException { String path = "http://192.1#.10.42:#/web_shiro_oracle/login.do"; //1, 得到URL对象 URL url = new URL(path); //2, 打开连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //3, 设置提交类型 conn.setRequestMethod("POST"); //4, 设置允许写出数据,默认是不允许 false conn.setDoOutput(true); conn.setDoInput(true);//当前的连接可以从服务器读取内容, 默认是true //5, 获取向服务器写出数据的流 OutputStream os = conn.getOutputStream(); //参数是键值队 , 不以"?"开始 os.write(Info.getBytes()); //os.write("googleTokenKey=&username=admin&password=5df5c29ae86331e1b5b526ad90d767e4".getBytes()); os.flush(); //6, 获取响应的数据 //得到服务器写回的响应数据 BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8")); String str = br.readLine(); System.out.println("响应内容为: " + str); return str; } }
package com.ju.Test; import java.io.IOException; import org.testng.Assert; import org.testng.Reporter; import org.testng.annotations.Test; import com.ju.Login.URLConnection; public class LoginTest { String userName; String Pwd; String googleTokenKey; URLConnection get=new URLConnection(); @Test(groups = { "BaseCase"}) public void Login_succ() throws IOException{ //googleTokenKey=null; //userName="admin"; //Pwd="5df5c29ae86331e1b5b526ad90d767e4"; String Info="googleTokenKey"+"="+""+"&"+"username"+"="+"admin"+"&"+"password"+"="+"5df5c29ae86331e1b5b526ad90d767e4"; Reporter.log(get.GetResponse(Info)); Reporter.log(Info); /*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 username_fail() throws IOException{ //googleTokenKey=null; //userName="admin"; //Pwd="5df5c29ae86331e1b5b526ad90d767e4"; String Info="googleTokenKey"+"="+""+"&"+"username"+"="+"admin1"+"&"+"password"+"="+"5df5c29ae86331e1b5b526ad90d767e4"; Reporter.log(get.GetResponse(Info)); Reporter.log(Info); /*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 pwd_fail() throws IOException{ //googleTokenKey=null; //userName="admin"; //Pwd="5df5c29ae86331e1b5b526ad90d767e4"; String Info="googleTokenKey"+"="+""+"&"+"username"+"="+"admin"+"&"+"password"+"="+"5df5c29ae86331e1b5b526ad90d767e"; Reporter.log(get.GetResponse(Info)); Reporter.log(Info); /*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);*/ } }
然后以TestNG运行loginTest,接着去项目保存的路径下面找到下面箭头标识的文件夹,点击index.html,就可以看到详细的log