我如何使用Apache poi的dataprovider
请帮忙!。 我有一个用Selenium框架新手,我有一个方法,接受5个参数预订一个派对; 它使用dataprovider从excel文件读取。 问题是(如下所示)它使用JXL导入只支持XLS文件(Excel 2003或更旧)。 我需要使用Apache Poi的类似代码的帮助,以便它支持XLSX和新版本的Excel(2007+)。 有人能帮助我吗?
package com.suite1; import util.TestUtil; import java.io.File; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.testng.Assert; import org.testng.SkipException; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class CreatePartyTest extends TestBase1 { Workbook wb; Sheet sh1; int numrow; @BeforeTest public void beforeTest() throws IOException { initialize(); if (TestUtil.isSkip("CreatePartyTest")) { throw new SkipException("Skipping test, check run mode"); } dr.get(CONFIG.getProperty("testSiteName")); getobject("signin_link").click(); getobject("username_Signin_input").sendKeys("alexy.dsouza"); getobject("password_input").sendKeys("testing123"); getobject("submit_button").click(); } @Test(dataProvider="Partydata") public void createParty (String Partyname, String Date, String Firstname, String Lastname, String email, String mobile) throws InterruptedException { getobject("party_link").click(); getobject("start_party_link").click(); getobject("partyname_input").sendKeys(Partyname); getobject("partydate_input").sendKeys(Date); getobject("hostfirstname_input").sendKeys(Firstname); getobject("hostlastname_input").sendKeys(Lastname); getobject("hostemail_input").sendKeys(email); getobject("hostmobile_input").sendKeys(mobile); getobject("make_reservation").click(); } //source @DataProvider(name="Partydata") public Object[][] TestDataFeed(){ try { // load workbook: this is where i store my excel wb=Workbook.getWorkbook(new File("C://Workspace//Max//excelfiles//Partydata.xls")); // load sheet in my case I am referring to first sheet only sh1= wb.getSheet(0); // get number of rows so that we can run loop based on this numrow= sh1.getRows(); } catch (Exception e) { e.printStackTrace(); } // Create 2 D array and pass row and columns Object [][] Accountdata=new Object[numrow-1][sh1.getColumns()]; // This will run a loop and each iteration it will fetch new row for(int i=0,j=1;i<numrow-1;i++){ // Fetch first row Accountname Accountdata[i][0]=sh1.getCell(0,j).getContents(); // Fetch first row BankName Accountdata[i][1]=sh1.getCell(1,j).getContents(); // Fetch everything else before an empty column Accountdata[i][2]=sh1.getCell(2,j).getContents(); Accountdata[i][3]=sh1.getCell(3,j).getContents(); Accountdata[i][4]=sh1.getCell(4,j++).getContents(); }// Return 2d array object so that test script can use the same return Accountdata; } }