我正在使用 Selenium Webdriver 进行自动化测试,此代码用于 TestNg
dataprovider
,摘要:我正在从 Excel 工作表中获取数据,
它工作正常。当我调试代码时,我将 TestData 作为第一次迭代的testGoogle1(String search1, String Search2)
它Search1 = Webdriver, Search2 = Qtp, so on,,,,
我想要的是它应该直接返回像testGoogle1(String search[])
这样的值数组,这样在@Test
本身我可以添加我的函数迭代所有行和列并测试它们。
谁能告诉我怎么写。
测试数据表
这是我的代码
package ExcelTest;
import com.thoughtworks.selenium.*;
import static org.testng.AssertJUnit.*;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import jxl.*;
public class Sample{
WebDriver driver;
@BeforeTest
public void startTest(){
driver = Startup.basic();
}
@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
Object[][] retObjArr=getTableArray("G:\\Selenium Jar Files\\TestData\\Data.xls","DataPool");
return(retObjArr);
}
@Test (dataProvider = "DP1")
public void testGoogle1(String search1, String Search2) throws Exception{
//selenium.open("http://www.google.co.in/");
// driver.get("http://www.google.co.in/");
//String hello = search.length;
//for(int i=0; i< search.length ;i++)
//{
System.out.println("param " +search);
Thread.sleep(3000);
System.out.println("Opened");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys(search);
element.submit();
System.out.println("Clicked");
}
//}
@AfterClass
public void tearDown() throws Exception {
//selenium.stop();
}
public String[][] getTableArray(String xlFilePath,String sheetName) throws Exception{
String[][] tabArray=null;
File inputWorkbook = new File(xlFilePath);
Workbook w;
int startRow,startCol, endRow, endCol,ci,cj;
try {
//w = Workbook.
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(sheetName);
// Loop over first 10 column and lines
endRow = sheet.getRows();
endCol = sheet.getColumns();
tabArray=new String[endRow-1][endCol-1];
ci=0;
for (int i=1;i<endRow;i++,ci++){
cj=0;
for (int j=1;j<endCol;j++,cj++){
Cell cell = sheet.getCell(j, i);
tabArray[ci][cj] = cell.getContents();
}
// System.out.println("");
}
//file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return(tabArray);
}
}
任何人都可以分享任何想法吗? 谢谢
编辑代码:
public class Sample{
WebDriver driver;
@BeforeTest
public void startTest(){
driver = Startup.basic();
}
@DataProvider(name = "DP1")
public Object[][][] createData1() throws Exception{
Object[][][] retObjArr=getTableArray("G:\\Selenium Jar Files\\TestData\\Data.xls","DataPool");
return (retObjArr);
}
@Test (dataProvider = "DP1")
public void testGoogle1(String search, String het) throws Exception{
System.out.println("param " +search);
Thread.sleep(3000);
System.out.println("Opened");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys(search);
element.submit();
System.out.println("Clicked");
}
//}
@AfterClass
public void tearDown() throws Exception {
//selenium.stop();
}
public Object[][][] getTableArray(String xlFilePath,String sheetName) throws Exception{
Object[][] tabArray=null;
File inputWorkbook = new File(xlFilePath);
Workbook w;
int startRow,startCol, endRow, endCol,ci,cj,ck;
try {
//w = Workbook.
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(sheetName);
// Loop over first 10 column and lines
endRow = sheet.getRows();
endCol = sheet.getColumns();
tabArray=new String[endRow-1][endCol-1];
ci=0;
for (int i=1;i<endRow;i++,ci++){
cj=0;
for (int j=1;j<endCol;j++,cj++){
Cell cell = sheet.getCell(j, i);
tabArray[ci][cj] = cell.getContents();
}
// System.out.println("");
}
//file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return(tabArray); /// Here Getting the error **Type mismatch: cannot convert from Object[][] to Object[][][]**
}
}
这就是
@DataProvider
的工作原理,假设我有以下数组:
[[value1, value2],
[value3, value4],
[value5, value6]]
注意有3行2列。测试将运行 3 次,每次传递 2 个值。价值是多少并不重要。
现在,如果您希望测试只运行一次,您的数组应该如下所示:
[[value1]]
我们可以让
value1
成为我们想要的任何东西,所以如果value1
是上面的数组,那么它会将整个数组传递给dataProvider。因此,您的退货声明应返回{{tabArray}}
查看您的代码,您正在尝试创建一个返回 Object[][][] 的数据提供程序函数。你不能这样做; dataprovider 函数必须返回 Object[][](任何对象的二维数组)或 Iterator,它将执行相同的功能但方式略有不同。
您当然可以在返回的二维数组中嵌套另一个数组;这就是为什么它是 Object[][] 而不是特定类型的原因。
还要确保正确构建返回结果。例如,您不能执行以下操作,因为它不会构造 Object[][] 数组:
return {{myArray}};
相反,你会这样做:
return new Object[][]{{myArray}};
我认为jxl比它需要的更难。我写了一个 TestNG DataProvider reading Excel data using Apache MetaModel(最近成为一个完整的 Apache 项目)的例子,你可以在here.
public static Object[][] getCsvData( File csvFile )
{
CsvConfiguration conf = new CsvConfiguration( 1 );
DataContext csvContext = DataContextFactory.createCsvDataContext( csvFile, conf );
Schema schema = csvContext.getDefaultSchema();
Table[] tables = schema.getTables();
Table table = tables[0]; // a representation of the csv file name including extension
DataSet dataSet = csvContext.query()
.from( table )
.selectAll()
.where("run").eq("Y")
.execute();
List<Row> rows = dataSet.toRows();
Object[][] myArray = get2ArgArrayFromRows( rows );
return myArray;
}
第二部分:
public static Object[][] get2ArgArrayFromRows( List<Row> rows ) {
Object[][] myArray = new Object[rows.size()][2];
int i = 0;
SelectItem[] cols = rows.get(0).getSelectItems();
for ( Row r : rows ) {
Object[] data = r.getValues();
for ( int j = 0; j < cols.length; j++ ) {
if ( data[j] == null ) data[j] = ""; // force empty string where there are NULL values
}
myArray[i][0] = cols;
myArray[i][1] = data;
i++;
}
logger.info( "Row count: " + rows.size() );
logger.info( "Column names: " + Arrays.toString( cols ) );
return myArray;
}
以及使用此 DataProvider 的测试示例:
@Test( dataProvider = "csv" )
public void testPrintCsvRowToLog( SelectItem[] cols, Object[] data ) {
String theCols = Joiner.on("|").join( cols );
String aRow = Joiner.on("|").join( data );
logger.info( theCols );
logger.info( aRow );
}
@Test 注解不能使用 TestNg Parameter 和 Dataprovider
BeforeTest/Beforeclass 注解最好使用 DataProvider 注解,@Test 注解最好使用 @Parameter 注解