本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/15867903.html
添加工具类
常用变量解析到map,方便后面解析初始化sql或者入参的时候对变量进行替换。
VariableUtil.java
package com.qzcsbj.autotest.utils; import com.qzcsbj.autotest.entity.Variable; import org.apache.log4j.Logger; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class VariableUtil { public static Logger logger = Logger.getLogger(VariableUtil.class); // 存放变量和值的map public static Map<String, String> variableMap = new HashMap<String, String>(); // 从对象列表variableList中获取变量和值,放到map中 public static void loadVariablesToMap(List<Variable> variableList) { for (Variable variable : variableList){ String name = variable.getName(); String value = variable.getValue(); variableMap.put(name,value); } } }
修改@BeforeTest修饰的方法
调用VariableUtil工具类中方法
@Parameters({ "excelPath", "dataSheetName", "variableSheetName"}) @BeforeTest public void readDataFromExcel(@Optional("caseData/caseData.xlsx") String excelPath, @Optional("case") String dataSheetName, @Optional("variables") String variableSheetName){ logger.info("excelPath: " + excelPath); logger.info("dataSheetName: " + dataSheetName); logger.info("variableSheetName: " + variableSheetName); cases = ExcelUtil.loadExcel(excelPath, dataSheetName, CaseData.class); variables = ExcelUtil.loadExcel(excelPath, variableSheetName, Variable.class); // logger.info("读取文件获取到的cases对象:" + cases); // logger.info("读取文件获取到的variables对象:" + variables); VariableUtil.loadVariablesToMap(variables); Set<String> keys = VariableUtil.variableMap.keySet(); for (String key : keys) { System.out.println(key + "=" + VariableUtil.variableMap.get(key)); } }
结果
运行testng.xml
通过结果可以看到,常用变量放到了map中
标签:map,06,String,VariableUtil,import,java,logger,excelPath From: https://www.cnblogs.com/uncleyong/p/17923784.html