我正在从excel读取数据并运行我的测试用例。可以使用testNg和eclipse在一个测试单元中运行多个测试方法.Below是我的代码。我试了但是得到了错误
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161) at org.testng.internal.Parameters.handleParameters(Parameters.java:429) 在org.testng.internal.Invoker.handleParameters(Invoker.java:1383) 在org.testng.internal.Invoker.createParameters(Invoker.java:1075) 在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
login.java
import com.thoughtworks.selenium.*;
import org.junit.AfterClass;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import java.io.File;
import jxl.*;
public class login extends SeleneseTestCase {
@BeforeClass
public void setUp() throws Exception {
try {
System.out.println("in setup");
SeleniumServer seleniumserver = new SeleniumServer();
seleniumserver.boot();
seleniumserver.start();
// String
// firefoxPath="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe";
setUp("http://localhost:8080/",
"*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
// selenium.open("/");
selenium.windowMaximize();
selenium.windowFocus();
} catch (Exception ex) {
System.out.println(ex);
}
}
@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception {
System.out.println("in createData1");
Object[][] retObjArr = getTableArray(
"test\\Resources\\Data\\logins.xls", "loginSheet", "Login");
return (retObjArr);
}
@Test(dataProvider = "DP1",testName="checkLogin")
public void testLogin1(String userName, String Password,
String expectedResult) throws Exception {
System.out.println("in testLogin1");
System.out.println("userName: " + userName + " Password: " + Password
+ " expectedResult: " + expectedResult);
selenium.open("Banking/Login.jsp/");
Thread.sleep(1000);
selenium.type("id=fname", userName);
Thread.sleep(1000);
selenium.type("id=Lname", Password);
Thread.sleep(1000);
selenium.click("name=sub");
Thread.sleep(1000);
boolean res = selenium.isTextPresent(expectedResult);
System.out.println("ver: " + res);
verifyTrue(res);
Thread.sleep(1000);
}
@Test(dataProvider = "DP1" ,testName="homepage")
public void testLogin2(String userName, String Password,
String expectedResult) throws Exception {
System.out.println("in testLogin2");
}
@AfterClass
public void tearDown() {
selenium.close();
selenium.stop();
}
/* this method read the data from excel sheet */
public String[][] getTableArray(String xlFilePath, String sheetName,
String tableTagName) throws Exception {
String[][] tabArray = null;
System.out.println("in getTableArray");
System.out.println("table: " + tableTagName + " xlFilePath:"
+ xlFilePath + " sheetName: " + sheetName);
Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet sheet = workbook.getSheet(sheetName);
int startRow, startCol, endRow, endCol, ci, cj;
Cell tableStart = sheet.findCell(tableTagName);// find table name in
// excel
startRow = tableStart.getRow();
startCol = tableStart.getColumn();
Cell tableEnd = sheet.findCell(tableTagName, 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();
// System.out.println("tabArray: "+ tabArray[ci][cj]);
}
}
return (tabArray);
}
}