jmeter 利用脚本读取excel 获取测试用例,并动态设置变量。

1、前置控制器的BeanShell 脚本。代码如下


import java.io.*;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
			String path = "E:/autoTest/Jemter/demo/test_case_data.xls";
			Workbook readwb = null;
			// 构建Workbook对象, 只读Workbook对象
			// 直接从本地文件创建Workbook
			InputStream instream = new FileInputStream(path);
			readwb = Workbook.getWorkbook(instream);
			// Sheet的下标是从0开始
			// 获取第一张Sheet表
			Sheet readsheet = readwb.getSheet(0);
			// 获取Sheet表中所包含的总列数
			int rsColumns = readsheet.getColumns();
			// 获取Sheet表中所包含的总行数
			int rsRows = readsheet.getRows();
			Cell cell= null;
			// 获取指定单元格的对象引用
			List list = new ArrayList();
			rsRows = (${__threadNum}+1);
			print("线程数============================================== :"+${__threadNum});
			for (int i = 0; i < rsRows; i++)//rsRows
			{
				for (int j = 0; j < rsColumns; j++)
				{	
					cell = readsheet.getCell(j, i);
					if(i == 0){
						list.add(cell.getContents());
						log.info("KEY: "+cell.getContents());
						//vars.put("city",readsheet.getCell(1, 1).getContents());
						continue;
					}
					log.info(list.get(j)+" , "+cell.getContents() + " \n");
					vars.put(list.get(j),cell.getContents());
					//每运行一次,取一行数据设为变量。+"_"+i
				}
			}
			log.info("数据总行数 : "+(cell.getRow()+1)+" 列:"+cell.getColumn());


你可能感兴趣的:(Jemter,接口测试)