本次主要是對登陸的接口測試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

