【发布时间】:2015-09-19 12:32:35
【问题描述】:
我对 Selenium webdriver 和 Java 还很陌生,之前我使用过 Selenium IDE。我正在尝试从 Excel 工作表中读取测试数据。然后将其写入 Eclipse 控制台,它可以工作,并且应该用于执行实际测试,但它不起作用。实际测试未执行,因为我收到错误参数类型不匹配。代码如下所示:
package Test_Excel;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
@RunWith(Parameterized.class)
public class TestWithExcelData {
// Our two parameters
private final int input;
private final int resultExpected;
// Constructor
public TestWithExcelData(int input, int result) {
this.input = input;
this.resultExpected = result;
}
@Parameters
public static Iterable<Object []> data() throws BiffException, IOException
{
String FilePath = "C://Selenium//workspace//testproject//src//testdata//TestData.xls";
FileInputStream fs = new FileInputStream(FilePath);
Object[][] object = new Object[6][2];
Workbook wb = Workbook.getWorkbook(fs);
//locate the excel file in the local machine
Sheet sheet = wb.getSheet("IOResult");
int i=1; //avoid header row
while(!(sheet.getCell(0, i).getContents().equals("end"))) //read data till it reaches the cell whose text is ‘end’
{
object[i-1][0]=sheet.getCell(0, i).getContents();
object[i-1][1]=sheet.getCell(1, i).getContents();
System.out.print(sheet.getCell(0, i).getContents() + "\t");
System.out.print(sheet.getCell(1, i).getContents() + "\t");
System.out.println();
i++;
}
return Arrays.asList(object);
}
@Test
public void testSquareOff(){
Assert.assertEquals(resultExpected, MathUtils.square(input));
}
}
有人可以为我指明正确的方向吗?
提前致谢
【问题讨论】:
-
你能给我们看一个堆栈跟踪吗?
-
顺便说一句,您不必在 JUnit 4.12 中将对象数组包装到列表中。 data() 可能返回 Object[][]。见junit.org/javadoc/latest/org/junit/runners/Parameterized.html
标签: java eclipse excel selenium junit