使用Testng数据提供程序的数据驱动无法正常工作

时间:2018-08-27 06:44:43

标签: java selenium-webdriver testng testng-dataprovider

我正尝试使用apache poitestng数据提供者使用excel从Excel读取数据,但是testng跳过了我的测试方法。

以下是错误消息

SKIPPED: VerifyLoanDetails
java.lang.RuntimeException: java.lang.NullPointerException

Caused by: java.lang.NullPointerException at com.seleniumhybrid.utils.ExcelUtil.getTestdata(ExcelUtil.java:62) at com.seleniumdata.zmartano.LoanDetails.getdata(LoanDetails.java:50)

下面是我的班级,可以从excel中读取数据

public class ExcelUtil{

    static Workbook book;
    static Sheet  sheet;
    public static String Testdata_sheet = Constants.path_TestData;



    public static Object  [][] getTestdata(String sheetname) {



        FileInputStream file = null;
        try{
        file = new FileInputStream(Testdata_sheet);
        }
        catch(FileNotFoundException e) {

            e.printStackTrace();
        }


        try {

            book = WorkbookFactory.create(file);
        }
        catch(InvalidFormatException e ) {

            e.printStackTrace();
        }

        catch (IOException e) {

            e.printStackTrace();

        }


        sheet = book.getSheet(sheetname);

        Object [][]data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
        for (int i=0;i<sheet.getLastRowNum();i++) {

            for(int k=0;k<sheet.getRow(0).getLastCellNum();k++) {

                data[i][k]= sheet.getRow(i+1).getCell(k).toString();

            }


        }

        return data;



    }
}

下面是我访问数据的测试班

public class LoanDetails extends Configreader{



                  WebDriver driver;     

   @BeforeTest
        public  void beforetest() throws Exception { 

                  driver = Browser.GetBrowser();




       }






    @Test(dataProvider = "getdata")

   public  void VerifyLoanDetails(String username) throws Exception {


    driver.findElement(By.xpath(pro.getProperty("account_xpath"))).click();
    driver.findElement(By.xpath(pro.getProperty("username_id"))).sendKeys(username);


    }


    @DataProvider
    public Object[][] getdata(){

        Object data [][] = ExcelUtil.getTestdata("credentials");


        return data;


    }
}

0 个答案:

没有答案