All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.poi.ParamAnalysis Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
package tools.poi;

/**
 * Created by zhengyu06 on 2017/9/12
 */

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.testng.Reporter;
import tools.config.SystemConfig;

import java.util.ArrayList;
import java.util.List;

public class ParamAnalysis {
	private ExcelDataSource edc = null;
	private int totalRow = 0;
	private int totalCol = 0;
	private String paramFilePath = "";

	public ParamAnalysis(String paramFilePath, String sheetName) {
		this.edc = new ExcelDataSource(paramFilePath, sheetName);
		this.paramFilePath = paramFilePath;
		this.totalRow = this.edc.excelGetRows();
		this.totalCol = this.edc.excelGetCols(0);
		Reporter.log("Total Rows = "+this.totalRow,true);
		Reporter.log("Total Cols = "+this.totalCol,true);
	}
	
	public String getCaseName(int rowNum) {
		String casename = "";
		if(rowNum < this.totalRow) {
			casename = this.edc.getCellData(rowNum, 0);
			Reporter.log("casename is: "+ casename,true);
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}
		return casename;
	}
	
	public String getHostName(int rowNum) {
		String hostname = "";
		if(rowNum < this.totalRow) {
			hostname = this.edc.getCellData(rowNum, 1);
			Reporter.log("hostname is: "+ hostname,true);
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}
		return hostname;
	}
	
	public String getUri(int rowNum) {
		String uri = "";
		if(rowNum < this.totalRow) {
			uri = this.edc.getCellData(rowNum, 2);
			Reporter.log("uri is: "+ uri,true);
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}
		return uri;
	}
	
	public String getMethod(int rowNum) {
		String method = "";
		if(rowNum < this.totalRow) {
			method = this.edc.getCellData(rowNum, 3);
			Reporter.log("method is: "+ method,true);
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}
		return method;
	}

	public String getJsonParam(int rowNum) {
		String jsonParam = "";
		if(rowNum < this.totalRow) {
			jsonParam = this.edc.getCellData(rowNum, 5);
			Reporter.log("JSON Param is: \n"+ jsonParam,true);
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}
		return jsonParam;
	}
	
	public List getParamList(int rowNum) {
		int count = 0;
		int colNum = this.edc.excelGetCols(rowNum);//这个只能获得有值得列的个数,包括空值的所有列的个数。
		Reporter.log("第"+(rowNum+1)+"行一共有"+colNum+"列。",true);
		List params = new ArrayList();
		String currKey = "", currVal = "";
		int INITIATE_COLUMN_INDEX = Integer.parseInt(SystemConfig.getConfigInfomation("INITIATE_COLUMN_INDEX"));
		if(rowNum < this.totalRow) {
			for(int i = INITIATE_COLUMN_INDEX; i < this.totalCol; i++) {//因为每行的中间可能有空值,所以要用整个全部列数进行循环全部。
				currKey = this.edc.getCellData(0, i);
				currVal = this.edc.getCellData(rowNum, i);
				if(currVal != null && !"".equals(currVal)) {
					count++;
					params.add(new BasicNameValuePair(currKey, currVal)); 
				}else {
//					Reporter.log(currKey+":不需要此参数。");
				}
			}
			Reporter.log("参数列表为:"+params,true);
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}
		Reporter.log("第"+(rowNum+1)+"行一共有"+count+"个基本参数。",true);
		return params;
	}

	public void setNewParam(int rowNum, String name, String value){
		String currKey = "";
		boolean flag = false;
		if(rowNum < this.totalRow) {
			int INITIATE_COLUMN_INDEX = Integer.parseInt(SystemConfig.getConfigInfomation("INITIATE_COLUMN_INDEX"));
			for(int i = 5; i < this.totalCol; i++) {//因为每行的中间可能有空值,所以要用整个全部列数进行循环全部。
				//从5开始而不是INITIATE_COLUMN_INDEX的值=6开始,是因为有可能POST的JSON串也要程序中进行动态更改。
//			for(int i = INITIATE_COLUMN_INDEX; i < this.totalCol; i++) {//因为每行的中间可能有空值,所以要用整个全部列数进行循环全部。
				currKey = this.edc.getCellData(0, i);
				if (name.contentEquals(currKey)){//如果既有表格有这一列,直接将此列cell值设置为传进来的参数值。
					if (this.edc.excelGetCell(rowNum, i) == null) {//如果这一列的对应行cell不存在
						this.edc.createCellValue(rowNum, i, value);
					} else {//如果这一列的对应行cell已经存在
						this.edc.setCellValue(rowNum, i, value);
					}
					Reporter.log("您在第"+(rowNum+1)+"行,"+(i+1)+"列,新设置的参数对为:"+name+"="+value,true);
					break;
				}else if (i == (this.totalCol-1)){//等于既有表格的最后一列了。
					this.edc.createCellValue(0, this.totalCol, name);
					this.edc.createCellValue(rowNum, this.totalCol, value);
					Reporter.log("您在第"+(rowNum+1)+"行,新增第"+(this.totalCol+1)+"列,新设置的参数对为:"+name+"="+value,true);
					flag = true;
				}
			}
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}

		if (flag)//如果flag = true,则说明参数Excel文件在最后新增了一列,用来接受动态传参。
			this.totalCol++;//将私有成员整个参数Excel的列数自加一。

		this.edc.saveExcel(this.paramFilePath);//保存参数Excel,用来后续创建NameValuePair
		Reporter.log("您的参数Excel动态添加变量参数后保存成功。",true);
	}

	public String getValidationString(int rowNum){
		return this.edc.getCellData(rowNum,4);
	}

	public ExcelDataSource getEdc(){
		return this.edc;
	}

	public String getCellValueByName(int rowNum, String fieldName){
		String currKey = "";
		String value = "";
		if(rowNum < this.totalRow) {
			int INITIATE_COLUMN_INDEX = Integer.parseInt(SystemConfig.getConfigInfomation("INITIATE_COLUMN_INDEX"));
			for(int i = INITIATE_COLUMN_INDEX; i < this.totalCol; i++) {//因为每行的中间可能有空值,所以要用整个全部列数进行循环全部。
				currKey = this.edc.getCellData(0, i);
				if (fieldName.toLowerCase().contentEquals(currKey.toLowerCase())){//如果既有表格有这一列,直接将此列cell值设置为传进来的参数值。
					Reporter.log("您指定的"+fieldName+"字段在Excel数据源中的第"+(i+1)+"列。",true);
					if (this.edc.excelGetCell(rowNum, i) == null) {//如果这一列的对应行cell不存在
						Reporter.log("您指定的第"+(rowNum+1)+"行,第"+(i+1)+"列 的值为空值。",true);
					} else {//如果这一列的对应行cell已经存在
						value = this.edc.getCellData(rowNum, i);
						Reporter.log("您指定的第"+(rowNum+1)+"行,第"+(i+1)+"列 的值为: "+value,true);
					}
					break;
				}else if (i == (this.totalCol-1)){//等于既有表格的最后一列了。
					Reporter.log("您所要获取的"+fieldName+"字段在Excel数据源中不存在。",true);
				}
			}
		}else {
			Reporter.log("您输入的行数已经超出范围。",true);
		}
		return value;
	}
	
	public void test() {
		for (int i=1; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy