不打开登陆界面执行Web自动化测试

自动化测试:不打开登陆界面操作

  • 背景
  • 获取POST参数
    • 代码

背景

在自动化测试时,经常会遇到不打开登陆界面直接执行某个url操作的需求,而该URL在浏览器中执行的时候,是不允许匿名执行的。在不使用selenium模拟用户打开登陆界面的前提下,使用HttpURLConnection可以不用依赖其他非java内置包。

获取POST参数

打开浏览器, F12 打开Network,查看Request和response数据

代码

package testing;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class HttpUrlConnDemo {
	
	private String cookieStr = "";
	
	public String getCookieStr() {
		return cookieStr;
	}

	public void setCookieStr(String cookieStr) {
		this.cookieStr = cookieStr;
	}

	public void getCookieFromResponse(URL url, String params) throws IOException{
		String returnstr = "";
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.addRequestProperty("Connection", "keep-alive");
		conn.addRequestProperty("Content-Type", "application/json;charset=UTF-8");
		conn.setRequestMethod("POST");
		DataOutputStream out = new DataOutputStream(conn.getOutputStream());
		out.write(params.getBytes());
		out.flush();
		if(conn.getResponseCode() == 200) {
			Map<String,List<String>> headers = conn.getHeaderFields();
			for(Entry<String, List<String>> entry: headers.entrySet()) {
				String key = entry.getKey();
				if ("Set-Cookie".equals(key)){
					List<String> v = entry.getValue();					
					for (String str : v) {
						returnstr +=str;
					}					
					break;
				}
			}
		}
		setCookieStr(returnstr);
		out.close();
		conn.disconnect();
	}
	
	public void requestWithCookie(URL url, String cookiestr) throws IOException{
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.addRequestProperty("Connection", "keep-alive");
		conn.addRequestProperty("Content-Type", "application/json;charset=UTF-8");
		conn.addRequestProperty("X-Powered-By","SNMW-WEB1.0");
		conn.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");
		conn.addRequestProperty("Cookie", cookiestr);
		conn.setRequestMethod("GET");
		
		BufferedReader in = new BufferedReader(
				  new InputStreamReader(conn.getInputStream()));
		
		String inputLine;
		StringBuffer content = new StringBuffer();
		while ((inputLine = in.readLine()) != null) {
		    content.append(inputLine);
		    System.out.print(inputLine);
		}
		in.close();
	}
	
	
	public String getParam(Map<String,String> param){
        StringBuilder str=new StringBuilder();
        str.append("{\"");
        int size=0;
        for (Map.Entry<String, String> m :param.entrySet())  {
            str.append(m.getKey());
            str.append("\":\"");
            str.append(m.getValue());
            str.append("\"");
            if(size<param.size()-1){
                str.append(",\"");
            }
            size++;
        }
        str.append("}");
        System.out.println(str.toString());
        return str.toString();
    }

	public static void main(String[] args) throws IOException {
		URL url = new URL("http://XXXXXXXX.com/XXXXX/login");
		HttpUrlConnDemo demo = new HttpUrlConnDemo();
		Map<String, String> m = new HashMap<String, String>();
		m.put("loginName", "xxx");
		m.put("password", "xxx");
		demo.getCookieFromResponse(url, demo.getParam(m));
		System.out.println(demo.getCookieStr());
		
		URL newurl = new URL("http://XXXXXXXX.com/XXXXX/xxxx?dateStr=20190508&type=1");
		demo.requestWithCookie(newurl, demo.getCookieStr());	
	}
}

你可能感兴趣的:(自动化测试)