我正在使用 Selenium Webdriver 进行自动化测试,此代码适用于 TestNg dataprovider
,摘要:我正在将数据从 Excel 工作表获取到数据,
工作正常。当我调试代码时,第一次迭代的 TestData 为 testGoogle1(String search1, String Search2)
Search1 = Webdriver, Search2 = Qtp,依此类推,,,,
我想要的是它应该直接返回类似 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。因此,您的 return 语句应返回 {{tabArray}}
关于java - TestNG:使用 Dataprovider 迭代数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22021793/