我按照http://functionaltestautomation.blogspot.com/2009/10/dataprovider-data-driven-testing-with.html?showComment=1321643221128上的說明創建了我的腳本。示例代碼完美工作。除了@test部分之外,我沒有做任何重大更改。出於某種原因,我得到錯誤java.lang.NullPointerException。我無法弄清楚可能是什麼原因造成的。Testng java.lang.NullPointerException
package script;
import com.thoughtworks.selenium.*;
import org.junit.AfterClass;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import java.io.File;
import jxl.*;
@SuppressWarnings("deprecation")
public class Testcase1 extends SeleneseTestCase{
@BeforeClass
public void setUp() throws Exception {
//SeleniumServer seleniumserver=new SeleniumServer();
//seleniumserver.boot();
//seleniumserver.start();
setUp("http://devblade08-08:8080", "*firefox");
selenium.open("/");
selenium.windowMaximize();
selenium.windowFocus();
}
@DataProvider(name = "Data")
public Object[][] createData1() throws Exception{
Object[][] retObjArr=getTableArray("test\\Resources\\Data\\TestCase1.xls",
"Scenario", "TestCase1");
return(retObjArr);
}
@Test (dataProvider = "Data")
public void testTestcase1(String Username, String Password, String Performance) throws Exception {
selenium.open("/");
selenium.click("link=» AudienceView Online");
selenium.waitForPageToLoad("30000");
selenium.type("name=BOset::WScontent::SearchCriteria::search_criteria", "Star Trek Automation");
selenium.click("css=input[type=\"submit\"]");
selenium.waitForPageToLoad("30000");
selenium.click("//div[@id='search_results']/form/table/tbody/tr[12]/td[5]/a/span");
selenium.waitForPageToLoad("30000");
selenium.click("css=div.seattab_off");
selenium.waitForPageToLoad("30000");
selenium.click("id=BOparam::WSorder::getBestAvailable::reqNum::EA25EE97-EA9B-41B2-8D03-BC6B647F1255::select");
selenium.select("id=BOparam::WSorder::getBestAvailable::reqNum::EA25EE97-EA9B-41B2-8D03-BC6B647F1255::select", "label=2");
selenium.click("css=option[value=\"2\"]");
selenium.click("name=doWork::WSorder::getBestAvailable");
selenium.waitForPageToLoad("30000");
selenium.click("id=popupDiv_okayButton");
selenium.click("name=continue");
selenium.waitForPageToLoad("30000");
selenium.click("name=loginName");
selenium.type("name=loginName", "banana");
selenium.type("name=loginPassword", "banana");
selenium.click("name=login");
selenium.waitForPageToLoad("30000");
}
@AfterClass
public void tearDown(){
selenium.close();
selenium.stop();
}
public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
String[][] tabArray=null;
Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet sheet = workbook.getSheet(sheetName);
int startRow,startCol, endRow, endCol,ci,cj;
Cell tableStart=sheet.findCell(tableName);
startRow=tableStart.getRow();
startCol=tableStart.getColumn();
Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000, false);
endRow=tableEnd.getRow();
endCol=tableEnd.getColumn();
System.out.println("startRow="+startRow+", endRow="+endRow+", " +
"startCol="+startCol+", endCol="+endCol);
tabArray=new String[endRow-startRow-1][endCol-startCol-1];
ci=0;
for (int i=startRow+1;i<endRow;i++,ci++){
cj=0;
for (int j=startCol+1;j<endCol;j++,cj++){
tabArray[ci][cj]=sheet.getCell(j,i).getContents();
}
}
return(tabArray);
}
}//end of class
呃,只需使用一個調試器。 –