Properties文件在编写的时候没编写一个值的都要写一次key,这个可以利用excel表格来解决。
1.在工程目录下创建一个excel的表格,填写数据
2.创建excel类,构建一个返回值是Object[][]的方法,读取excel表格中的数据
public class excel{
public static Object[][] readFromExcel_o(String dataFile,String sheetName,int rowNum) throws IOException {
ArrayList<Object> list = new ArrayList<Object>();
Object[][] data = null;
//创建workbook
File file = new File(dataFile);
try {
workbook = new HSSFWorkbook(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 读取excel数据
// 获得指定的excel表
HSSFSheet sheet = workbook.getSheet(sheetName);
// 获取表格的总行数
int rowCount = sheet.getLastRowNum() + 1;
// 获取表头的列数
int columnCount = sheet.getRow(0).getLastCellNum();
// 将表格中的 值传到一个list中
if(sheet!=null){
for(int i=1;i<=rowCount-1;i++){
for(int j=0;j<columnCount;j++){
HSSFRow row = sheet.getRow(i);
String concent = row.getCell(j).toString();
list.add(concent);
}
}
}
if (rowNum<=0||rowNum>=rowCount){
data = new Object[rowCount-1][columnCount] ;
int k=-1;
for (int i=0;i<rowCount-1;i++){
for (int j=0;j<columnCount;j++){
if (k<list.size()){
k++;
}
String concent = list.get(k).toString();
data[i][j]=list.get(k);
}
}
}else {
int k=-1;
data = new Object[rowNum][columnCount];
for (int i=0;i<rowNum-1;i++){
for (int j=0;j<columnCount-1;j++){
if (k<list.size()){
data[i][j]=list.get(k);
}
}
}
}
workbook.close();
return data;
}
}
3.DataProvider中调用readFromExcel_o方法读取excel的值并返回值给DataProvider
public class DataPro {
// 成功登陆的用户名密码
@DataProvider(name = "loginSuccess")
public static Object[][] data() throws IOException {
Object[][] data;
String text = System.getProperty("user.dir");
String file = text + "/taskdata_excel/danshang.xls";
data = ExcelUtil.readFromExcel_o(file, "sheet1", 3);
return data;
}
}
4.创建测试类,利用DataProvider传入的值执行脚本
public class Login_Test {
WebDriver driver;
@Test(dataProvider = "loginSuccess",alwaysRun = true, dataProviderClass = DataPro.class)
public void login(String account,String password) {
driver = SeleniumDriver.openBrowser("firefox","http://www.epwk.us/");
Action.click(LoginPage.loginButton);
// 输入账号密码登陆
Action.sendkeys(LoginPage.account, account);
Action.sendkeys(LoginPage.password, password);
Action.click(LoginPage.submintButton);
}
网友评论