Properties文件在編寫的時(shí)候沒編寫一個(gè)值的都要寫一次key,這個(gè)可以利用excel表格來解決岳颇。
1.在工程目錄下創(chuàng)建一個(gè)excel的表格,填寫數(shù)據(jù)
2.創(chuàng)建excel類薪夕,構(gòu)建一個(gè)返回值是Object[][]的方法,讀取excel表格中的數(shù)據(jù)
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;
//創(chuàng)建workbook
File file = new File(dataFile);
try {
workbook = new HSSFWorkbook(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 讀取excel數(shù)據(jù)
// 獲得指定的excel表
HSSFSheet sheet = workbook.getSheet(sheetName);
// 獲取表格的總行數(shù)
int rowCount = sheet.getLastRowNum() + 1;
// 獲取表頭的列數(shù)
int columnCount = sheet.getRow(0).getLastCellNum();
// 將表格中的 值傳到一個(gè)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中調(diào)用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.創(chuàng)建測(cè)試類柔吼,利用DataProvider傳入的值執(zhí)行腳本
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);
// 輸入賬號(hào)密碼登陸
Action.sendkeys(LoginPage.account, account);
Action.sendkeys(LoginPage.password, password);
Action.click(LoginPage.submintButton);
}