JAVA接口自动化框架2:测试框架的基本实现

本章基本实现java接口自动化框架的基本功能,包括请求,配置,测试类等完整实现

一、parameters包,主要存放请求参数

package com.qa.parameters;

public class Manager {
    private String account;
    private String loginPwd;
    private String partnerCode;
    
    public Manager(){
        
    }
    
    public Manager(String account,String loginPwd,String partnerCode){
        super();
        this.loginPwd=loginPwd;
        this.account=account;
        this.partnerCode=partnerCode;
    }
    
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getLoginPwd() {
        return loginPwd;
    }
    public void setLoginPwd(String loginPwd) {
        this.loginPwd = loginPwd;
    }
    public String getPartnerCode() {
        return partnerCode;
    }
    public void setPartnerCode(String partnerCode) {
        this.partnerCode = partnerCode;
    }    
}

 

二、base包,存放状态常量,测试类的基类

package com.qa.base;
import org.testng.annotations.BeforeClass;
import com.qa.utils.PropertiesUtils;

/**
 * 该类可以当成所有测试类的模板基类,其他需要测试的类继承该类
 * session,token等需要全局使用的均需要在此类中进行定义;若测试需要登录可在本类进行登录
 * @author jff
 * @date 2018年9月25日
 * @version V1.0.1
 */
public abstract class BaseApi {
    protected String hostManager;
    
    @BeforeClass
    public void setUp() {
        host1=PropertiesUtils.getConfigValue("HOST");
        hostManager=PropertiesUtils.getConfigValue("HOSTMANAGER");
    }
}


package com.qa.base;

/**
 *     常量类,相关的常量都统一放在该类中
 * @author Administrator
 *
 */
public class Constants {
    //不要在代码里写死例状态码,用常量写出来,方便每一个TestNG测试用例去调用去断言
    public static final int RESPNSE_STATUS_CODE_200 = 200;
    public static final int RESPNSE_STATUS_CODE_201 = 201;
    public static final int RESPNSE_STATUS_CODE_204 = 204;
    public static final int RESPNSE_STATUS_CODE_404 = 404;
    public static final int RESPNSE_STATUS_CODE_500 = 500;
}

三、restclient包存放各种请求,包括post,delete,get,put等

package com.qa.restclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.qa.utils.fatjson.FastjsonUtils;

/*
 * 1.实现get请求方法及带请求头信息,POST请求方法,PUT方法,DELETE方法
 * 2.一个是带请求头信息的POST请求方法
 * 3.获取响应状态码
 * 4.json内容解析
 */
public class RestClient {
	
	private static final Logger log = LoggerFactory.getLogger(RestClient.class);
	
	// get请求方法
	public static CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
		return get(url, null);
	}

	// Get 请求方法(带请求头信息)
	public static CloseableHttpResponse get(String url, Map<String, String> headers)
			throws ClientProtocolException, IOException {
		// 创建一个可关闭的HttpClient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建一个HttpGet的请求对象
		HttpGet httpget = new HttpGet(url);
		// 加载请求头到httpget对象
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				httpget.addHeader(entry.getKey(), entry.getValue());
			}
		}
		// 执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
		CloseableHttpResponse httpResponse = httpclient.execute(httpget);

		return httpResponse;
	}
	
	
	//POST方法(如果不需要header可传入null),提交form表单(默认application/x-www-form-urlencoded)
	public static CloseableHttpResponse postForm(String url, Map<String, String> params, Map<String, String> headers)
			throws ClientProtocolException, IOException {
		// 设置请求头数据传输格式,使用表单提交的方式,postman中有写
		//headers.put("Content-Type","application/x-www-form-urlencoded");
		return post(url, null, params, headers);
	}

	//  POST方法,发送json格式(如果不需要h
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值