📜  使用 Excel 的 TestNG 数据提供程序 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:27.682000             🧑  作者: Mango

代码示例1
package testng;

import java.util.Hashtable;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class testNGParameterizationExcel {

  public static ExcelReader excel = null;

  @Test(dataProvider = "getData")
  public void testData(Hashtable data) {
    // System.out.println(username + "---------" + password + "------------"
    // + is_correct);
    System.out.println(data.get("UserName"));
  }

  @DataProvider
  public static Object[][] getData() {
    if (excel == null) {
      excel = new ExcelReader("C:\\Users\\shaanu\\Desktop\\Java\\Test.xlsx");
    }

    String sheetName = "new";
    int rows = excel.getRowCount(sheetName);
    int columns = excel.getColumnCount(sheetName);
    Object[][] data = new Object[rows - 1][1];
    Hashtable table = null;
    for (int rowNums = 2; rowNums <= rows; rowNums++) {
      table = new Hashtable();
      for (int colNum = 0; colNum < columns; colNum++) {
        // data[rowNums-2][colNum] = excel.getCellData(sheetName,
        // colNum, rowNums);
        table.put(excel.getCellData(sheetName, colNum, 1), excel.getCellData(sheetName, colNum, rowNums));
        data[rowNums - 2][0] = table;
      }
    }

    return data;
  }
}