zoukankan      html  css  js  c++  java
  • Java的post请求-----接口测试

    本次主要是对登陆的接口测试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

  • 相关阅读:
    Call to undefined function curl_init() 解决方案-PHP
    手机web禁止微信调整字体
    Apache2.4+PHP7+MySQL5.7配置
    pip下载时临时指定源
    form提交表单时,action携带参数(form跳转页面时在url中拼接字符串)
    pyspark 出现pyspark.sql.utils.IllegalArgumentException: 'requirement failed: Column prediction must be of type double but was actually float.'异常
    安装vue
    ubuntu安装ssh和ssh本地免密登录
    Python获取当前目录和上级目录
    购买了云服务器后做的一些事
  • 原文地址:https://www.cnblogs.com/wangxiaoqun/p/6721439.html
Copyright © 2011-2022 走看看