接口自动化测试实战核心代码(泛型重构测试方法,参数泛型化,精简代码)

需要知识,反射,泛型

package com.test.dataprovider;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.testng.Assert;




/**
 * @author chenya
 *
 *         May 18, 2018-11:28:50 AM
 */
public class CommonGeneric {

	private static String urlpath;
	private static String excelpath;
	private static String resultexcel;
	private static String assertres;
	private static int index;
	private static boolean runresult;
	private static String loginfilepath;
	private static String user;

	public static String getUser() {
		return user;
	}

	public static void setUser(String user) {
		CommonGeneric.user = user;
	}

	public static String getPass() {
		return pass;
	}

	public static void setPass(String pass) {
		CommonGeneric.pass = pass;
	}

	private static String pass;

	public static String getLoginfilepath() {
		return loginfilepath;
	}

	public static void setLoginfilepath(String loginfilepath) {
		CommonGeneric.loginfilepath = loginfilepath;
	}

	public static  void CommonTest(List list, String method) throws Exception {

		System.out.println("执行编号为" + list.get(CommonEnum.caseNo.getValue()) + "测试用例" + "--->"
				+ list.get(list.size() - CommonEnum.description.getValue()) + "--》预期结果-----》"
				+ list.get(list.size() - CommonEnum.expectation.getValue()));

		String tempStr = list.get(CommonEnum.caseNo.getValue()).toString().split("-v1.0-")[1];

		String str = tempStr;

		String newStr = str.replaceFirst("^0*", "");

		System.out.println("No:" + "【" + newStr + "】");

		runresult = (boolean) CommonGeneric
				.getMethodAndparamslist(Thread.currentThread().getStackTrace()[1].getClassName(), "CommonClass", list);

		if (runresult == true) {

	

		} else if (runresult == false) {


			Assert.assertTrue(runresult);

		} 

	}

	public static String getExcelpath() {
		return excelpath;
	}

	public static void setExcelpath(String excelpath) {
		CommonGeneric.excelpath = excelpath;
	}

	// get CommonClass
	public static  Object getMethodAndparamslist(String cla, String method, List list)
			throws Exception {

		Class clazz = Class.forName(cla);

		Object object = clazz.newInstance();

		Method Tmethod = clazz.getDeclaredMethod(method, List.class);

		Tmethod.setAccessible(true);

		return Tmethod.invoke(object, new Object[] { list });

	}
	
	public static  Map CommonData(List names) throws Exception {

		Map mapforParams = new TreeMap();

		names.forEach(n -> System.out.print(n + "\t"));

		for (int i = 3; i < names.size() - CommonEnum.description.getValue(); i++) {

			mapforParams.put(names.get(i), names.get(i + 1));

			i = i + 1;

		}
		return mapforParams;
		
	}

	public static List CommonClass(List values) throws Exception {

		List Params = new ArrayList<>();

		values.forEach(n -> System.out.print(n + "\t"));

		for (int i = 1; i < values.size() - CommonEnum.description.getValue(); i++) {

			Params.add(values.get(i));


		}
		return Params;



	}

	// get excel data for CommonTest‘s list

	public static  List GetParams(Object... params) {

		List list = new ArrayList<>();

		synchronized (list) {

			for (Object t : params) {

				list.add(t);
			}

			return list;
		}

	}

	public static String getUrlpath() {

		return urlpath;
	}

	public static synchronized void setUrlpath(String urlpath) {

		CommonGeneric.urlpath = urlpath;
	}

	public static synchronized String getAssertres() {
		return assertres;
	}

	public static synchronized void setAssertres(String assertres) {
		CommonGeneric.assertres = assertres;
	}

	public static int getIndex() {
		return index;
	}

	public static synchronized void setIndex(int index) {
		CommonGeneric.index = index;
	}

	public static synchronized String getResultexcel() {
		return resultexcel;
	}

	public static synchronized void setResultexcel(String resultexcel) {
		CommonGeneric.resultexcel = resultexcel;
	}
}
 
  

 

 

package com.test.dataprovider;


public enum CommonEnum {

	caseNo("用例编号",0), 
	description("用例描述",4), 
	expectation("预期结果",3),
	methodname("接口名称",2),
	username("用户名",1);
	private String name;
	private int value;

	CommonEnum(String name, int value) {
		this.name = name;
		this.value = value;
	}

	public String getName() {

		return name;
	}

	public int getValue() {

		return value;
	}

	public void setName(String name) {

		this.name = name;

	}

	public void setValue(int value) {

		this.value = value;

	}
}
	@Test(dataProvider = "CommonData", dataProviderClass = FlyDataProviderClass.class)
	public void smslogin(T... params) throws Exception {
	
			CommonGeneric.CommonTest(2, CommonGeneric.GetParams(params),
					Thread.currentThread().getStackTrace()[1].getMethodName());

	}

 

你可能感兴趣的:(java,java高级测试技术)